说明:这里是借鉴:晓风残月前辈的博客,他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本重写的,目前我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记
这一步当中,我们给PlayScene中 添加两个按钮,让主角Jump and Crouch,按钮功能如下:
Jump按钮,按下主角跳起来
Crouch按钮,按下主角下蹲,一直按着一直蹲,松开之后主角才站起来
这里用按钮包含头文件"cocos-ext.h"会遇到一点点问题,后面给出详细解决方法:
首先PlayScene.h中要包含头文件:
<span style="font-size:14px;">#include "cocos-ext.h" USING_NS_CC_EXT;</span>然后添加函数:
//Jump void createJumpButton(); void jumpEvent(Ref* pSender,Control::EventType event); //Crouch void createCrouchButton(); void crouchDown(Ref* pSender,Control::EventType event); void crouchUp(Ref* pSender,Control::EventType event);
这里先解释一下,Crouch 有两个事件,我们在按钮中,要添加两个按钮事件,一个是按下,那么人物蹲下,然后按钮松开,人物又起来,两个事件,两个回调函数
然后实现:void PlayScene::createJumpButton(){ auto visibleSize = Director::getInstance()->getVisibleSize(); auto jumpLabel = Label::create("Jump","Arail",30); auto norBtn = Scale9Sprite::create("norBtn.png"); auto lightBtn = Scale9Sprite::create("lightBtn.png"); auto jumpBtn = ControlButton::create(jumpLabel,norBtn); jumpBtn->setPosition(visibleSize.width-80,130); jumpBtn->setBackgroundSpriteForState(lightBtn,Control::State::HIGH_LIGHTED); jumpBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(PlayScene::jumpEvent), Control::EventType::TOUCH_DOWN); this->addChild(jumpBtn); } void PlayScene::jumpEvent(Ref* pSender,Control::EventType event){ //SimpleAudioEngine::sharedEngine()->playEffect("jump.mp3"); m_runner->Jump(); } void PlayScene::createCrouchButton(){ auto visibleSize = Director::getInstance()->getVisibleSize(); auto crouchLabel = Label::create("Crouch","Arail",30); auto norBtn = Scale9Sprite::create("norBtn.png"); auto lightBtn = Scale9Sprite::create("lightBtn.png"); auto crouchBtn = ControlButton::create(crouchLabel,norBtn); crouchBtn->setPosition(visibleSize.width-100,80); crouchBtn->setBackgroundSpriteForState(lightBtn,Control::State::HIGH_LIGHTED); // crouchBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(PlayScene::crouchDown), Control::EventType::TOUCH_DOWN); // crouchBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(PlayScene::crouchUp), Control::EventType::TOUCH_UP_INSIDE); this->addChild(crouchBtn); } void PlayScene::crouchDown(Ref* pSender,Control::EventType event){ //添加判断 if(m_runner->getState() == running){ m_runner->Crouch(); m_runner->setPosition(runner_posX,ground_hight+m_runner->getCrouchSize().height/2); } //SimpleAudioEngine::sharedEngine()->playEffect("crouch.mp3"); } void PlayScene::crouchUp(Ref* pSender,Control::EventType event){ //否则,在跳起来的时候,点击crouch 松开之后,状态就会变成running if(m_runner->getState() == crouch){ m_runner->stopAllActions(); m_runner->Run(); m_runner->setPosition(runner_posX,ground_hight+m_runner->getRunJumpSize().height/2); } }
this->scheduleUpdate();,因为我们需要在update中判断起跳到最高点下落
然后在 PlayScene 的init函数也要添加 两个create按钮函数
那么生成,然后这里就会遇到问题:
那么这里我们需要在项目上右键到属性,【配置属性】->【C/C++】->【常规】->【附加包含目录】如图:
在最下面添加:$(EngineRoot)
然后同样在【连接器】->【输入】中添加libExtensions.lib 如图:
然后生成会说无法打开libExtensions.lib,那么就需要在这里下载libExtension.lib(不用积分)然后把它直接放在 [项目的目录下] / proj.win32 / Debug.win32中,然后就可以了。
我想应该有别的方法,如果有知道的麻烦告诉我一下,不胜感激
再次重新生成,就可以啦
终于可以看到效果:
好啦,后面要开始准备金币和岩石阻碍啦。。。
个人愚昧观点,欢迎指正与讨论