How to use this joystick?

How to use this joystick?
0
#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.

Hi thanks for your welcome, so recently i’ve ported SneakyInput for work properly on cocos2d-x 3.9 on coccos studio based projects too i want to share this https://github.com/UlisesFreitas/SneakyInput-for-cocos2d-x-v3.9-Cocos-Studio-projects.git

SneakyInput-for-cocos2d-x-v3.9-Cocos-Studio-projects

SneakyJoystick

SneakyJoystick is a library that provides a ‘joystick-like’ features to a layer in Cocos2d-x. The library support:

  • Joystick Thumb
  • Joystick D-Pad
  • Regular Buttons
  • Holdable Buttons
  • Toggleable Buttons

The project was originally created CJ Hanson (https://github.com/cjhanson). This is a port for Cocos2d-x version 2.2.1 and 3.0rc0, and now 3.9 cocos studio projects

How to Use the Library

The best way to use the library is to create a JoystickLayer, and then add that layer to your Game Scene.

This is an example on how to create a Joystick Thumb

How to implement on my game:

In case the HelloWorld i.e

On HelloWorld.h

Add

#include "SneakyButton.h"
#include "SneakyButtonSkinnedBase.h"
#include "SneakyJoystickSkinnedBase.h"

Below
public:

SneakyJoystick *leftJoystick;
SneakyButton *jumpBtn;

HelloWorld.cpp
Add

Rect joystickBaseDimensions;
joystickBaseDimensions = Rect(0, 0, 160.0f, 160.0f);

Point joystickBasePosition;
joystickBasePosition = Vec2(visibleSize.width * 0.2f, visibleSize.height*0.2f);

SneakyJoystickSkinnedBase *joystickBase = new SneakyJoystickSkinnedBase();
joystickBase->init();
joystickBase->setPosition(joystickBasePosition);
joystickBase->setBackgroundSprite(Sprite::create("res/joystick-back.png"));
joystickBase->setThumbSprite(Sprite::create("res/stick.png"));

SneakyJoystick *aJoystick = new SneakyJoystick();
aJoystick->initWithRect(joystickBaseDimensions);

aJoystick->autorelease();
joystickBase->setJoystick(aJoystick);
joystickBase->setPosition(joystickBasePosition);

leftJoystick = joystickBase->getJoystick();
leftJoystick->retain();
this->addChild(joystickBase);

And for button

Rect jumpButtonDimensions = Rect(0, 0, 64.0f, 64.0f);
Point jumpButtonPosition;
jumpButtonPosition = Vec2(visibleSize.width * 0.9f, visibleSize.height * 0.2f);

SneakyButtonSkinnedBase *jumpButtonBase = new SneakyButtonSkinnedBase();
jumpButtonBase->init();
jumpButtonBase->setPosition(jumpButtonPosition);

jumpButtonBase->setDefaultSprite(Sprite::create("res/btn-attack.png"));
jumpButtonBase->setActivatedSprite(Sprite::create("res/btn-attack-pressed.png"));
jumpButtonBase->setDisabledSprite(Sprite::create("res/btn-attack-pressed.png"));
jumpButtonBase->setPressSprite(Sprite::create("res/btn-attack-pressed.png"));

SneakyButton *ajumpButton = new SneakyButton();
ajumpButton->initWithRect(jumpButtonDimensions);
ajumpButton->autorelease();

jumpButtonBase->setButton(ajumpButton);
jumpButtonBase->setPosition(jumpButtonPosition);

jumpBtn = jumpButtonBase->getSbutton();
jumpBtn->retain();
this->addChild(jumpButtonBase);

And finally on update example:

void HelloWorldScene::update(float dt){

    this->setViewPointCenter(_everboy->getPosition());

    if(leftJoystick->getVelocity().x > 0){
        everboyBody->setVelocity( Vect( 200, 0 ) );
    }
    if(leftJoystick->getVelocity().x < 0){
        everboyBody->setVelocity( Vect( -200, 0 ) );
    }
    if(leftJoystick->getVelocity().x == 0 ){
        everboyBody->setVelocity( Vect( 0, everboyBody->getWorld()->getGravity().y ) );
    }
    if(jumpBtn->getValue()){
        everboyBody->applyImpulse(Vec2(0, 200));
        everboyBody->setVelocity(Vec2(0,100));
    }
}

Done now you have your dPad or Joystick and your buttons.

1 Like