cocos2d-x模拟摇杆

现在很多手机APP都有模拟操控摇杆的功能,今天项目中接触到一个委托事件,我顺便试试实现这个摇杆功能。

代码如下

[cpp]  view plain copy
  1. /* 
  2.  *  Joystick.h 
  3.  *  YaoGan 
  4.  * 
  5.  *  Created by Liu Yanghui on 11-10-27. 
  6.  *  Copyright 2011 ard8. All rights reserved. 
  7.  * 
  8.  */  
  9.   
  10. #ifndef Joystick_H  
  11. #define Joystick_H  
  12. #include "cocos2d.h"  
  13. using namespace cocos2d;  
  14. class Joystick :public CCLayer {  
  15.     public :  
  16.     CCPoint centerPoint;//摇杆中心  
  17.     CCPoint currentPoint;//摇杆当前位置  
  18.     bool active;//是否激活摇杆  
  19.     float radius;//摇杆半径  
  20.     CCSprite *jsSprite;  
  21.       
  22.       
  23.     void Active();  
  24.     void Inactive();  
  25.     CCPoint getDirection();  
  26.     float getVelocity();  
  27.     void  updatePos(ccTime dt);  
  28.       
  29.     //初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景  
  30.     static Joystick*  JoystickWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg);  
  31.     Joystick * initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg);  
  32.       
  33.     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  34.     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);  
  35.     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  
  36.     LAYER_NODE_FUNC(Joystick);  
  37. };  
  38. #endif  


 

[cpp]  view plain copy
  1. /* 
  2.  *  Joystick.cpp 
  3.  *  YaoGan 
  4.  * 
  5.  *  Created by Liu Yanghui on 11-10-27. 
  6.  *  Copyright 2011 ard8. All rights reserved. 
  7.  * 
  8.  */  
  9.   
  10. #include "Joystick.h"  
  11.   
  12. void Joystick::updatePos(ccTime dt){  
  13.     jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint, jsSprite->getPosition()),0.5)));  
  14. }  
  15.   
  16. void Joystick::Active()  
  17. {  
  18.     if (!active) {  
  19.         active=true;  
  20.         schedule(schedule_selector(Joystick::updatePos));//添加刷新函数  
  21.         CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0,false);//添加触摸委托  
  22.     }else {  
  23.           
  24.     }  
  25. }  
  26. //冻结摇杆  
  27. void   Joystick::Inactive()  
  28. {  
  29.     if (active) {  
  30.         active=false;  
  31.         this->unschedule(schedule_selector(Joystick::updatePos));//删除刷新  
  32.         CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);//删除委托  
  33.     }else {  
  34.           
  35.     }  
  36. }  
  37.   
  38. bool Joystick::ccTouchBegan(CCTouch* touch, CCEvent* event)  
  39. {  
  40.     if (!active)  
  41.         return false;  
  42.     CCPoint touchPoint = touch->locationInView(touch->view());  
  43.     touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);  
  44.     if (ccpDistance(touchPoint, centerPoint) > radius)  
  45.         return false;  
  46.     currentPoint = touchPoint;  
  47.     return true;  
  48. }  
  49.   
  50. void  Joystick::ccTouchMoved(CCTouch* touch, CCEvent* event)  
  51. {  
  52.     CCPoint touchPoint = touch->locationInView(touch->view());  
  53.     touchPoint = CCDirector:: sharedDirector()->convertToGL(touchPoint);  
  54.     if (ccpDistance(touchPoint, centerPoint) > radius)  
  55.     {  
  56.         currentPoint =ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));  
  57.     }else {  
  58.         currentPoint = touchPoint;  
  59.     }  
  60. }  
  61.   
  62. void  Joystick::ccTouchEnded(CCTouch* touch, CCEvent* event)  
  63. {  
  64.       
  65.     currentPoint = centerPoint;  
  66. }  
  67. //获取摇杆方位,注意是单位向量  
  68. CCPoint Joystick::getDirection()  
  69. {  
  70.     return ccpNormalize(ccpSub(centerPoint, currentPoint));  
  71. }  
  72. //获取摇杆力度  
  73. float Joystick::getVelocity()  
  74. {  
  75.     return ccpDistance(centerPoint, currentPoint);  
  76. }  
  77.   
  78. Joystick* Joystick:: JoystickWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg){  
  79.     Joystick *jstick=Joystick::node();  
  80.     jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg);  
  81.     return jstick;  
  82. }  
  83.   
  84. Joystick* Joystick::initWithCenter(CCPoint aPoint ,float aRadius ,CCSprite* aJsSprite,CCSprite* aJsBg){  
  85.       
  86.       
  87.     active = false;  
  88.     radius = aRadius;  
  89.     centerPoint = aPoint;  
  90.     currentPoint = centerPoint;  
  91.     jsSprite = aJsSprite;  
  92.     jsSprite->setPosition(centerPoint);  
  93.     aJsBg->setPosition(centerPoint);  
  94.     this->addChild(jsSprite);  
  95.     this->addChild(aJsBg);  
  96.     return this;  
  97. }  


好了,基本都实现了摇杆的功能,最后我来调用一下,看看结果如何。随便创建一个场景

初始化时加入我要得资源文件。

[cpp]  view plain copy
  1. CCSize screenSize=CCDirector::sharedDirector()->getWinSize();  
  2.     CCSprite *mainBg=CCSprite::spriteWithFile("bg.png");  
  3.     mainBg->setPosition(ccp(screenSize.width*0.5,screenSize.height*0.5));  
  4.     this->addChild(mainBg);  
  5.       
  6.     CCSprite *testPointL=CCSprite::spriteWithFile("point.png");//摇杆  
  7.     CCSprite *testBGL=CCSprite::spriteWithFile("joytickbg.png");//摇杆背景  
  8.     Joystick *testJSL=Joystick::JoystickWithCenter(ccp(80.0f,80.0f),60.0f ,testPointL ,testBGL);  
  9.     this->addChild(testJSL);  
  10.     testJSL->Active();  
  11.       
  12.     return true;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值