Two major base classes of Cocos2d-x

Source: Internet
Author: User
Tags addchild

Cocos2d-x has two important base classes, a ref that manages reference counts, and a Node that defines many basic attributes.

The basic concept in cocos2d-x refers to the cocos2d-x memory management mechanism when it comes to the CREATE function. The memory management mechanism of COCOS2D-X draws on the memory management mechanism of OC, the implementation method is Ref this class (remove some unsightly things):

classref{ Public:    //retained    voidretain (); //Release    voidrelease (); //Automatic Managementref*autorelease (); //Get reference countUnsignedintGetreferencecount ()Const;protected: Ref (); Public:    Virtual~Ref ();protected:    //Reference CountUnsignedint_referencecount; FriendclassAutoreleasepool;};

The class is simple and has only one field:

unsigned int _referencecount;

Saves the number of references to the current object, initializes it to 1 when the object is created, and automatically calls the destructor to free memory when it equals 0;

The four functions are also very simple, retain release is in the original reference count +1, 1, save the object needs retain, do not need to save the release, the engine has encapsulated data container, you can avoid forgetting retain and release, so generally the use of.

ref* autorelease ();

Called only once when the object is created, added to the memory pool, automatically frees memory when the reference count is 0, and calls in the default Create function, as well as when you write create.

The last function is to get the current reference count, which is known as the name.

Ref is almost the parent of all classes, except for some simple data classes, so using ref as a parameter in many callback functions can be used to convert a dynamic type to its original type within a callback function.

Node is a much more informative class, picking out some common things to look at:

 class  Node: public   ref{ public  :  static  Node * create (); 
  virtual  void  addChild (Node * child);  virtual  void  addChild (Node * Child, int      Localzorder);  virtual  void  addChild (node* child, int  localzorder, int      tag);  virtual  void  addChild (node* child, int  localzorder, const  std::string  &name); 
 void scheduleupdate (void);           Virtual voidUpdatefloatDelta); Virtual voidOnEnter (); Virtual voidonExit ();protected:    //Nodes should be created using create ();Node (); Virtual~Node (); Virtual BOOLinit ();protected:    float_rotationx;//rotate to X axis     float_rotationy;//rotate to Y-axis         float_scalex;//indent X Y Z    float_scaley; float_scalez; float_SKEWX;//Tilt X Y    float_skewy;    VEC2 _position; //locationVEC2 _anchorpoint;//Anchor PointSize _contentsize;//size Size    int_localzorder;//Local Rendering Order    float_globalzorder;//Global Rendering Order    int_tag;//labelSTD::string_name;//name bool _running; // whether to run 
    BOOL //Yes No visible
  Vector  <Node*> _children; //child      node               Node *_parent; //  parent node                  Director  * _director; //     director               Scheduler *_scheduler; //     timer       ActionManager *_actionmanager; //     action manager   eventdispatcher* _eventdispatcher; //  listener manager } 

The above attributes basically have a set get method (there are a few below); rotation, contraction, tilt can change the shape of the node, position node in the screen position, the position of the parent node affects the position of the child nodes, the anchor point affects the effect of its own deformation; size of the node in the screen rectangle size, generally the same as the picture, The font size is the same, can be changed, the local rendering order affects the rendering order of the sibling nodes, the global impact of all nodes, two is a large value, displayed on the above, the value of the small will be blocked to the global coverage of the local; tag, the name is used for marking, easy to find the node, the same function; There can be multiple, parent node only one, after the director, timer, Action Listener Manager in the game initialization is done, Node constructs reference these, reasonable use of these existing pointers can write simple, efficient code.

Attribute picked out there are so many, the corresponding method is more, here only take addChild for example, AddChild has four overloads, the necessary is node nodes, followed by the local rendering order, and finally with a tag or a name to mark, such a parameter order does not necessarily match you, nothing, With other functions to modify the line, you can also add two functions to Node, in the cocos2d-x start configuration (Windows platform) said that the new project will copy the entire engine, in a project to modify the source code does not affect other projects, if you feel that the added functionality is very useful, it is necessary to retain, You can submit your code on GitHub.

In addition, there are several useful functions

virtual void update (float delta);

Every frame call, something that needs to be processed in real time can override this function to put the required processing code here, but to call Scheduleupdate () in other functions, it will not execute.

virtual void onEnter ();

virtual void onExit ();

These are the two that are about to enter, leaving the node to be called, handling some run-time attitudes conveniently and automatically called.

These three functions need to be aware of the amount: calling the Update,onenter,onexit of the parent class in the rewritten code will cause unexpected problems.

The Create Init function starts with the node class, and the init of the parent class called in Init eventually comes to this class, and the init of the node class returns only true;

BOOL Node::init () {    returntrue;}
Ref is the base class for most classes to facilitate the management of memory, commonly used as function parameters, can be converted to the original type of node class provides a large number of attributes, virtual functions, improve code reuse, reduce the repetitive work of programmers

Two major base classes of Cocos2d-x

Related Article

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.