转自--http://blog.youkuaiyun.com/evankaka/article/details/42936117
本文要实现游戏中的暂停、重新开始。将当前界面截图,然后用这张图去构造一个层(以这张图为背景),然后加个按钮,主界面点暂停时,pushScene(),然后转到游戏暂停界面,当在游戏暂停界面点继续游戏popScne()。在cocos2dx中推进(pushScene())暂停场景,之前运行的场景将会自动暂停,然后我们可以在暂停场景中操作,当我们不再需要暂停场景时,可以popScene()将暂停场景弹出。重新开始游戏直接replaceScene().
cocos2d-x版本:2.2.5
工程环境:windows7+VS2010
打开方式:将工程放在cocos2d-x安装目录下的project文件夹下用VS打开
(源码免费下载)
本文效果:
一、游戏暂停界面初步实现
思路:将当前界面截图,然后用这张图去构造一个层(以这张图为背景),然后加个按钮,主界面点暂停时,pushScene(),然后转到游戏暂停界面,当在游戏暂停界面点继续游戏时popScne(),
首先看看自定义的游戏暂停的层
头文件 Gamepause.h
- #ifndef __Gamepause__H__
- #define __Gamepause__H__
- #include "cocos2d.h"
- USING_NS_CC;
- class Gamepause : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene(CCRenderTexture* sqr);
- CREATE_FUNC(Gamepause);
- //继续游戏
- void menuContinueCallback(CCObject* pSender);
- private:
- };
- #endif // __Gamepause_H__
- #include "Gamepause.h"
- //传入一个CCrenderTexture
- //相当于一个正在运行的游戏的截图作为这个暂停对话框的背景
- //这样就看起来像是对话框在游戏界面之上,一般游戏当中都是这样子写的。
- CCScene* Gamepause::scene(CCRenderTexture* sqr)
- {
- CCScene *scene = CCScene::create();
- Gamepause *layer = Gamepause::create();
- scene->addChild(layer,1);
- //增加部分:使用Game界面中截图的sqr纹理图片创建Sprite
- //并将Sprite添加到GamePause场景层中
- //得到窗口的大小
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- CCSprite *back_spr = CCSprite::createWithTexture(sqr->getSprite()->getTexture());
- back_spr->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); //放置位置,这个相对于中心位置。
- back_spr->setFlipY(true); //翻转,因为UI坐标和OpenGL坐标不同
- back_spr->setColor(cocos2d::ccGRAY); //图片颜色变灰色
- scene->addChild(back_spr);
- return scene;
- }
- bool Gamepause::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- //得到窗口的大小
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- //原点坐标
- CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
- //继续游戏按钮
- CCMenuItemImage *pContinueItem = CCMenuItemImage::create(
- "pause_continue.png",
- "pause_continue.png",
- this,
- menu_selector(Gamepause::menuContinueCallback));
- pContinueItem->setPosition(ccp( visibleSize.width/2 ,visibleSize.height/2+30));
- CCMenu* pMenu = CCMenu::create(pContinueItem,NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu, 2);
- return true;
- }
- void Gamepause::menuContinueCallback(CCObject* pSender)
- {
- CCDirector::sharedDirector()->popScene();
- }
在游戏主界面init函数加个:
- CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",
- this,
- menu_selector(HelloWorld::menuPauseCallback));
- pCloseItem->setPosition(ccp(visibleSize.width - pCloseItem->getContentSize().width/2 ,
- visibleSize.height - pCloseItem->getContentSize().height/2));
- // create menu, it's an autorelease object
- CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu, 1);
然后是回调用的函数 暂停界面((记得加上面的头文件就是了))
- void HelloWorld::menuPauseCallback(CCObject* pSender)
- {
- //得到窗口的大小
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- CCRenderTexture *renderTexture = CCRenderTexture::create(visibleSize.width,visibleSize.height);
- //遍历当前类的所有子节点信息,画入renderTexture中。
- //这里类似截图。
- renderTexture->begin();
- this->getParent()->visit();
- renderTexture->end();
- //将游戏界面暂停,压入场景堆栈。并切换到GamePause界面
- CCDirector::sharedDirector()->pushScene(Gamepause::scene(renderTexture));
- }
这里来看看效果:
效果就是这样了,基本实现了游戏暂停的功能了。上面的代码可以直接拿去用,自己把图片改改就行了
二、游戏暂停界面美化实现
单单只有上面的肯定是不行的,太难看了,所以给图片的按钮加个背景图片,然后再加三个按钮,
这是按钮背景图片
这是三个按钮的图片
要用的直接拿去用,全是我原创的
直接看下代码,就是在上面的基础上来增加函数的
Gamepause.h
- #ifndef __Gamepause__H__
- #define __Gamepause__H__
- #include "cocos2d.h"
- USING_NS_CC;
- class Gamepause : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene(CCRenderTexture* sqr);
- CREATE_FUNC(Gamepause);
- //继续游戏
- void menuContinueCallback(CCObject* pSender);
- //重新开始游戏
- void menuRestart(CCObject* pSender);
- //回主界面
- void menuLogin(CCObject* pSender);
- private:
- };
- #endif // __Gamepause_H__
Gamepause.cpp
- #include "Gamepause.h"
- #include "HelloWorldScene.h"//重新开始游戏的头文件
- //传入一个CCrenderTexture
- //相当于一个正在运行的游戏的截图作为这个暂停对话框的背景
- //这样就看起来像是对话框在游戏界面之上,一般游戏当中都是这样子写的。
- CCScene* Gamepause::scene(CCRenderTexture* sqr)
- {
- CCScene *scene = CCScene::create();
- Gamepause *layer = Gamepause::create();
- scene->addChild(layer,1);//把游戏层放上面,我们还要在这上面放按钮
- //增加部分:使用Game界面中截图的sqr纹理图片创建Sprite
- //并将Sprite添加到GamePause场景层中
- //得到窗口的大小
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- CCSprite *back_spr = CCSprite::createWithTexture(sqr->getSprite()->getTexture());
- back_spr->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); //放置位置,这个相对于中心位置。
- back_spr->setFlipY(true); //翻转,因为UI坐标和OpenGL坐标不同
- back_spr->setColor(cocos2d::ccGRAY); //图片颜色变灰色
- scene->addChild(back_spr);
- //添加游戏暂停背景小图,用来放按钮
- CCSprite *back_small_spr = CCSprite::create("back_pause.png");
- back_small_spr->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); //放置位置,这个相对于中心位置。
- scene->addChild(back_small_spr);
- return scene;
- }
- bool Gamepause::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- //得到窗口的大小
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- //原点坐标
- CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
- //继续游戏按钮
- CCMenuItemImage *pContinueItem = CCMenuItemImage::create(
- "pause_continue.png",
- "pause_continue.png",
- this,
- menu_selector(Gamepause::menuContinueCallback));
- pContinueItem->setPosition(ccp( visibleSize.width/2 ,visibleSize.height/2+30));
- //重新开始游戏按钮
- CCMenuItemImage *pRestartItem = CCMenuItemImage::create(
- "pause_restart.png",
- "pause_restart.png",
- this,
- menu_selector(Gamepause::menuRestart));
- pRestartItem->setPosition(ccp( visibleSize.width/2 ,visibleSize.height/2-20));
- //回主界面
- CCMenuItemImage *pLoginItem = CCMenuItemImage::create(
- "pause_login.png",
- "pause_login.png",
- this,
- menu_selector(Gamepause::menuLogin));
- pLoginItem->setPosition(ccp( visibleSize.width/2 ,visibleSize.height/2-70));
- // create menu, it's an autorelease object
- CCMenu* pMenu = CCMenu::create(pContinueItem,pRestartItem,pLoginItem,NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu, 2);
- return true;
- }
- void Gamepause::menuContinueCallback(CCObject* pSender)
- {
- CCDirector::sharedDirector()->popScene();
- }
- //重新开始游戏
- void Gamepause::menuRestart(CCObject* pSender)
- {
- CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());
- }
- //回主界面
- void Gamepause::menuLogin(CCObject* pSender)
- {
- }
最后再来看看效果:
三、思路总结
游戏暂停将当前界面截图,然后这个张图去创建一个层,然后就是push场景和pop场景了。