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的代码:

[cpp]   view plain copy
  1. float buttonRadius=50;        
  2.   
  3. buttonA=new SneakyButton();  
  4. buttonA->autorelease();  
  5. buttonA->initWithRect(CCRectZero);  
  6. buttonA->setIsToggleable(false);  
  7. buttonA->setIsHoldable(true);          
  8.   
  9. SneakyButtonSkinnedBase *buttonASkin=new SneakyButtonSkinnedBase();  
  10. buttonASkin->autorelease();  
  11. buttonASkin->init();  
  12. buttonASkin->setPosition(ccp(size.width-buttonRadius,buttonRadius));  
  13. buttonASkin->setDefaultSprite(CCSprite::spriteWithFile("button-default.png"));  
  14. // buttonASkin->setDisabledSprite(CCSprite::spriteWithFile("button-disabled.png"));  
  15. buttonASkin->setPressSprite(CCSprite::spriteWithFile("button-pressed.png"));  
  16. buttonASkin->setActivatedSprite(CCSprite::spriteWithFile("button-activated.png"));  
  17. buttonASkin->setButton(buttonA);  
  18.   
  19. this->addChild(buttonASkin);  
使用jostick的代码:
[cpp]   view plain copy
  1. float joystickRadius=50;  
  2.   
  3. joystick=new SneakyJoystick();  
  4. joystick->autorelease();  
  5. joystick->initWithRect(CCRectZero);  
  6. joystick->setAutoCenter(true);  
  7. joystick->setHasDeadzone(true);  
  8. joystick->setDeadRadius(10);  
  9.   
  10. SneakyJoystickSkinnedBase *joystickSkin=new SneakyJoystickSkinnedBase();  
  11. joystickSkin->autorelease();  
  12. joystickSkin->init();  
  13. joystickSkin->setBackgroundSprite(CCSprite::spriteWithFile("button-disabled.png"));  
  14. joystickSkin->setThumbSprite(CCSprite::spriteWithFile("button-disabled.png"));  
  15. joystickSkin->getThumbSprite()->setScale(0.5f);  
  16. joystickSkin->setPosition(ccp(joystickRadius,joystickRadius));  
  17. joystickSkin->setJoystick(joystick);  
  18.   
  19. this->addChild(joystickSkin);  

然后在update函数中获取按钮状态:
[cpp]   view plain copy
  1. #define FIRE_INTERVAL 0.3f  
  2. float HelloWorld::fireTime=0;  
  3. void HelloWorld::update(ccTime dt)  
  4. {  
  5.     CCPoint velocity=joystick->getVelocity();  
  6.     if(velocity.x!=0||velocity.y!=0)  
  7.     {  
  8.         CCLOG("joystick:[%f,%f]",velocity.x,velocity.y);  
  9.     }  
  10.   
  11.     fireTime+=dt;  
  12.       
  13.     if(buttonA->getIsActive()&&fireTime>=FIRE_INTERVAL)  
  14.     {  
  15.         CCLOG("buttonA pressed.");  
  16.         fireTime=0;  
  17.     }  
  18. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值