SneakyInput是虚拟摇杆库。
SneakyInput是2dx里面的虚拟手柄。但是从官网下载下来2dx的版本太旧,要对他进行修改,适用于cocos2d-x 2.1.4版本。理论上适用所有的2dx的2.1+版本。
下载地址:点击打开链接
废话少说,直接进入正题:
声明:
#include "SneakyInput/SneakyJoystickSkinnedBase.h"
#include "SneakyInput/SneakyButtonSkinnedBase.h"
SneakyButton *button;
SneakyJoystick *joystick;
定义SneakyInput的摇杆和获取摇杆的数据:
定义:
SneakyJoystickSkinnedBase *joystickBase = new SneakyJoystickSkinnedBase();
joystickBase->autorelease();
joystickBase->init();
joystickBase->setBackgroundSprite(CCSprite::create("b.png")); // 底座
joystickBase->setThumbSprite(CCSprite::create("f.png")); // 操控杆
joystickBase->setPosition(CCPointMake(48, 48));
joystick = new SneakyJoystick();
joystick->autorelease();
joystick->initWithRect(CCRectMake(0,0,64,64));
joystickBase->setJoystick(joystick);
//是否自动回到中心
joystick->setAutoCenter(true);
//是否支持死亡区域,该区域不会触发
joystick->setHasDeadzone(true);
//死亡区域半径
joystick->setDeadRadius(10);
this->addChild(joystickBase);
获取数据:
joystick->getVelocity();
定义按钮:
//按钮按钮皮肤
SneakyButtonSkinnedBase *buttonBase = new SneakyButtonSkinnedBase();
buttonBase->autorelease();
buttonBase->init();
buttonBase->setDefaultSprite(CCSprite::create("default.png"));
buttonBase->setActivatedSprite(CCSprite::create("activated.png"));
buttonBase->setPressSprite(CCSprite::create("press.png"));
buttonBase->setPosition(CCPointMake(480-48, 48));
//按钮
button = new SneakyButton();
button->autorelease();
button->initWithRect(CCRectMake(0,0,32,32));
button->setIsToggleable(false);
//设置在按下时,是否保持按下状态
button->setIsHoldable(true);
buttonBase->setButton(button);
this->addChild(buttonBase);
获取数据:
button->getIsActive();//按钮是否按下
一般获取数据的地方都是在update(float dt) 里面获取。
void HelloWorld::update(float dt)
{
if (button->getIsActive())
{
CCLog("fire!");
}
CCPoint scaledV = ccpMult(joystick->getVelocity(), 1);
float x = scaledV.x;
float y = scaledV.y;
CCLog("%f---%f",x,y);
}
下载地址:
点击打开链接