#include "cocos2d.h"
using namespace cocos2d;
class Joystick :
public CCLayer
{
public:
Joystick(void);
~Joystick(void);
public:
CCPoint centerPoint; // center
CCPoint currentPoint; // current position
bool active;
float radius;
CCSprite *jsSprite; // instance
void Active();
void Inactive();
CCPoint getDirection();
float getVelocity();
void updatePos(ccTime dt);
static Joystick* JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
Joystick* initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
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;
}
I don’t how to use this joystick to move my sprite.
I hope someone can give me an example. Thanks.