Cocos2d-x UI Development Scenario switch code Instance _c language

Source: Internet
Author: User
Tags addchild

The scene switch in Cocos2d-x is done by calling the corresponding method, and the Ccdirector::shareddirector ()->replacescene () method is used to toggle the scene, and the parameter is the new scene to be switched. This method will release the old scene. by Pushscene () switch, the old scene is pushed into the stack so that the Popscene () function can be used to stack, and continue to run the original scene. Run a new scene with Runwithscene () when the program starts. The following is illustrated by the code. Click the image below to see the effect.

This code set up a total of two scenes, one is the original HelloWorld scene, one is the secondscene scene. Look at the code specifically.

BOOL Helloworld::init ()
{
 bool BRet = false;
 Do
 {
  cc_break_if (! Cclayer::init ());

		Cclabelttf * TTF = cclabelttf::create ("Next Scene", "Arial",);
		Add a label menu for this scenario
		Ccmenuitemlabel * labelmenu = Ccmenuitemlabel::create (Ttf,this,menu_selector (HelloWorld:: Menuclosecallback));
		Ccmenu * menu = ccmenu::create (labelmenu,null);
		This->addchild (menu);

  BRet = true;
 } while (0);

 return bRet;
}

This function enables the switching of the scene
void Helloworld::menuclosecallback (ccobject* psender)
{
	//Use the following scene switching classes can produce special effects, these classes have many , there are usually two or three parameters, the first parameter is
	//time, the second parameter is the scene to switch to, the third parameter varies according to the class, the effect of this fade is the third parameter is the color
	cctransitionfade * fade = Cctransitionfade::create (2.0,secondscene::scene (), CCC3 (255,100,100));
	Ccdirector::shareddirector ()->replacescene (fade);
	The following methods can also be used to
	//ccdirector::shareddirector ()->pushscene (fade);

Here's SecondScene.cpp.

#include "SecondScene.h" #include "HelloWorldScene.h" Ccscene * Secondscene::scene () {ccscene * scene = NULL;
		do {scene = Ccscene::create ();

		Cc_break_if (!scene);
		Secondscene * layer = secondscene::create ();

		Cc_break_if (!layer);
	Scene->addchild (layer);

	while (0);
return scene;

	BOOL Secondscene::init () {bool BRet = false; do {cc_break_if (!

		Cclayer::init ());
		Cclabelttf * TTF = Cclabelttf::create ("A-Scene", "Arial", 32);
		Ccmenuitemlabel * Labelmenu = Ccmenuitemlabel::create (Ttf,this,menu_selector (Secondscene::changescene));
		Ccmenu * menu = ccmenu::create (labelmenu,null);

		This->addchild (menu);
	BRet = true;

	while (0);
return bRet; } void Secondscene::changescene (Ccobject * sender) {//Use Cctransitionpageturn class need to set up camera Ccdirector::shareddirector () First-&
	Gt;setdepthtest (TRUE); The third argument is false to page forward, and true to page backward Cctransitionpageturn * Pageturn = Cctransitionpageturn::create (2.0,helloworld::
	Scene (), false); Ccdirector::shareddirector ()->rEplacescene (Pageturn);
There is no special effect with the following method//ccdirector::shareddirector ()->popscene (); }

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.