Cocos2d-x the life cycle of a scene

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************



Another year's graduation season ...
Graduate Seniors Learn to share a variety of experience--postgraduate, Kau Gang, training, Balabala ....
go ahead and be my cocos2d-x.


take a look at the life cycle of the scene today (this basic thing should not be noticed now O (╯-╰) o)
This life cycle, in fact, is very important, for example, when playing background music, when to play, when to stop, these we should understand.


1. Scene
in general, a scene requires only one layer. Layers need to be sub-typed, writing their own layer classes, and the scene does not need to subclass.
As a result, the life cycle of the scene is reflected through the life cycle of the layer, and by rewriting the life cycle functions of the layer, you can deal with events in different life cycle stages of the scene.
For example, you can do some initialization processing in the Layer entry function (onEnter), freeing some resources in the layer Exit function (OnExit).



2. Layer's life cycle function
> BOOL init () initialization layer invocation
> void OnEnter () Called when entering the layer
> void Onentertransitiondidfinish () enters the layer and is called at the end of the transition animation
> void Oneixt () Called when exiting a layer
> void Oneixttransitiondidstart () exits the layer and is called when the transition animation starts
> void Cleanup () Layer object is called when it is cleared
PS: Layers inherit from nodes, which are essentially inherited from node, that is, all node objects (layer, Scene, Sprite, etc.) have these life-cycle functions, so long as subclasses of these classes can override these functions.

the overriding of the INIT function is more common (it should be overridden = =). )
In the case of HelloWorld, for example:
In the header file is:

  virtual BOOL init ();

in the source file is:

BOOL Helloworld::init () {  if (! Layer::init ())  {    return false;  }  ...           What to write and what to write  return true;}

to an uncommon, overriding onenter function,
In the header file:

virtual void onEnter ();

in the source file:

void Helloworld::onenter () {  layer::onenter ();  ...           Add what you want to add, such as the play of the background music ~}  

The rest are the same, do you understand the routine? Add your own changes to the call parent class, virtual declaration
It is important to remember that the corresponding function of the parent class is implemented first, and if not called, the animations, actions, schedule, and so on in the layer may not be executed.

is the order in which we perform the HelloWorld scenario:






3. Multi-scene switching life cycle
Just looked at a scene to start the order, now look at the multi-scene switch, the life cycle is what it looks like?
There are three scenarios in which the scene is switched:
? Use the Replacescene function to switch from one scene to another
? Use the Pushscene function to move the current scene into the scene stack and then switch to another scene
? Use the Popscene function to end the current scene, pop the first scene from the scene stack, and switch to that scene


Assuming the current scene is HelloWorld, the next scene to switch to is Hellotree, the following three graphs to fully solve the above three cases:

? Use the Replacescene function to switch scenes


? Use the Pushscene function to switch scenes


? Use the Popscene function to switch scenes






*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

Cocos2d-x the life cycle of a scene

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.