首先把虚拟摇杆封装成一个类,就要建立一个.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中的内容
好了,到这里一个虚拟摇杆的的类就完成了,接下来就是虚拟摇杆的精灵了 其中要看你把这个摇杆精灵放在哪个。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修改过来的了,
- #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;
- }