首先把虚拟摇杆封装成一个类,就要建立一个.cpp文件和.文件,这个文件就命名为HRocker.h文件。
其中HRocker.h的文件如下:
#ifndef HRocker_H#define HRocker_H#include "cocos2d.h"using namespace cocos2d;class HRocker :public Layer {public: //初始化 aPoint是摇杆中心 aradius是摇杆半径 aJssprite是摇杆控制点 aJsBg是摇杆背景 static HRocker* HRockerWithCenter(Vec2 point,float Radius,Sprite* aJssprite,Sprite* aJsBg,bool _isFollowRole); //启动摇杆 voID Active(); //解除摇杆 voID Inactive(); Vec2 getDirection();private: EventListenertouchOneByOne* touchListener; HRocker * initWithCenter(Vec2 point,float aradius,bool _isFollowRole); Vec2 centerPoint;//摇杆中心 Vec2 currentPoint;//摇杆当前位置 bool active;//是否激活摇杆 float radius;//摇杆半径 Sprite *jssprite; bool isFollowRole;//是否跟随用户点击 float getVeLocity(); voID updatePos(float dt); virtual bool ontouchBegan(touch *ptouch,Event *pEvent); virtual voID ontouchmoved(touch *ptouch,Event *pEvent); virtual voID ontouchended(touch *ptouch,Event *pEvent); CREATE_FUNC(HRocker);};#endif
其下是HRockre.cpp中的内容
#include "HRocker.h"using namespace cocos2d;//定义一个计时器,随时检测鼠标点击的位置voID HRocker::updatePos(float 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));//添加刷新函数 //CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,false); touchListener = EventListenertouchOneByOne::create(); touchListener->setSwallowtouches(true); touchListener->ontouchBegan = CC_CALLBACK_2(HRocker::ontouchBegan,this); touchListener->ontouchmoved = CC_CALLBACK_2(HRocker::ontouchmoved,this); touchListener->ontouchended = CC_CALLBACK_2(HRocker::ontouchended,this); // 注册事件监听机制 _eventdispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this); } else { }}//解除摇杆voID HRocker::Inactive(){ if (active) { active = false; this->unschedule(schedule_selector(HRocker::updatePos));//删除刷新 _eventdispatcher->removeEventListener(touchListener);//删除委托 } else { }}//摇杆方位Vec2 HRocker::getDirection(){ return ccpnormalize(ccpsub(centerPoint,currentPoint));}//摇杆力度float HRocker::getVeLocity(){ return ccpdistance(centerPoint,currentPoint);}HRocker* HRocker::HRockerWithCenter(Vec2 point,bool _isFollowRole){ HRocker *Jstick = HRocker::create(); Jstick->initWithCenter(point,aradius,aJssprite,aJsBg,_isFollowRole); return Jstick;}bool HRocker::ontouchBegan(touch* touch,Event* event){ if (!active) return false; this->setVisible(true); Vec2 touchPoint = touch->getLocationInVIEw(); touchPoint = Director::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::ontouchmoved(touch* touch,Event* event){ Vec2 touchPoint = touch->getLocationInVIEw(); touchPoint = Director::sharedDirector()->convertToGL(touchPoint); if (ccpdistance(touchPoint,centerPoint) > radius) { currentPoint = ccpAdd(centerPoint,ccpMult(ccpnormalize(ccpsub(touchPoint,centerPoint)),radius)); } else { currentPoint = touchPoint; }}voID HRocker::ontouchended(touch* touch,Event* event){ currentPoint = centerPoint; if (isFollowRole){ this->setVisible(false); }}HRocker* HRocker::initWithCenter(Vec2 aPoint,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->setVisible(false); } this->Active();//激活摇杆 return this;}
好了,到这里一个虚拟摇杆的的类就完成了,接下来就是虚拟摇杆的精灵了 其中要看你把这个摇杆精灵放在哪个。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修改过来的了, 总结 以上是内存溢出为你收集整理的cocos2dx-3.10虚拟摇杆的实现全部内容,希望文章能够帮你解决cocos2dx-3.10虚拟摇杆的实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)