说明:这里是借鉴:晓风残月前辈的博客。他是将泰然网的跑酷教程,用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记
这一步其中,我们给PlayScene中 加入两个button,让主角Jump and Crouch,button功能例如以下:
Jumpbutton。按下主角跳起来
Crouchbutton,按下主角下蹲,一直按着一直蹲,松开之后主角才站起来
这里用button包括头文件"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 有两个事件。我们在button中,要加入两个button事件,一个是按下。那么人物蹲下。然后button松开,人物又起来。两个事件。两个回调函数
然后实现: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); } }
然后在Runner.cpp中的init 函数中,还要记得
this->scheduleUpdate();,由于我们须要在update中推断起跳到最高点下落
然后在 PlayScene 的init函数也要加入 两个createbutton函数
那么生成,然后这里就会遇到问题:
那么这里我们须要在项目上右键到属性,【配置属性】->【C/C++】->【常规】->【附加包括文件夹】如图:
在最以下加入:$(EngineRoot)
然后相同在【连接器】->【输入】中加入libExtensions.lib 如图:
然后生成会说无法打开libExtensions.lib,那么就须要在这里下载libExtension.lib(不用积分)然后把它直接放在 [项目的文件夹下] / proj.win32 / Debug.win32中,然后就能够了。
我想应该有别的方法。假设有知道的麻烦告诉我一下。不胜感激
再次又一次生成,就能够啦
最终能够看到效果:
好啦,后面要開始准备金币和岩石阻碍啦。
。
。
个人愚昧观点。欢迎指正与讨论