Cocos2d-x game pause, continue to play, restart the implementation of the interface---game development "Zhao Yun to Fight" (10)

Source: Internet
Author: User
Tags addchild call back

          here isEvankaka Blog, welcome everyone to discuss and exchange ~~~~~~

Reprint Please specify the source http://blog.csdn.net/evankaka/article/details/42936117

This article is to realize the pause in the game, start again. The current interface, and then use this graph to construct a layer (in this picture as the background), and then add a button, the main interface point is paused, Pushscene (), then go to the game Pause screen and continue the game when the game is paused Popscne (). In Cocos2dx ( pushscene ()) Pause the scene, the previously running scene will be automatically paused, then we can operate in the pause scene , and when we no longer need to pause the scene, we can Popscene () Pauses the scene pop-up. Re-start the game directly Replacescene ().

Cocos2d-x version: 2.2.5

Engineering Environment: WINDOWS7+VS2010

Open by: Put the project under the project folder in the Cocos2d-x installation directory with VS Open

The effect of this article:


First, the game Pause Interface Preliminary implementation

Idea: The current interface, and then use this map to construct a layer (in this picture as the background), and then add a button, the main interface point pause,pushscene (), and then go to the game pause screen, when the game pause interface point to continue the game Popscne (),

First look at the layer of the custom game paused

Header file 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);//continue to play void Menucontinuecallback (ccobject* psender);p rivate:}; #endif//__gamepause_h__
then the implementation file
#include "Gamepause.h"//Pass in a ccrendertexture//equivalent to a running game as the background of this pause dialog//So it looks like a dialog box on the game interface, the general game is written in this way.    ccscene* Gamepause::scene (ccrendertexture* sqr) {Ccscene *scene = Ccscene::create ();     Gamepause *layer = Gamepause::create ();      Scene->addchild (layer,1); Add part: Create a sprite//using the SQR texture picture in the game interface and add the sprite to the Gamepause scene layer//Get the size of the window ccsize visiblesize = ccdirector:: Shareddirector ()->getvisiblesize ();  Ccsprite *BACK_SPR = Ccsprite::createwithtexture (Sqr->getsprite ()->gettexture ()); Back_spr->setposition (CCP (VISIBLESIZE.WIDTH/2,VISIBLESIZE.HEIGHT/2)); The placement position, which is relative to the center position.            Back_spr->setflipy (TRUE); Flip because UI coordinates and OpenGL coordinates are different back_spr->setcolor (Cocos2d::ccgray);    The color of the picture turns gray scene->addchild (BACK_SPR); return scene;} BOOL Gamepause::init () {if (!    Cclayer::init ()) {return false; }//gets the size of the window ccsize visiblesize = Ccdirector::shareddirector ()->getvisiblesize ();//origin coordinates ccpoint origin = Ccdirector:: Shareddirector ()->getvisibleorigin ();//Continue the game button CcmenUitemimage *pcontinueitem = ccmenuitemimage::create ("Pause_continue.png", "Pause_continue.png", This,menu_selector ( Gamepause::menucontinuecallback));p continueitem->setposition (CCP (VISIBLESIZE.WIDTH/2, visibleSize.height/2+ 30));    ccmenu* Pmenu = ccmenu::create (pcontinueitem,null);p menu->setposition (Ccpointzero); This->addChild (PMenu, 2); return true;} void Gamepause::menucontinuecallback (ccobject* psender) {ccdirector::shareddirector ()->popscene ();}
How to use

In the game main interface The INIT function adds a:

Ccmenuitemimage *pcloseitem = ccmenuitemimage::create ("Closenormal.png", "Closeselected.png", This,menu_selector ( Helloworld::menupausecallback));p closeitem->setposition (CCP (visiblesize.width-pcloseitem->getcontentsize (). WIDTH/2, Visiblesize.height-pcloseitem->getcontentsize (). HEIGHT/2));//Create menu, it's an autorelease objectccmenu* Pmenu = ccmenu::create (Pcloseitem, NULL);p menu->setposition (Ccpointzero); This->addchild (PMenu, 1);

Then there is the call back function Pause interface (Remember to add the face of the header file just))

void Helloworld::menupausecallback (ccobject* psender) {//Gets the size of the window ccsize visiblesize = Ccdirector::shareddirector ()- >getvisiblesize (); Ccrendertexture *rendertexture = ccrendertexture::create (visiblesize.width,visiblesize.height);//Traverse all child node information of the current class, Draw into the rendertexture. Similar here. Rendertexture->begin (); This->getparent ()->visit (); Rendertexture->end ();//Pause the game interface and press into the scene stack. and switch to Gamepause interface Ccdirector::shareddirector ()->pushscene (Gamepause::scene (Rendertexture));}

Here to see the effect:



The effect is this, the basic realization of the game pause function. The above code can be directly used to change the picture itself.


second, the game Pause screen beautification implementation

Only the above must be no, too ugly, so give the picture button to add a background image, and then add three buttons,

This is the button background picture


This is a picture of three buttons


I'm going to use it straight away, it's all my original.

Look directly at the code, that is, on the basis of the above to increase the function

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);//Continue the game void Menucontinuecallback (ccobject* psender);//Restart the game void Menurestart (ccobject* Psender);//Return to the main interface void Menulogin (ccobject* psender);p rivate:}; #endif//__gamepause_h__

Gamepause.cpp
#include "Gamepause.h" #include "HelloWorldScene.h"//re-start the game header file//pass in a ccrendertexture//equivalent to a running game as the background of this Pause dialog box// This looks like a dialog box above the game interface, which is written in the general game.    ccscene* Gamepause::scene (ccrendertexture* sqr) {Ccscene *scene = Ccscene::create (); Gamepause *layer = Gamepause::create () scene->addchild (layer,1);//Put the game layer on top and we'll put a button on it. Add part: Use the SQR texture picture in the game interface to create a sprite//and add a sprite to the Gamepause scene layer//Get the size of the window ccsize visiblesize = Ccdirector::shareddirector ( )->getvisiblesize ();  Ccsprite *BACK_SPR = Ccsprite::createwithtexture (Sqr->getsprite ()->gettexture ()); Back_spr->setposition (CCP (VISIBLESIZE.WIDTH/2,VISIBLESIZE.HEIGHT/2)); The placement position, which is relative to the center position.            Back_spr->setflipy (TRUE); Flip because UI coordinates and OpenGL coordinates are different back_spr->setcolor (Cocos2d::ccgray); The picture color turns gray scene->addchild (BACK_SPR);//Add the game to pause the background small image, used to put the button ccsprite *back_small_spr = Ccsprite::create ("Back_ Pause.png "); Back_small_spr->setposition (CCP (VISIBLESIZE.WIDTH/2,VISIBLESIZE.HEIGHT/2)); The placement position, which is relative to the center position.    Scene->addchild (BACK_SMALL_SPR); ReTurn scene;} BOOL Gamepause::init () {if (!    Cclayer::init ()) {return false; }//gets the size of the window ccsize visiblesize = Ccdirector::shareddirector ()->getvisiblesize ();//origin coordinates ccpoint origin = Ccdirector:: Shareddirector ()->getvisibleorigin ();//Continue the game button ccmenuitemimage *pcontinueitem = Ccmenuitemimage::create ("Pause_ Continue.png "," Pause_continue.png ", This,menu_selector (gamepause::menucontinuecallback));p continueitem-> SetPosition (CCP (VISIBLESIZE.WIDTH/2, visiblesize.height/2+30));//Restart game button 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));//back to main interface Ccmenuitemimage * Ploginitem = Ccmenuitemimage::create ("Pause_login.png", "Pause_login.png", This,menu_selector (Gamepause::menuLogin );p Loginitem->setposition (CCP (VISIBLESIZE.WIDTH/2, visiblesize.height/2-70));//Create menu, it's an autorelease objectccmenu* Pmenu = CCMeNu::create (pcontinueitem,prestartitem,ploginitem,null);p menu->setposition (Ccpointzero); This->addChild (    Pmenu, 2); return true;} void Gamepause::menucontinuecallback (ccobject* psender) {ccdirector::shareddirector ()->popscene ();} Start the game again void Gamepause::menurestart (ccobject* psender) {ccdirector::shareddirector ()->replacescene (HelloWorld: : Scene ());} Back to main interface void Gamepause::menulogin (ccobject* psender) {}


Finally look at the effect: (Passed a night, did not succeed, come again tomorrow to do it)


Cocos2d-x game pause, continue to play, restart the implementation of the interface---game development "Zhao Yun to Fight" (10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.