【cocos2d-x】横向滚屏射击游戏②----虚拟控制手柄
因为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);returnpScene;}
将SneakyInput添加到InputLayer的头文件中
#include"SneakyInput/SneakyButton.h"#include"SneakyInput/SneakyJoystick.h"#include"SneakyInput/SneakyButtonSkinnedBase.h"#include"SneakyInput/SneakyJoystickSkinnedBase.h"
另外,我在头文件中加了一个SneakyButton成员变量,因为我们马上就会用到。
classInputLayer:publicCCLayer{public:InputLayer();virtual~InputLayer();SneakyButton*snkBtn;voidupdate(ccTimetime);boolinit();CREATE_FUNC(InputLayer);};
在init方法中 我们生成了一个SneakyButton
boolInputLayer::init(){boolbRet=false;do{CCSizesize=CCDirector::sharedDirector()->getWinSize();floatbuttonRadius=42;snkBtn=newSneakyButton();snkBtn->autorelease();snkBtn->initWithRect(CCRectZero);snkBtn->setPosition(CCPointMake(size.width-buttonRadius,buttonRadius));snkBtn->setRadius(buttonRadius);this->addChild(snkBtn);this->scheduleUpdate();bRet=true;}while(0);returnbRet;}
因为SneakyButton没有用到initWithRect方法中的CGRect参数,所以我传了一个CGRectZero给这个方法。实际的处理触摸事件的代码是使用radius(半径)这个属性来决定按钮是否要响应触摸。
InputLayer类通过以下代码预约更新
this->scheduleUpdate();
更新方法是用来测试按钮是否已被点击
voidInputLayer::update(ccTimetime){if(snkBtn->getIsActive()){CCLog("按下按钮");}}
运行程序,你会发现屏幕上没有任何按钮 不过你可以点击屏幕右下角 然后可以在log日志中正在打印“按下按钮”
接下来 我们将让按钮可见,也就是添加皮肤
这里 我们使用到了SneakyButtonSkinnedBase
boolInputLayer::init(){boolbRet=false;do{CCSizesize=CCDirector::sharedDirector()->getWinSize();floatbuttonRadius=42;snkBtn=newSneakyButton();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);returnbRet;} 我们不需要设置按钮的半径属性了,因为SneakyButtonSkinnedBase类会使用提供的按钮图片来确定按钮半径的大小
控制动作
接下来我们在游戏中添加摇杆
boolInputLayer::init(){boolbRet=false;do{CCSizesize=CCDirector::sharedDirector()->getWinSize();floatbuttonRiadus=75;snkJs=newSneakyJoystick();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);returnbRet;}
完成摇杆的添加 接下来要实现摇杆事件的监听
voidInputLayer::update(ccTimetime){//getVelocity()到的数值很小需要放大800是估算的CCPointvelocity=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
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。