一、前言
@H_502_5@整个教程快接近尾声了。还有一个暂停功能需要添加
二、正文
首先,我们要在右上方添加一个按钮
bool FlightLayer::init(){ MenuItemImage* pauseBtnItem = MenuItemImage::create("UI/pauseBtn.png","UI/pauseBtn.png",[=](Ref* pSender){Director::getInstance()->pushScene(PauseScene::create());}); Menu* pauseBtn = Menu::create(pauseBtnItem,NULL); pauseBtn->setPosition(1180,650); this->addChild(pauseBtn,4); return true; }
这个按钮很简单,回到函数也直接用lumada表达式了
点击的时候会弹出PuaseScene
class PauseScene : public Scene{ public: virtual bool init(); CREATE_FUNC(PauseScene); private: void continueGame(Ref* pSender,TouchEventType type); void stopGame(Ref* pSender,TouchEventType type); };
#include "PauseScene.h" bool PauseScene::init(){ Widget* pNode = GUIReader::getInstance()->widgetFromJsonFile("UI/PauseUI.json"); this->addChild(pNode,0); Button* continueBtn = (Button*)Helper::seekWidgetByName(pNode,"continueBtn"); continueBtn->addTouchEventListener(this,toucheventselector(PauseScene::continueGame)); Button* stopBtn = (Button*)Helper::seekWidgetByName(pNode,"stopBtn"); stopBtn->addTouchEventListener(this,toucheventselector(PauseScene::stopGame)); return true; } void PauseScene::continueGame(Ref* pSender,TouchEventType type){ if(type == TouchEventType::TOUCH_EVENT_BEGAN){ CCLOG("GAME CONTINUE"); Director::getInstance()->popScene(); } } void PauseScene::stopGame(Ref* pSender,TouchEventType type){ if(type == TouchEventType::TOUCH_EVENT_BEGAN){ CCLOG("GAME STOP"); Director::getInstance()->replaceScene(ChooseScene::create()); } }大概就这样吧。