When the enabled property of the component turns from true to false , or the active property of the node turns from true to false , it will trigger onEnable callback, it will activate the onDisable callback.
When the component or node calls destroy , it will call the onDestroy callback. Then they will be collected when this frame is done. When you declare both onLoad and onDestroy , they will always be called in pairs, which means that from component's initialization to destruction, they will either all be called or none will be called.
Where onLoad and start are often used for a component's initialization and will only be executed once when the node become activeInHierarchy. In addition to the content mentioned above and the different execution order, there are the following differences:. Lifecycle Callback. Life cycle callback Cocos Creator provides the life cycle callback function for component script. When we tell the director to replace the scene with a different scene, or to end the scene, all children objects are recursively destroyed.
Cocos2d-x Node subclasses including Sprite and Layer use a factory method called create or createWith that returns an instance of the class. That factory method encapsulates the use of the keyword new to dynamically allocate an instance of the class. When the scene is destroyed, all nodes are recursively also destroyed and delete is called on all instances of node.
A more detailed explanation of Cocos2d-x memory management was provided in part one of this article under theCocos2d-x Memory Management. The Node class is a foundational class in Cocos2d-x. Most classes that you create will be subclasses of Node, Sprite or Layer. Sprite and Layer are both subclasses of Node.
You then implement the init method in your. The superclass will call the init method for you. The onEnter, onEnterTransitionDidFinish, and onExit methods can be overridden in your implementation if you want to use them.
They are called as their name implies. The pragma once preprocessor command is used to ensure that this code is only included once into the program when the app is compiled. You should always add that at the top of each header file that you create. The constructor and destructor are declared private. This is done so that it is not possible for a user of this class to try to create the class using the constructor. This is a Cocos2d-x thing because Cocos2d-x takes care of a lot of memory management for us, which is a nice thing to have.
Any subclass of Node can be added to another Node. Then that parent node superclass will take care of removing that child node from your game when the parent class is no longer needed by the game. The MyClassName. Note the include directive that is used to include the MyClassName.
All implementation. The init method is where the majority of your game initialization should be conducted. You can organize your code into other methods in this class, such as the arbitrarily named method below doSomeSetup, or in other objects. But it is a good idea to initialize things from the init method. The second most common location to init an object is the onEnter or onEnterTransitionDidFinish methods that are inherited from Node and called by the Node superclass.
Notice the onEnter method and how the superclass Node code is called by using the name of the class followed by two colons and then the name of the method. This is also the syntax for how static methods are called. Using the above code, you can then create an instance of MyClassName in some other class like this. The type is inferred from variable assignment. If the myClassInstance was created inside of another Node, Layer, Scene, Sprite, or any subclass of Node, then you could add the myClassInstance node as a child to the parent node like this.
As noted above, when we add a node subclass to the scene graph, then the scene graph will take care of destroying that node subclass for us. The destructor of our Node subclass will still be called so that we can do any cleanup as necessary. The virtual keyword can be used to tell the compiler to prefer a derived method instead of the method in the base class.
One common use of this is when defining an abstract base class for use in the delegation pattern. An abstract base class is essentially just an interface definition.
If a class inherits that abstract base class, it is essentially just inheriting that interface and must provide an implementation for that interface by defining those methods in the header and. The delegate pattern is a great way to decouple code into reusable classes that stand alone. Take for example a race car class called RaceCar. The RaceCar needs a gas and brake pedal. Generally, developers will have a Game. If there are three components Configuration. Among them, the init method needs to be implemented in Configuration.
In this way, it is guaranteed the initialization sequence of Configuration , GameData and Menu. Similarly, it is necessary to ensure the update order of each frame of the above three scripts, we can also replace the update scattered in each script with our own defined method:. The execution order of component scripts on the same node can be controlled by the order of the components in the Inspector panel.
The components arranged above will be executed before the components arranged below. We can adjust the arrangement order and execution order of the components through the Move Up and Move Down menus in the gear button at the upper right corner of the component. If there are two components: CompA and CompB , their contents may be similar to this example:.
0コメント