cocos2d-x 之虚拟摇杆实现
其实要实现这个功能并不难,直接把以前在java上实现的逻辑照搬过来就是了。。。直接看下文吧:
首先,把摇杆当成一个独立的类,具有自身的一系列功能,代码中只列举一部分最常用的功能点:
Joystick.h:
#include "cocos2d.h"
using namespace cocos2d;
class Joystick :
public CCLayer
{
public:
Joystick(void);
~Joystick(void);
public:
CCPoint centerPoint; // 摇杆中心
CCPoint currentPoint; // 摇杆当前位置
bool active; // 是否激活摇杆
float radius; // 摇杆半径
CCSprite *jsSprite; // 摇杆实例
//************************************
// Method: Active
// FullName: Joystick::Active
// Access: public
// Returns: void
// Qualifier: 设置摇杆功能可用
//************************************
void Active();
//************************************
// Method: Inactive
// FullName: Joystick::Inactive
// Access: public
// Returns: void
// Qualifier: 设置摇杆功能不可用
//************************************
void Inactive();
//************************************
// Method: getDirection
// FullName: Joystick::getDirection
// Access: public
// Returns: cocos2d::CCPoint
// Qualifier: 获取摇杆方向,这里返回的是单位向量
//************************************
CCPoint getDirection();
//************************************
// Method: getVelocity
// FullName: Joystick::getVelocity
// Access: public
// Returns: float
// Qualifier: 获取摇杆的力度
//************************************
float getVelocity();
//************************************
// Method: updatePos
// FullName: Joystick::updatePos
// Access: public
// Returns: void
// Qualifier: 刷新函数,交给日程管理器
// Parameter: ccTime dt
//************************************
void updatePos(ccTime dt);
//************************************
// Method: JoystickWithCenter
// FullName: Joystick::JoystickWithCenter
// Access: public static
// Returns: Joystick*
// Qualifier: 初始化摇杆
// Parameter: CCPoint aPoint 摇杆中心
// Parameter: float aRadius 摇杆半径
// Parameter: CCSprite * aJsSprite 摇杆控制点
// Parameter: CCSprite * aJsBg 摇杆背景
//************************************
static Joystick* JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
Joystick* initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
// 以下是复写Touch响应函数
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
LAYER_NODE_FUNC(Joystick);
};
Joystick.cpp
#include "Joystick.h"
Joystick::Joystick(void)
{
}
Joystick::~Joystick(void)
{
}
void Joystick::updatePos(ccTime dt)
{
jsSprite->setPosition(ccpAdd(jsSprite->getPosition(), ccpMult(ccpSub(currentPoint, jsSprite->getPosition()), 0.5)));
}
void Joystick::Active()
{
if(!active)
{
active = true;
schedule(schedule_selector(Joystick::updatePos)); // 添加刷新函数
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false); // 添加触摸委托
}
}
void Joystick::Inactive()
{
if(active)
{
active = false;
this->unschedule(schedule_selector(Joystick::updatePos)); // 删除刷新函数
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); // 删除委托
}
}
bool Joystick::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if(!active)
return false;
CCPoint touchPoint = pTouch->locationInView(pTouch->view());
touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
if(ccpDistance(touchPoint, centerPoint) > radius)
return false;
currentPoint = touchPoint;
return true;
}
void Joystick::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint touchPoint = pTouch->locationInView(pTouch->view());
touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
if(ccpDistance(touchPoint, centerPoint) > radius)
{
currentPoint = ccpAdd(centerPoint, ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));
}
else
{
currentPoint = touchPoint;
}
}
void Joystick::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
currentPoint = centerPoint;
}
CCPoint Joystick::getDirection()
{
return ccpNormalize(ccpSub(centerPoint, currentPoint));
}
float Joystick::getVelocity()
{
return ccpDistance(centerPoint, currentPoint);
}
Joystick* Joystick:: JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg)
{
Joystick *jstick=Joystick::node();
jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg);
return jstick;
}
Joystick* Joystick::initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg)
{
active = false;
radius = aRadius;
centerPoint = aPoint;
currentPoint = centerPoint;
jsSprite = aJsSprite;
jsSprite->setPosition(centerPoint);
aJsBg->setPosition(centerPoint);
this->addChild(jsSprite);
this->addChild(aJsBg);
return this;
}
完成了摇杆类后,直接在HelloWorld中测试效果吧,在init函数中加上下面的代码即可:
CCSprite *testPointL = CCSprite::spriteWithFile("point.png");
CCSprite *testBGL = CCSprite::spriteWithFile("joystickbg.png");
Joystick *testJSL = Joystick::JoystickWithCenter(ccp(80.0f, 80.0f), 60.0f, testPointL, testBGL);
this->addChild(testJSL);
testJSL->Active();
看效果图:
资源文件:


浙公网安备 33010602011771号