因为iOS,Android设备使用触摸屏来输入,没有传统移动游戏设备配备的按钮,十字按钮或者模拟手柄,我们需要一个虚拟手柄来控制游戏。你可以使用虚拟手柄对游戏物体进行操控,就像使用实际的手柄一样。
SneakyInput控制手柄源码: 点我下载
把×××下来,加入到你的项目中,别忘了在android.mk添加相关内容哦!
我们首要目标是添加一个可以让玩家进行飞船射击的按钮,他们点击按钮的时候 会发射×××,
----------------------
接下来 我会在项目中添加一个新的类InputLayer ,这个类继承自CCLayer,他会被添加到MainScene
CCScene *MainScene::scene() { CCScene *pScene = NULL; do { pScene = CCScene::create(); MainScene *main = MainScene::create(); pScene->addChild(main, -1); InputLayer *input = InputLayer::create(); pScene->addChild(input, 0); } while (0); return pScene; }
将SneakyInput添加到InputLayer的头文件中
#include "SneakyInput/SneakyButton.h" #include "SneakyInput/SneakyJoystick.h" #include "SneakyInput/SneakyButtonSkinnedBase.h" #include "SneakyInput/SneakyJoystickSkinnedBase.h"
另外,我在头文件中加了一个SneakyButton成员变量,因为我们马上就会用到。
class InputLayer: public CCLayer { public: InputLayer(); virtual ~InputLayer(); SneakyButton *snkBtn; void update(ccTime time); bool init();CREATE_FUNC(InputLayer) ; };
在init方法中 我们生成了一个SneakyButton
bool InputLayer::init() { bool bRet = false; do { CCSize size = CCDirector::sharedDirector()->getWinSize(); float buttonRadius = 42; snkBtn = new SneakyButton(); snkBtn->autorelease(); snkBtn->initWithRect(CCRectZero); snkBtn->setPosition(CCPointMake(size.width-buttonRadius,buttonRadius)); snkBtn->setRadius(buttonRadius); this->addChild(snkBtn); this->scheduleUpdate(); bRet = true; } while (0); return bRet; }
因为SneakyButton没有用到initWithRect方法中的CGRect参数,所以我传了一个CGRectZero给这个方法。实际的处理触摸事件的代码是使用radius(半径)这个属性来决定按钮是否要响应触摸。 |
InputLayer类通过以下代码预约更新
this->scheduleUpdate();
更新方法是用来测试按钮是否已被点击
void InputLayer::update(ccTime time) { if (snkBtn->getIsActive()) { CCLog("按下按钮"); } }
运行程序,你会发现屏幕上没有任何按钮 不过你可以点击屏幕右下角 然后可以在log日志中正在打印“按下按钮”
接下来 我们将让按钮可见,也就是添加皮肤
这里 我们使用到了SneakyButtonSkinnedBase
bool InputLayer::init() { bool bRet = false; do { CCSize size = CCDirector::sharedDirector()->getWinSize(); float buttonRadius = 42; snkBtn = new SneakyButton(); snkBtn->autorelease(); snkBtn->initWithRect(CCRectZero); // 这个属性可以让玩家按住按钮不放的时候,×××会持续地射击出去 snkBtn->setIsHoldable(true); SneakyButtonSkinnedBase *sbsb = SneakyButtonSkinnedBase::create(); //默认状态 sbsb->setDefaultSprite(CCSprite::create("nor.png")); //点击状态 sbsb->setPressSprite(CCSprite::create("tou.png")); //激活状态 sbsb->setActivatedSprite(CCSprite::create("tou.png")); sbsb->setPosition(CCPointMake(size.width-buttonRadius,buttonRadius)); sbsb->setButton(snkBtn); this->addChild(sbsb); this->scheduleUpdate(); bRet = true; } while (0); return bRet; }
我们不需要设置按钮的半径属性了,因为SneakyButtonSkinnedBase类会使用提供的按钮图片来确定按钮半径的大小 |
控制动作
接下来我们在游戏中添加摇杆
bool InputLayer::init() { bool bRet = false; do { CCSize size = CCDirector::sharedDirector()->getWinSize(); float buttonRiadus = 75; snkJs = new SneakyJoystick(); snkJs->autorelease(); // 决定虚拟手柄的半径大小 snkJs->initWithRect(CCRectMake(0,0,buttonRiadus,buttonRiadus)); //自动回到中心 snkJs->setAutoCenter(true); //是否支持死亡区域,该区域不会触发 snkJs->setHasDeadzone(true); //死亡区域的半径 snkJs->setDeadRadius(15); SneakyJoystickSkinnedBase *sjssb =SneakyJoystickSkinnedBase::create(); sjssb->setPosition(CCPointMake(buttonRiadus*1.5,1.5*buttonRiadus)); //摇杆的背景图 sjssb->setBackgroundSprite(CCSprite::create("handle1.png")); //摇杆的图片 sjssb->setThumbSprite(CCSprite::create("handle2.png")); sjssb->setJoystick(snkJs); this->addChild(sjssb); this->scheduleUpdate(); bRet = true; } while (0); return bRet; }
完成摇杆的添加 接下来要实现摇杆事件的监听
void InputLayer::update(ccTime time) { // getVelocity()到的数值很小 需要放大 800是估算的 CCPoint velocity = ccpMult(snkJs->getVelocity(), 800); if (velocity.x != 0 && velocity.y != 0) { CCLog("x=%f,y=%f", velocity.x, velocity.y); } }
接下来一章将开发一个小游戏,如有问题,请提出
本教程根据Cocos2d教程翻译过来
使用的cocos2d-x版本为2.02
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。