COCOS2DX Learning Path----Tenth chapter (Node node life cycle detailed)

Source: Internet
Author: User
Tags addchild what callback

in this article, we'll look at the lifecycle of node nodes.

The callback for the node's life cycle, also called the callback event. When a node is manipulated, such as being added or removed, it will invoke some of its own event methods by default. Now let's look at what callback methods belong to its events, as follows:

virtual void OnEnter ();//Call virtual void onentertransitiondidfinish () when added to a node in the scene;//Call virtual void when the node is added to the scene Onexittransitiondidstart ();//The virtual void OnExit () is called when the node in the scene is started to be removed, or the virtual void Cleanup () is called after the node in the scene is removed;// Stop all node states that are running in the scene
The above is the life cycle callback function of the node.

OK, now let's take a look at how to test it this time.

Since the basic display classes are related to nodes, I used the Layercolor class to test (don't ask me why every Test example has layercolor this class, haha, just because I think it is more suitable and intuitive, The key point is that it can instead of the sprite display to avoid the use of texture ~ No more said Ha, continue to continue ~)

Then since it was added and removed, I used the scene switch to do this test. As to how to be able to see the state of the switch, in fact, initially I want to use a print log method to detect the state of the node, but it seems that the effect is not very obvious, and there is no split line, when the switch may state a lot of words, and the last state output may be confused, So I chose Microsoft's one function interface MessageBox. This can pop up a small window out, very convenient. Then there are two parameters, the first one is the window content, the second is the title of the window. Well, let's take a look at the test now.

First Test: Go into the scene, initialize the layer and add, rewrite the life cycle callback function of the layer and detect it. Let's look at the effect first:


With Test 1, we can see the Layer1 life cycle callback method as follows:

Into the example of Layer1 created scene: Layer1:init---layer1:onenter---Layer1:onenterdidfinish

Close Layer1 Scene Current application: Layer1:onexit-Layer1:cleanup

OK, let's take a look at Test 2:


There are two scenarios in Test 2, Layer1 and Layer2, where scenes are toggled to check the Layer1 and Layer2 life cycle callback functions. We know that there are two ways to switch scenes, one is to switch through the Director class Singleton call Replacescene () method, and the other is to switch by directing the class call Pushscene () and Popscene (). The difference between these two methods is whether the resources of the previous scene were released when the switch was completed. So, Test 3 is also the method of scene switching to test, the way to switch to use the second type.

Well, then go back to the question in this Test 2, where the Replacescene switching method is used to test the callback process from above:

Enter Layer1:

Layer1:init->layer1:onenter->layer1:onentertransitiondidfinish

Enter Layer2:

Layer2:init->layer1:onexittransitiondidstart->layer1:onexit-> layer1:cleanup->layer2:onenter-> Layer2:onentertransitiondidfinish

Re-entry Layer1:

Layer1:init->layer2:onexittransitiondidstart->layer2:onexit->layer2:cleanup->layer1:onenter-> Layer1:onentertransitiondidfinish

To exit the application:

Layer1:oneix->layer1:cleanup


Finally, let's take a look at the third Test:


Test 3 here through Pushscene () and Popscene () to test, just look at their callback process:

Enter Layer1:

Layer1:init->layer1:onenter->layer1:onentertransitiondidfinish

Enter Layer2:

Layer2:init->layer1:onexittransitiondidstart->layer1:onexit->layer2:onenter->layer2:o Nenterdidfinish

Re-entry Layer1:

Layer2:onexittransitionstart->layer2:onexit->layer2:cleanup->layer1:onenter->layer1:o Nentertransitiondidfinish

To exit the application:

Layer1:oneix->layer1:cleanup


Well, the above is the rewrite and test of the node life cycle callback function. The following is directly posted here to test the source bar:

NodeLifeCircleTest.h:

#ifndef __node_life_circle_test__#define __node_life_circle_test__#include "Cocos2d.h" Using_ns_cc;class TestLayer1 :p ublic layercolor{public:static Scene *createscene (); virtual bool init (); Create_func (TestLayer1);//life cycle event callback overrides virtual void OnEnter ();//called virtual void Onentertransitiondidfinish when added to a node in the scene ( )////is called virtual void Onexittransitiondidstart () When the node is added to the scene, or//When you start to remove a node from the scene, you call virtual void onExit ();// Call virtual void Cleanup () After the node in the scene is removed,//Stop all node states running in the scene};class TestLayer2:p ublic layercolor{public:static Scene * Createscene (); virtual bool init (); Create_func (TestLayer2);//life cycle event callback rewrite virtual void OnEnter (); virtual void onentertransitiondidfinish (); virtual void OnExit (); virtual void Onexittransitiondidstart (); virtual void Cleanup ();}; #endif
NodeLifeCircleTest.cpp:

#include "NodeLifeCircleTest.h"/*************************************************** Description: LayerColor1 correlation function Definition ******** /scene *testlayer1::createscene () {Auto Scene = scene::create (); Auto layer = Testlayer1::create (); Scene->addchild (layer); return scene;} BOOL Testlayer1::init () {if (! Layercolor::initwithcolor (Color4b::green)) {return false;} MessageBox ("Init ...", "TestLayer1"), Auto visiblesize = Director::getinstance ()->getvisiblesize ();//Current test label Description Auto Test_label = Label::createwithsystemfont ("Node Life Circle Test", "", "), Test_label->setposition (VEC2 ( VISIBLESIZE.WIDTH/2, Visiblesize.height-test_label->getcontentsize (). height); This->addchild (Test_label) ;//Jump Menu Auto EnterMenuItem1 = Menuitemlabel::create (Label::createwithsystemfont ("This is the Layer1 \nclick here Enter Layer A. "," "," (), [] (Ref *r) {director::getinstance ()->replacescene (Testlayer2::createscene ());//director:: GetInstance ()->pushscene (Testlayer2::createscene ());}); Auto ENterLayer2 = Menu::create (EnterMenuItem1, NULL); This->addchild (EnterLayer2); return true;} TestLayer1 life cycle Event callback override void Testlayer1::onenter () {//Call parent class Layercolor::onenter (); Cclog ("TestLayer1 onEnter ..."); MessageBox ("OnEnter ...", "TestLayer1");} void Testlayer1::onentertransitiondidfinish () {//Call parent class layercolor::onentertransitiondidfinish (); Cclog ("TestLayer1 onentertransitiondidfinish ..."); MessageBox ("Onentertransitiondidfinish ...", "TestLayer1");} void Testlayer1::onexit () {//Call parent class layercolor::onexit (); Cclog ("TestLayer1 onExit ..."); MessageBox ("OnExit ...", "TestLayer1");} void Testlayer1::onexittransitiondidstart () {//Call parent class Layercolor::onexittransitiondidstart (); Cclog ("TestLayer1 onexittransitiondidstart ..."); MessageBox ("Onexittransitiondidstart ...", "TestLayer1");} void Testlayer1::cleanup () {//Call parent class Layercolor::cleanup (); Cclog ("TestLayer1 cleanup ..."); MessageBox ("Cleanup ...", "TestLayer1");} /*************************************************** Description: LayerColor2 correlation function Definition *************************************** *****/scene *testlayer2::createscene () {Auto Scene = scene::create (); Auto layer = Testlayer2::create ();scene-> AddChild (layer); return scene;} BOOL Testlayer2::init () {if (! Layercolor::initwithcolor (color4b::red)) {return false;} MessageBox ("Init ...", "TestLayer2"), Auto visiblesize = Director::getinstance ()->getvisiblesize ();//Current test label Description Auto Test_label = Label::createwithsystemfont ("Node Life Circle Test", "", "), Test_label->setposition (VEC2 ( VISIBLESIZE.WIDTH/2, Visiblesize.height-test_label->getcontentsize (). height); This->addchild (Test_label) ;//Jump Menu Auto EnterMenuItem2 = Menuitemlabel::create (Label::createwithsystemfont ("This is the Layer2 \nclick here Enter Layer One ... "," "," (), [] (Ref *r) {director::getinstance ()->replacescene (Testlayer1::createscene ());//director:: GetInstance ()->popscene ();}); Auto EnterLayer1 = Menu::create (EnterMenuItem2, NULL); This->addchild (EnterLayer1); return true;} TestLayer2 life cycle Event callback override void Testlayer2::onenter () {//Call parent class Layercolor::onenter (); Cclog("TestLayer2 onEnter ..."); MessageBox ("OnEnter ...", "TestLayer2");} void Testlayer2::onentertransitiondidfinish () {//Call parent class layercolor::onentertransitiondidfinish (); Cclog ("TestLayer2 onentertransitiondidfinish ..."); MessageBox ("Onentertransitiondidfinish ...", "TestLayer2");} void Testlayer2::onexit () {//Call parent class layercolor::onexit (); Cclog ("TestLayer2 onExit ..."); MessageBox ("OnExit ...", "TestLayer2");} void Testlayer2::onexittransitiondidstart () {//Call parent class Layercolor::onexittransitiondidstart (); Cclog ("TestLayer2 onexittransitiondidstart ..."); MessageBox ("Onexittransitiondidstart ...", "TestLayer2");} void Testlayer2::cleanup () {//Call parent class Layercolor::cleanup (); Cclog ("TestLayer2 cleanup ..."); MessageBox ("Cleanup ...", "TestLayer2");}


Well, if there's anything you don't understand, run the test and understand it more. It can be helpful to understand the life cycle of a node if it is understandable or useful for future development. For example, when the game enters the scene to play the sound, the end of the game to switch the interface when the sound processing should be appropriate in which callback function to add or stop sound playback, this is a very important question to think about. If you understand this, I believe you have a bottom of the heart ha ~ since speaking of sound, the next article also to talk about the load of the sound of the problem ha ~





COCOS2DX Learning Path----Tenth chapter (Node node life cycle detailed)

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.