cocos2d-x的初步学习十二之虚拟摇杆Joystick

来源:互联网 发布:关于字体的软件 编辑:程序博客网 时间:2024/05/22 03:37

分类: cocos2dx学习 963人阅读 评论(2)收藏 举报
Cocos2d-x跨平台摇杆Joystick游戏

这篇文章中,我们讲下虚拟摇杆,一般游戏中都会有虚拟摇杆,看了下别人写的Joystick,发现版本都是比较老的了,一些api已经改变了,稍微修改了下。OK,上代码:

摇杆类头文件:

[cpp] view plaincopy
  1. #include "cocos2d.h"  
  2.   
  3. using namespace cocos2d;  
  4.   
  5. class HRocker :public CCLayer {  
  6.     public :  
  7.     //初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景  
  8.     static HRocker*  HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole);  
  9.     //启动摇杆  
  10.     void Active();  
  11.     //解除摇杆  
  12.     void Inactive();  
  13.     CCPoint getDirection();  
  14.   
  15. private:  
  16.     HRocker * initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole);  
  17.     CCPoint centerPoint;//摇杆中心  
  18.     CCPoint currentPoint;//摇杆当前位置  
  19.     bool active;//是否激活摇杆  
  20.     float radius;//摇杆半径  
  21.     CCSprite *jsSprite;  
  22.     bool isFollowRole;//是否跟随用户点击  
  23.     float getVelocity();  
  24.     void  updatePos(CCTime dt);  
  25.     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  26.     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);  
  27.     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  
  28.       
  29.     CREATE_FUNC(HRocker);  
  30. };  

.cpp:

[cpp] view plaincopy
  1. #include "Joystick.h"  
  2.   
  3. using namespace cocos2d;  
  4.   
  5.   
  6. void HRocker::updatePos(CCTime dt){  
  7.     jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint, jsSprite->getPosition()),0.5)));  
  8. }  
  9. //启动摇杆  
  10. void HRocker::Active()  
  11. {  
  12.     if (!active) {  
  13.         active=true;  
  14.         schedule(schedule_selector(HRocker::updatePos));//添加刷新函数  
  15.         CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0,false);  
  16.     }else {  
  17.     }  
  18. }  
  19. //解除摇杆  
  20. void   HRocker::Inactive()  
  21. {  
  22.     if (active) {  
  23.         active=false;  
  24.         this->unschedule(schedule_selector(HRocker::updatePos));//删除刷新  
  25.          CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);//删除委托  
  26.     }else {  
  27.     }  
  28. }  
  29. //摇杆方位  
  30. CCPoint HRocker::getDirection()  
  31. {  
  32.       
  33.     return ccpNormalize(ccpSub(centerPoint, currentPoint));  
  34. }  
  35. //摇杆力度  
  36. float HRocker::getVelocity()  
  37. {  
  38.     return ccpDistance(centerPoint, currentPoint);  
  39. }  
  40. HRocker* HRocker:: HRockerWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){  
  41.     HRocker *jstick=HRocker::create();  
  42.     jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg,_isFollowRole);  
  43.     return jstick;  
  44. }  
  45. bool HRocker::ccTouchBegan(CCTouch* touch, CCEvent* event)  
  46. {  
  47.     if (!active)  
  48.         return false;  
  49.     this->setVisible(true);  
  50.     CCPoint touchPoint = touch->getLocationInView();  
  51.     touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);  
  52.     if(!isFollowRole){  
  53.         if (ccpDistance(touchPoint, centerPoint) > radius){  
  54.             return false;  
  55.         }  
  56.     }  
  57.     currentPoint = touchPoint;  
  58.     if(isFollowRole){  
  59.         centerPoint=currentPoint;  
  60.         jsSprite->setPosition(currentPoint);  
  61.         this->getChildByTag(88)->setPosition(currentPoint);  
  62.     }  
  63.     return true;  
  64. }  
  65. void  HRocker::ccTouchMoved(CCTouch* touch, CCEvent* event)  
  66. {  
  67.     CCPoint touchPoint = touch->getLocationInView();  
  68.     touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);  
  69.     if (ccpDistance(touchPoint, centerPoint) > radius)  
  70.     {  
  71.         currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));  
  72.     }else {  
  73.         currentPoint = touchPoint;  
  74.     }  
  75. }  
  76. void  HRocker::ccTouchEnded(CCTouch* touch, CCEvent* event)  
  77. {  
  78.     currentPoint = centerPoint;  
  79.     if(isFollowRole){  
  80.         this->setVisible(false);  
  81.     }  
  82. }  
  83. HRocker* HRocker::initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg,bool _isFollowRole){  
  84.     isFollowRole =_isFollowRole;  
  85.     active = false;  
  86.     radius = aRadius;  
  87.     if(!_isFollowRole){  
  88.         centerPoint =aPoint;  
  89.     }else{  
  90.         centerPoint =ccp(0,0);  
  91.     }  
  92.     currentPoint = centerPoint;  
  93.     jsSprite = aJsSprite;  
  94.     jsSprite->setPosition(centerPoint);  
  95.     aJsBg->setPosition(centerPoint);  
  96.     aJsBg->setTag(88);  
  97.     this->addChild(aJsBg);  
  98.     this->addChild(jsSprite);  
  99.     if(isFollowRole){  
  100.         this->setVisible(false);  
  101.     }  
  102.     this->Active();//激活摇杆  
  103.     return this;  
  104. }  

OK,我们看下实现加载这个类:
[cpp] view plaincopy
  1. //摇杆  
  2.    CCSprite *controlSprite=CCSprite::create("cen.png");  
  3.    //摇杆背景  
  4.    CCSprite *bgSprite=CCSprite::create("control_bg.png");  
  5.      
  6.      
  7.    joystick=HRocker::HRockerWithCenter(ccp(200.0f,200.0f),60.0f ,controlSprite ,bgSprite,false);  
  8.   
  9.    this->addChild(joystick,1);  
  10.      
  11.     
  12.    this->scheduleUpdate();  

发现里面有个getDirection()方法,可以作为判断方向,大家可以试试,我们看下效果图:




0 0