cocos2dx3.2开发 RPG《Flighting》十四暂停按钮

一、前言

整个教程快接近尾声了。还有一个暂停功能需要添加

二、正文

首先,我们要在右上方添加一个按钮

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());
	}
}
大概就这样吧。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...