Cocos2d-x的SneakyInput

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();


定义SneakyInput的按钮和获取按钮的数据:

定义按钮:

		//按钮按钮皮肤
		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);
}
下载地址: 点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值