<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                本站文章均為[?李華明Himi?](http://www.himigame.com/about-himi)原創,轉載務必在明顯處注明: 轉載自[【黑米GameDev街區】](http://www.himigame.com/)?原文鏈接:?[http://www.himigame.com/iphone-cocos2dx/721.html](http://www.himigame.com/iphone-cocos2dx/721.html "【iOS-cocos2d-X") [? 點擊訂閱 ?](http://list.qq.com/cgi-bin/qf_invite?id=acfc24e272cc4a26debf3b3866edb626a9ea3fc80fd8893c)**?本博客最新動態!及時將最新博文通知您! 對于虛擬搖桿在游戲開發中必不可少,Android方面的是由Himi自己實現封裝的,大家可以移步到這里查看詳細實現機制: [【Android游戲開發二十四】360°平滑游戲搖桿(觸屏方向導航)?](http://www.himigame.com/android-game/384.html) 那么在Cocos2d引擎已提供此搖桿類(Joystick),所以Himi也就懶得重寫了,但是Cocos2dx中并沒有封裝,那么這里Himi給出Cocos2dx版的Joystick(HRocker類),并且Himi對此類添加了一個跟隨用戶觸點作為搖桿坐標的功能! 這里不多說代碼結構直接貼出源碼,然后重點說下使用與方法參數,具體實現可以參考源碼以及Android部分Himi的實現機制; ~~~ // // HRocker.h // RockerPro // // Created by Himi on 12-3-30. // Copyright (c) 2012年 Himi. All rights reserved. // #ifndef RockerPro_HRocker_h #define RockerPro_HRocker_h #ifndef HRocker_H #define HRocker_H #include "cocos2d.h" using namespace cocos2d; class HRocker :public CCLayer { public : //初始化 aPoint是搖桿中心 aRadius是搖桿半徑 aJsSprite是搖桿控制點 aJsBg是搖桿背景 static HRocker* HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole); //啟動搖桿 void Active(); //解除搖桿 void Inactive(); private: HRocker * initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole); CCPoint centerPoint;//搖桿中心 CCPoint currentPoint;//搖桿當前位置 bool active;//是否激活搖桿 float radius;//搖桿半徑 CCSprite *jsSprite; bool isFollowRole;//是否跟隨用戶點擊 CCPoint getDirection(); float getVelocity(); void updatePos(ccTime dt); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); LAYER_NODE_FUNC(HRocker); }; #endif ~~~ ~~~ // // HRocker.cpp // RockerPro // // Created by Himi on 12-3-30. // Copyright (c) 2012年 Himi. All rights reserved. // #include "HRocker.h" void HRocker::updatePos(ccTime dt){ jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint, jsSprite->getPosition()),0.5))); } //啟動搖桿 void HRocker::Active() { if (!active) { active=true; schedule(schedule_selector(HRocker::updatePos));//添加刷新函數 CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0,false);//添加觸摸委托 }else { } } //解除搖桿 void HRocker::Inactive() { if (active) { active=false; this->unschedule(schedule_selector(HRocker::updatePos));//刪除刷新 CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);//刪除委托 }else { } } //搖桿方位 CCPoint HRocker::getDirection() { return ccpNormalize(ccpSub(centerPoint, currentPoint)); } //搖桿力度 float HRocker::getVelocity() { return ccpDistance(centerPoint, currentPoint); } HRocker* HRocker:: HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){ HRocker *jstick=HRocker::node(); jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg,_isFollowRole); return jstick; } bool HRocker::ccTouchBegan(CCTouch* touch, CCEvent* event) { if (!active) return false; this->setIsVisible(true); CCPoint touchPoint = touch->locationInView(touch->view()); touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint); if(!isFollowRole){ if (ccpDistance(touchPoint, centerPoint) > radius){ return false; } } currentPoint = touchPoint; if(isFollowRole){ centerPoint=currentPoint; jsSprite->setPosition(currentPoint); this->getChildByTag(88)->setPosition(currentPoint); } return true; } void HRocker::ccTouchMoved(CCTouch* touch, CCEvent* event) { CCPoint touchPoint = touch->locationInView(touch->view()); touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint); if (ccpDistance(touchPoint, centerPoint) > radius) { currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius)); }else { currentPoint = touchPoint; } } void HRocker::ccTouchEnded(CCTouch* touch, CCEvent* event) { currentPoint = centerPoint; if(isFollowRole){ this->setIsVisible(false); } } HRocker* HRocker::initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){ isFollowRole =_isFollowRole; active = false; radius = aRadius; if(!_isFollowRole){ centerPoint =aPoint; }else{ centerPoint =ccp(0,0); } currentPoint = centerPoint; jsSprite = aJsSprite; jsSprite->setPosition(centerPoint); aJsBg->setPosition(centerPoint); aJsBg->setTag(88); this->addChild(aJsBg); this->addChild(jsSprite); if(isFollowRole){ this->setIsVisible(false); } this->Active();//激活搖桿 return this; } ~~~ 創建使用方法很eazy,如下函數: HRocker* HRocker:: HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole); 第一個參數aPoint:搖桿中心點的坐標; 第二個參數aRadius: 搖桿的半徑 第三個參數:aJsSprite :搖桿的圖片資源精靈 第四個參數:aJsBg: 搖桿背景圖片資源精靈 第五個參數:isFollowRole:是否讓搖桿永遠跟隨用戶觸屏點(Himi新添加的功能) 這里對于最后一個參數可能很多童鞋不太理解,那么這里大概描述下: 對于手機游戲而言,虛擬的搖桿并不是一個很好的操作方式,但是為了滿足游戲的必要操作無疑必須使用,但是虛擬搖桿存在兩方面問題: 1.沒有實體感覺,對于用戶來說不能觸覺上明顯分清當前自己有沒有觸摸在虛擬搖桿上或者當前是按下還是按上等; 2.遮擋部分游戲畫面,這一點不僅僅式虛擬搖桿的存在造成遮擋畫面,用戶使用虛擬搖桿時更加的造成游戲畫面被擋住; 3.不容易操作,過于死板,不小心就觸發了虛擬搖桿區域之外; 對于虛擬搖桿存在的第一方面沒有實體感我們沒法改進,但是,是否觸摸到虛擬鍵盤這個可以使用手機震動提示;第二,三方面的問題在當前iOS手機游戲上很多公司采用了讓虛擬搖桿跟隨用戶觸屏點為搖桿中心的方式!并且用戶不觸摸屏幕默認不顯示虛擬搖桿;這么一來不僅讓游戲畫面能在不需要操作的時候盡可能的完美展示外,還能有效避免用戶觸摸不到搖桿判斷區域的問題;搖桿跟隨功能就是Himi封裝Rocker類創建時第五個參數?isFollowRole,傳入true即可跟隨! 如果還有童鞋聽的不是很清楚,那么將Himi這個Rocker類進行拷貝自己項目中,然后使用以下代碼進行創建使用嘗試下吧: ~~~ CCSprite *spRocker=CCSprite::spriteWithFile("CloseSelected.png");//搖桿 CCSprite *spRockerBG=CCSprite::spriteWithFile("rockerBg.png");//搖桿背景 HRocker *rocker=HRocker::HRockerWithCenter(ccp(210.0f,130.0f),50.0f ,spRocker ,spRockerBG,false);//創建搖桿 this->addChild(rocker);//搖桿添加到layer中 //this 是個layer CCSprite *spRocker2=CCSprite::spriteWithFile("CloseSelected.png");//搖桿 CCSprite *spRockerBG2=CCSprite::spriteWithFile("rockerBg.png");//搖桿背景 HRocker* rocker2=HRocker::HRockerWithCenter(ccp(210.0f,130.0f),50.0f ,spRocker2 ,spRockerBG2,true);//創建搖桿 this->addChild(rocker2);//搖桿添加到layer中 ~~~ 截圖如下: [![](https://box.kancloud.cn/2016-03-31_56fcd023766ca.png)](http://www.himigame.com/wp-content/uploads/2012/03/31123.png) 更多的自定義大家可以自行嘗試,Himi就不添加了,畢竟每款游戲都有不同需求~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看