cocos2dx-3.10虚拟摇杆的实现

前端之家收集整理的这篇文章主要介绍了cocos2dx-3.10虚拟摇杆的实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
本篇文章是在cocos2dx-2.x的基础上改编而来,基本没有什么大的改变,只是让技术小白少走一些弯路。好了。下面就直接上代码了。
首先把虚拟摇杆封装成一个类,就要建立一个.cpp文件和.文件,这个文件就命名为HRocker.h文件

其中HRocker.h的文件如下:

  1. #ifndef HRocker_H
  2. #define HRocker_H
  3. #include "cocos2d.h"
  4. using namespace cocos2d;
  5. class HRocker :public Layer {
  6. public:
  7. //初始化 aPoint是摇杆中心 aRadius是摇杆半径 aJsSprite是摇杆控制点 aJsBg是摇杆背景
  8. static HRocker* HRockerWithCenter(Vec2 point,float Radius,Sprite* aJsSprite,Sprite* aJsBg,bool _isFollowRole);
  9. //启动摇杆
  10. void Active();
  11. //解除摇杆
  12. void Inactive();
  13. Vec2 getDirection();
  14. private:
  15. EventListenerTouchOneByOne* touchListener;
  16. HRocker * initWithCenter(Vec2 point,float aRadius,bool _isFollowRole);
  17. Vec2 centerPoint;//摇杆中心
  18. Vec2 currentPoint;//摇杆当前位置
  19. bool active;//是否激活摇杆
  20. float radius;//摇杆半径
  21. Sprite *jsSprite;
  22. bool isFollowRole;//是否跟随用户点击
  23. float getVelocity();
  24. void updatePos(float dt);
  25. virtual bool onTouchBegan(Touch *pTouch,Event *pEvent);
  26. virtual void onTouchMoved(Touch *pTouch,Event *pEvent);
  27. virtual void onTouchEnded(Touch *pTouch,Event *pEvent);
  28. CREATE_FUNC(HRocker);
  29. };
  30. #endif

其下是HRockre.cpp中的内容
  1. #include "HRocker.h"
  2. using namespace cocos2d;
  3. //定义一个计时器,随时检测鼠标点击的位置
  4. void HRocker::updatePos(float dt){
  5. jsSprite->setPosition(ccpAdd(jsSprite->getPosition(),ccpMult(ccpSub(currentPoint,jsSprite->getPosition()),0.5)));
  6. }
  7. //启动摇杆
  8. void HRocker::Active()
  9. {
  10. if (!active) {
  11. active = true;
  12. schedule(schedule_selector(HRocker::updatePos));//添加刷新函数
  13. //CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,false);
  14. touchListener = EventListenerTouchOneByOne::create();
  15. touchListener->setSwallowTouches(true);
  16. touchListener->onTouchBegan = CC_CALLBACK_2(HRocker::onTouchBegan,this);
  17. touchListener->onTouchMoved = CC_CALLBACK_2(HRocker::onTouchMoved,this);
  18. touchListener->onTouchEnded = CC_CALLBACK_2(HRocker::onTouchEnded,this);
  19. // 注册事件监听机制
  20. _eventDispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this);
  21. }
  22. else {
  23. }
  24. }
  25. //解除摇杆
  26. void HRocker::Inactive()
  27. {
  28. if (active) {
  29. active = false;
  30. this->unschedule(schedule_selector(HRocker::updatePos));//删除刷新
  31. _eventDispatcher->removeEventListener(touchListener);//删除委托
  32. }
  33. else {
  34. }
  35. }
  36. //摇杆方位
  37. Vec2 HRocker::getDirection()
  38. {
  39. return ccpNormalize(ccpSub(centerPoint,currentPoint));
  40. }
  41. //摇杆力度
  42. float HRocker::getVelocity()
  43. {
  44. return ccpDistance(centerPoint,currentPoint);
  45. }
  46. HRocker* HRocker::HRockerWithCenter(Vec2 point,bool _isFollowRole){
  47. HRocker *jstick = HRocker::create();
  48. jstick->initWithCenter(point,aRadius,aJsSprite,aJsBg,_isFollowRole);
  49. return jstick;
  50. }
  51. bool HRocker::onTouchBegan(Touch* touch,Event* event)
  52. {
  53. if (!active)
  54. return false;
  55. this->setVisible(true);
  56. Vec2 touchPoint = touch->getLocationInView();
  57. touchPoint = Director::sharedDirector()->convertToGL(touchPoint);
  58. if (!isFollowRole){
  59. if (ccpDistance(touchPoint,centerPoint) > radius){
  60. return false;
  61. }
  62. }
  63. currentPoint = touchPoint;
  64. if (isFollowRole){
  65. centerPoint = currentPoint;
  66. jsSprite->setPosition(currentPoint);
  67. this->getChildByTag(88)->setPosition(currentPoint);
  68. }
  69. return true;
  70. }
  71. void HRocker::onTouchMoved(Touch* touch,Event* event)
  72. {
  73. Vec2 touchPoint = touch->getLocationInView();
  74. touchPoint = Director::sharedDirector()->convertToGL(touchPoint);
  75. if (ccpDistance(touchPoint,centerPoint) > radius)
  76. {
  77. currentPoint = ccpAdd(centerPoint,ccpMult(ccpNormalize(ccpSub(touchPoint,centerPoint)),radius));
  78. }
  79. else {
  80. currentPoint = touchPoint;
  81. }
  82. }
  83. void HRocker::onTouchEnded(Touch* touch,Event* event)
  84. {
  85. currentPoint = centerPoint;
  86. if (isFollowRole){
  87. this->setVisible(false);
  88. }
  89. }
  90. HRocker* HRocker::initWithCenter(Vec2 aPoint,bool _isFollowRole){
  91. isFollowRole = _isFollowRole;
  92. active = false;
  93. radius = aRadius;
  94. if (!_isFollowRole){
  95. centerPoint = aPoint;
  96. }
  97. else{
  98. centerPoint = ccp(0,0);
  99. }
  100. currentPoint = centerPoint;
  101. jsSprite = aJsSprite;
  102. jsSprite->setPosition(centerPoint);
  103. aJsBg->setPosition(centerPoint);
  104. aJsBg->setTag(88);
  105. this->addChild(aJsBg);
  106. this->addChild(jsSprite);
  107. if (isFollowRole){
  108. this->setVisible(false);
  109. }
  110. this->Active();//激活摇杆
  111. return this;
  112. }
好了,到这里一个虚拟摇杆的的类就完成了,接下来就是虚拟摇杆的精灵了 其中要看你把这个摇杆精灵放在哪个。cpp的文件当中了,我这个只是测试的例子,所以就放在 helloword.cpp文件当中。在init函数当中添加如下的代码就可以运行了。 其中要加上#include "HRocker.h"这个有文件 Sprite *spRocker2 = Sprite::create("rockerBar.png");//摇杆 Sprite *spRockerBG2 = Sprite::create("rocker.png");//摇杆背景 HRocker* rocker2 = HRocker::HRockerWithCenter(Vec2(210.0f,130.0f),50.0f,spRocker2,spRockerBG2,true);//创建摇杆 this->addChild(rocker2);//摇杆添加到layer中 这就是我通过cocos2dx-2.x修改过来的了,

猜你在找的Cocos2d-x相关文章