这篇文章中,我们将用另一个虚拟摇杆类,SneakyInput,这个是开源的,已经有c++版,SneakInput c++的源码下载地址为:https://github.com/Ntran013/SneakyInput
注意,这个下载下来需要修改,因为那是基于老版本的cocos2dx的。已修改后的源码下载地址为:http://download.csdn.net/detail/my183100521/6620631
使用SneakyInput示例如下:
HelloWorldScene.h
void update(float t); cocos2d::CCSprite *iconSprite; SneakyJoystick * joystick;
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); // position the label on the center of the screen pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); // add the label as a child to this layer this->addChild(pLabel, 1); CCSize wsize = CCDirector::sharedDirector()->getVisibleSize(); iconSprite=CCSprite::create("Icon-72.png"); iconSprite->setPosition(ccp(wsize.width/2, wsize.height/2)); iconSprite->setTag(123); this->addChild(iconSprite, 1); float joystickRadius = 220; 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::create("joystick1.png")); //中心点 joystickSkin->setThumbSprite(CCSprite::create("joystick2.png")); joystickSkin->getThumbSprite()->setScale(1.0f); joystickSkin->setPosition(CCPointMake(joystickRadius,joystickRadius)); joystickSkin->setJoystick(joystick); this->addChild(joystickSkin); this->scheduleUpdate(); return true; }
更新代码:
void HelloWorld::update(float t) { // getVelocity()到的数值很小 需要放大 CCPoint poi = ccpMult(joystick->getVelocity(), 100); //right if ((poi.x > 0) && (poi.x - poi.y) >0 && (poi.x + poi.y) > 0) { iconSprite->setPosition(ccp(iconSprite->getPosition().x+1, iconSprite->getPosition().y)); } //left else if ( (poi.x < 0) && (poi.x + poi.y) < 0 &&(poi.x - poi.y) < 0) { iconSprite->setPosition(ccp(iconSprite->getPosition().x-1, iconSprite->getPosition().y)); } //up else if ((poi.y > 0) &&(poi.y - poi.x) > 0 &&(poi.y + poi.x) >0 ) { iconSprite->setPosition(ccp(iconSprite->getPosition().x, iconSprite->getPosition().y+1)); } //down else if ((poi.y < 0) &&(poi.y - poi.x) < 0 && (poi.y + poi.x) < 0) { iconSprite->setPosition(ccp(iconSprite->getPosition().x, iconSprite->getPosition().y-1)); } }
效果图:
参考博文:http://blog.csdn.net/kuloveyouwei/article/details/9102623
转载请注明出处:http://blog.csdn.net/rexuefengye/article/details/16855971
麻烦了。呵呵〜〜〜