Cocos2d-x Game Pause screen, listen home button, return key, Menu key solution

Source: Internet
Author: User
Tags addchild

Game Pause Screen:

Cocos2d-x in the game pause interface provides the idea is to use Pushscene () and Popscne (), that is, push and pop-up scenes, when the game is paused, advance (Pushscene ()) Pause the scene, the previous run of the scene will be automatically paused, and then we can pause the scene to operate, such as Resume,restart,quit, when we no longer need to pause the scene, you can Popscene () will pause the scene pop-up. (The scene is like a piece of paper, we push a scene, the equivalent of a piece of paper to cover another, pop-up scene is equivalent to the surface of the piece of paper removed).

The relevant code for the push-pause scenario is as follows:

Ccrendertexture *rendertexture = ccrendertexture::create (800,600);
	Rendertexture->begin ();
	This->getparent ()->visit ();
	Rendertexture->end ();  This is actually saved by ccrendertexture the current interface (equivalent to a screenshot), and then passed to the pause interface, as the background sprite

	ccdirector::shareddirector ()->pushscene ( Pauselayer::scene (rendertexture,true));

The relevant code for pausing the scene Pauselayer is as follows:

ccscene* pauselayer::scene (ccrendertexture* sqr,bool isflip) {
	Ccscene *m_scene = Ccscene::create ();

	Ccsprite *_SPR = Ccsprite::createwithtexture (Sqr->getsprite ()->gettexture ());
	_spr->setposition (CCP (+));
	_spr->setflipy (isflip);
	_spr->setcolor (Ccgray);
	M_scene->addchild (_SPR);

	' Layer ' is an Autorelease object
    pauselayer* layerr = Pauselayer::create ();

	Add layer as a child to scene
	M_scene->addchild (layerr);



	Return the scene
	return m_scene;
}


Listen for the return key and menu key:

Points:

1. Inheriting cckeypaddelegate

2. Implement two virtual functions

virtual void Keybackclicked ()
virtual void Keymenuclicked ()

If you want to implement the listener is Cclayer or inherit Cclayer, then only the second step and in the initialization of setkeypadenabled (true);

Because the Cclayer itself inherits the Cckeypaddelegate, as shown in the figure below

Class Cc_dll Cclayer:public Ccnode, public cctouchdelegate, public ccaccelerometerdelegate, public cckeypaddelegate


Listen to the home button:

In Cocos2d-x I have not yet found a clear way to listen to the home key, but can use an alternative.

I wonder if you have found two methods in AppDelegate.cpp:

This function was called when the app was inactive. When comes a phone call,it's be invoked too
void Appdelegate::applicationdidenterbackground () {
    ccdirector:: Shareddirector ()->stopanimation ();

    If you use Simpleaudioengine, it must is pause
    //Simpleaudioengine::sharedengine ()->pausebackgroundmusic (); c4/>}

//This function would be called if the app is active again
void Appdelegate::applicationwillenterforegroun D () {
    ccdirector::shareddirector ()->startanimation ();

    If you use the Simpleaudioengine, it must resume here
    //Simpleaudioengine::sharedengine ()->resumebackgroundmusic ();
}

Notice the English interpretation of these two methods, in fact, the two methods are to determine whether the program is switched or whether it is thrown into the background work. Because pressing the home button on the phone, the actual switch is to push the program to the background. So, we can make a fuss over these two methods.

The relevant code is as follows:

void Appdelegate::applicationdidenterbackground ()
{
    ccdirector::shareddirector ()->stopanimation ();

    Simpleaudioengine::sharedengine ()->pausebackgroundmusic ();
	global* sh = global::toins ();
	ccrendertexture* rendertexture;
	Switch (sh->targetscene) {case
	targetscenefirstscene: Break
		;
	Case Targetscenesecondscene:
		rendertexture = Ccrendertexture::create (800,600);
		Rendertexture->begin ();
		Sh->battlelayer->visit ();
		Rendertexture->end ();

		Ccdirector::shareddirector ()->pushscene (Pauselayer::scene (Rendertexture,false));
		break;
	Case Targetsceneinvalid: Break
		;
	Default: Break
		;
	}
}

This function was called when the app was active again
void Appdelegate::applicationwillenterforeground ()
{
    ccdirector::shareddirector ()->startanimation ();

    Simpleaudioengine::sharedengine ()->resumebackgroundmusic ();
}

In the above code, what I do is, when the program inactive (push to the background), push the Pause interface


If there are other solutions, please leave a message

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.