歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> SneakInput在cocos2d-x下的示例

SneakInput在cocos2d-x下的示例

日期:2017/3/1 10:13:18   编辑:Linux編程

看了很多教程和文檔,無論2d還是2d-x都推薦使用開源的SneakInput作為其觸屏的手柄組件。

因此我也下載了它的源碼並將其融合到自己的游戲裡,整個演示的源碼下載地址為:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/8月/11日/SneakInput在cocos2d-x下的示例/

我的環境為vs2010 + cocos2d-1.0.1-x-0.12.0

另外SneakInput c++的源碼下載地址為:https://github.com/Ntran013/SneakyInput

經過自己的試驗,發現在我的環境下並不需要修改SneakInput的源碼,將源碼解壓後,放在自己的項目裡就可以正常使用。

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

使用button的代碼:

  1. float buttonRadius=50;
  2. buttonA=new SneakyButton();
  3. buttonA->autorelease();
  4. buttonA->initWithRect(CCRectZero);
  5. buttonA->setIsToggleable(false);
  6. buttonA->setIsHoldable(true);
  7. SneakyButtonSkinnedBase *buttonASkin=new SneakyButtonSkinnedBase();
  8. buttonASkin->autorelease();
  9. buttonASkin->init();
  10. buttonASkin->setPosition(ccp(size.width-buttonRadius,buttonRadius));
  11. buttonASkin->setDefaultSprite(CCSprite::spriteWithFile("button-default.png"));
  12. // buttonASkin->setDisabledSprite(CCSprite::spriteWithFile("button-disabled.png"));
  13. buttonASkin->setPressSprite(CCSprite::spriteWithFile("button-pressed.png"));
  14. buttonASkin->setActivatedSprite(CCSprite::spriteWithFile("button-activated.png"));
  15. buttonASkin->setButton(buttonA);
  16. this->addChild(buttonASkin);

使用jostick的代碼:

  1. float joystickRadius=50;
  2. joystick=new SneakyJoystick();
  3. joystick->autorelease();
  4. joystick->initWithRect(CCRectZero);
  5. joystick->setAutoCenter(true);
  6. joystick->setHasDeadzone(true);
  7. joystick->setDeadRadius(10);
  8. SneakyJoystickSkinnedBase *joystickSkin=new SneakyJoystickSkinnedBase();
  9. joystickSkin->autorelease();
  10. joystickSkin->init();
  11. joystickSkin->setBackgroundSprite(CCSprite::spriteWithFile("button-disabled.png"));
  12. joystickSkin->setThumbSprite(CCSprite::spriteWithFile("button-disabled.png"));
  13. joystickSkin->getThumbSprite()->setScale(0.5f);
  14. joystickSkin->setPosition(ccp(joystickRadius,joystickRadius));
  15. joystickSkin->setJoystick(joystick);
  16. this->addChild(joystickSkin);
然後在update函數中獲取按鈕狀態:
  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. fireTime+=dt;
  11. if(buttonA->getIsActive()&&fireTime>=FIRE_INTERVAL)
  12. {
  13. CCLOG("buttonA pressed.");
  14. fireTime=0;
  15. }
  16. }
Copyright © Linux教程網 All Rights Reserved