SneakInput在cocos2d-x下的示例

看了很多教程和文档,无论2d还是2d-x都推荐使用开源的SneakInput作为其触屏的手柄组件。

因此我也下载了它的源码并将其融合到自己的游戏里,

整个演示的源码下载地址为:

http://download.csdn.net/detail/weyson/4469089

我的环境为vs2010 + cocos2d-1.0.1-x-0.12.0

另外SneakInput c++的源码下载地址为:

https://github.com/Ntran013/SneakyInput

经过自己的试验,发现在我的环境下并不需要修改SneakInput的源码,将源码解压后,放在自己的项目里就可以正常使用。

SneakInput主要由2部分组成joystick和button。

使用button的代码:

float buttonRadius=50;		

buttonA=new SneakyButton();
buttonA->autorelease();
buttonA->initWithRect(CCRectZero);
buttonA->setIsToggleable(false);
buttonA->setIsHoldable(true);		

SneakyButtonSkinnedBase *buttonASkin=new SneakyButtonSkinnedBase();
buttonASkin->autorelease();
buttonASkin->init();
buttonASkin->setPosition(ccp(size.width-buttonRadius,buttonRadius));
buttonASkin->setDefaultSprite(CCSprite::spriteWithFile("button-default.png"));
// buttonASkin->setDisabledSprite(CCSprite::spriteWithFile("button-disabled.png"));
buttonASkin->setPressSprite(CCSprite::spriteWithFile("button-pressed.png"));
buttonASkin->setActivatedSprite(CCSprite::spriteWithFile("button-activated.png"));
buttonASkin->setButton(buttonA);

this->addChild(buttonASkin);
使用jostick的代码:

float joystickRadius=50;

joystick=new SneakyJoystick();
joystick->autorelease();
joystick->initWithRect(CCRectZero);
joystick->setAutoCenter(true);
joystick->setHasDeadzone(true);
joystick->setDeadRadius(10);

SneakyJoystickSkinnedBase *joystickSkin=new SneakyJoystickSkinnedBase();
joystickSkin->autorelease();
joystickSkin->init();
joystickSkin->setBackgroundSprite(CCSprite::spriteWithFile("button-disabled.png"));
joystickSkin->setThumbSprite(CCSprite::spriteWithFile("button-disabled.png"));
joystickSkin->getThumbSprite()->setScale(0.5f);
joystickSkin->setPosition(ccp(joystickRadius,joystickRadius));
joystickSkin->setJoystick(joystick);

this->addChild(joystickSkin);

然后在update函数中获取按钮状态:

#define FIRE_INTERVAL 0.3f
float HelloWorld::fireTime=0;
void HelloWorld::update(ccTime dt)
{
	CCPoint velocity=joystick->getVelocity();
	if(velocity.x!=0||velocity.y!=0)
	{
		CCLOG("joystick:[%f,%f]",velocity.x,velocity.y);
	}

	fireTime+=dt;
	
	if(buttonA->getIsActive()&&fireTime>=FIRE_INTERVAL)
	{
		CCLOG("buttonA pressed.");
		fireTime=0;
	}
}




你可能感兴趣的:(游戏,velocity,文档,float,button,2010)