Study Notes for Cocos2d-x (3) "the father of everything" -- ccnode

Source: Internet
Author: User
Tags addchild

Cocos2d-x game

Original article, reproduced please indicate the source: http://blog.csdn.net/sfh366958228/article/details/38701767


Through the first two study notes, we can easily find that ccscene, cclayer, ccsprite, ccaction, and other elements are child classes of ccnode. However, the title of "father of everything" is a bit exaggerated, after all, there are components such as ccdirector and cccamera that do not inherit from ccnode.

But ccnode is definitely a core of Cocos2d-x, we can understand it as a node. It is an abstract class that cannot be visually displayed. It is only used to define the public attributes and methods of all nodes.

 

Features

1) each node can use the addchild method to include other nodes as sub-nodes, or use removechild to remove sub-nodes. Ccnode is like a free tree.

2) each sub-node can be configured with a settag to obtain the sub-node through getchildbytag.

3) each node can execute scheduled tasks to process them in a system loop of the Cocos2d-x.

4) each node can perform instant or delayed actions through runaction.

5) each node is added to the scene. When the scene is activated, the drawing method of this node is automatically called to complete self-drawing.


Attribute
Class cc_dll ccnode: Public ccobject {public: ccnode (void); Virtual ~ Ccnode (void); // initialize the virtual bool Init () node; // create a Node object static ccnode * Create (void); // obtain a description string, easy to debug const char * description (void); // you can set/obtain the Z axis coordinate virtual void setzorder (INT zorder); Virtual int getzorder (); /*** sets the Z order which stands for the drawing order *** this is an internal method. don't call it outside the framework. * The difference between setzorder (INT) and _ setorder (INT) is: *-_ setzorder (INT) is a pure setter for m_nzorder memeber variable *-setzorder (INT) firstly changes m_nzorder, then recorder this node in its parent's chilren array. */virtual void _ setzorder (int z); // you can call this operation to obtain the real Z axis coordinate of OpenGL. Virtual void setvertexz (float vertexz); Virtual float getvertexz (); // set/obtain the zooming coefficient of the X axis virtual void setscalex (float fscalex); Virtual float getscalex (); // set/obtain the zooming coefficient of the Y axis virtual void setscaley (float fscaley ); virtual float getscaley (); // sets/obtains the scaling coefficient virtual void setscale (float scale); Virtual float getscale (); // sets the scaling coefficient. Virtual void setscale (float fscalex, float fscaley); // sets/obtains the node coordinate virtual void setposition (const ccpoint & position); Virtual const ccpoint & getposition (); // set the node coordinate virtual void setposition (float X, float y); // obtain the node coordinate to transmit the parameter virtual void getposition (float * X, float * y ); // set/obtain the Y axis coordinate of the X axis. These methods are used to bind virtual void setpositionx (float X), virtual float getpositionx (void), and virtual void setpositiony (float y) to Lua and JavaScript ); virtual float getpositiony (void); // you can call this operation to obtain the virtual void setskewx (float fskewx) and virtual float getskewx (); // set/obtain the virtual void setskewy (float fskewy); Virtual float getskewy (); // set/obtain the virtual void setanchorpoint (const ccpoint & anchorpoint ); virtual const ccpoint & getanchorpoint (); // obtain the virtual const ccpoint & getanchorpointinpoints () on the absolute coordinate (); // set/obtain the node size virtual void setcontentsize (const ccsize & contentsize); Virtual const ccsize & getcontentsize () const; // set/obtain the node visibility virtual void setvisible (bool visible); Virtual bool isvisible (); // set/obtain the node Rotation Angle virtual void setrotation (float frotation ); virtual float getrotation (); // sets/obtains the Rotation Angle of the X axis of a node. Virtual void setrotationx (float frotaionx); Virtual float getrotationx (); // set/obtain the node Y axis rotation angle virtual void setrotationy (float frotationy); Virtual float getrotationy (); /*** sets the arrival order when this node has a same zorder with other children. ** a node which called addchild subsequently will take a larger arrival order, * If two children have the same Z order, the child with larger arrival order will be drawn later. ** @ warning this method is used internally for zorder sorting, don't change this manually ** @ Param uorderofarrival the arrival order. */virtual void setorderofarrival (unsigned int uorderofarrival); Virtual unsigned int getorderofarrival ();/*** sets the state of OpenGL server side. ** @ Param glserverstate the state of OpenGL server side. * @ JS na */virtual void setglserverstate (ccglserverstate glserverstate); Virtual ccglserverstate getglserverstate ();/*** sets whether the anchor point will be (0, 0) When you position this node. ** this is an internal method, only used by cclayer and ccscene. don't call it outside framework. * The default value is false, while in cclayer and ccscene are true ** @ Param ignore true if anchor point will be (0, 0) when you position this node * @ todo this method shoud be renamed as either (bool) or something with "set" */virtual void ignoreanchorpointforposition (bool ignore); Virtual bool isignoreanchorpointforposition (); // Add the subnode virtual void addchild (ccnode * child); // Add the subnode, and set the subnode Z axis virtual void addchild (ccnode * child, int zorder ); // Add a subnode, and set the subnode Z axis coordinates and the subnode tag virtual void addchild (ccnode * child, int zorder, int tag ); // obtain the virtual ccnode * getchildbytag (INT tag) of the subnode through the tag; // obtain all the subnodes of the current node virtual ccarray * getchildren (); // obtain the current number of subnodes virtual unsigned int getchildrencount (void) const; // set/obtain the current node parent node virtual void setparent (ccnode * parent ); virtual ccnode * getparent (); // remove the current node from the parent node virtual void removefromparent ();/*** removes this node itself from its parent node. * If the node orphan, then nothing happens. * @ Param cleanup true if all actions and callbacks on this node shocould be removed, false otherwise. * @ JS removefromparent */virtual void removefromparentandcleanup (bool cleanup); // remove the virtual void removechild (ccnode * Child) of the subnode; // remove the subnode, check whether the virtual void removechild (ccnode * child, bool cleanup) of the current node is clear; // remove the virtual void removechildbytag (INT tag) of the subnode through the tag ); // remove sub-nodes by TAG, and set whether the virtual void removechildbytag (INT tag, bool cleanup) of the node is clear; // remove all sub-nodes virtual void removeallchildren (); // remove all child nodes and set whether the virtual void removeallchildrenwithcleanup (bool cleanup) of the current node is clear; // reset the zaxis coordinate virtual void reorderchild (ccnode * child, int zorder); // sort all subnodes by virtual void sortallchildren (); // obtain/set the Grid Object virtual ccgridbase * getgrid (); Virtual void setgrid (ccgridbase * pgrid ); // obtain/set the tag ID virtual int gettag () const; virtual void settag (INT ntag); // obtain/set the virtual void * getuserdata () of user data (); virtual void setuserdata (void * puserdata); // obtain/set the user data object virtual ccobject * getuserobject (); Virtual void setuserobject (ccobject * puserobject ); /*** return the shader program currently used for this node ** @ return the shader program currelty used for this node */virtual ccglprogram * getshaderprogram (); virtual void setshaderprogram (ccglprogram * pshaderprogram); // obtain the camera object virtual cccamera * getcamera (); // obtain whether the current node is running Virtual bool isrunning (); // register/cancel the registration script handle Virtual void registerscripthandler (INT handler); Virtual void unregisterscripthandler (void); // obtain the script handle inline int getscripthandler () {return m_nscripthandler ;}; /*** schedules for Lua script. * @ JS na */void scheduleupdatewithprioritylua (INT nhandler, int priority); // call virtual void onenter () when the node enters (); // call virtual void onentertransitiondidfinish () when the node ends the animation; // call virtual void onexit () when the node exits (); // call virtual void onexittransitiondidstart () when the node exits the animation; // stop all running animations and schedule virtual void cleanup (void ); // refactor this method to draw your own node virtual void draw (void); // recursively traverse the current node tree virtual void visit (void ); /*** returns a "local" axis aligned bounding box of the node. * The returned box is relative only to its parent. ** @ note this method returns a temporaty variable, so it can't returns const ccrect & * @ todo Rename to getboundingbox () in the future versions. ** @ return a "local" axis aligned boudning box of the node. * @ JS getboundingbox */virtual ccrect boundingbox (void); // sets/gets an actionmanager virtual void setactionmanager (ccactionmanager * actionmanager) of the current node; virtual ccactionmanager * getactionmanager (); // execute action ccaction * runaction (ccaction * action); // stop all actions void stopallactions (void); // stop the specified action void stopaction (ccaction * action ); // stop/get action void stopactionbytag (INT tag) through tag; ccaction * getactionbytag (INT tag ); /*** returns the numbers of actions that are running plus the ones that are schedule to run (actions in actionstoadd and actions arrays ). ** composable actions are counted as 1 action. example: * if you are running 1 sequence of 7 actions, it will return 1. * If you are running 7 sequences of 2 actions, it will return 7. * @ todo Rename to getnumberofrunningactions () ** @ return the number of actions that are running plus the ones that are schedule to run */unsigned int numberofrunningactions (void ); // set/obtain virtual void setscheduler (ccscheduler * Scheduler); Virtual ccscheduler * getscheduler ();/*** checks whether a selector is scheduled. ** @ Param selector a function selector * @ return whether the funcion selector is scheduled. * @ JS na * @ Lua na */bool isscheduled (sel_schedule selector);/*** schedules the "Update" method. ** it will use the order number 0. this method will be called every frame. * Scheduled methods with a lower order value will be called before the ones that have a higher order value. * Only One "Update" method cocould be scheduled per node. * @ Lua na */void scheduleupdate (void);/*** schedules the "Update" method with a custom priority. ** this selector will be called every frame. * Scheduled methods with a lower priority will be called before the ones that have a higher value. * Only One "Update" selector cocould be scheduled per node (you can't have 2 'update' selectors ). * @ Lua na */void scheduleupdatewithpriority (INT priority);/** unschedules the "Update" method. * @ see scheduleupdate (); */void unscheduleupdate (void);/*** execute a task ** @ Param interval tick interval in seconds. 0 means tick every frame. if interval = 0, it's recommended to use scheduleupdate () instead. * @ Param repeat the selector will be excuted (Repeat + 1) times, you can use kccrepeatforever for tick infinitely. * @ Param delay the amount of time that the first tick will wait before execution. * @ Lua na */void schedule (partition selector, float interval, unsigned int repeat, float delay); void schedule (partition selector, float interval); void scheduleonce (sel_schedule selector, float delay); void schedule (sel_schedule selector); // cancel the task void unschedule (sel_schedule selector); // cancel all tasks void unscheduleallselectors (void ); // resume/pause node actions and task void resumeschedulerandactions (void); void pauseschedulerandactions (void);/** update method will be called automatically every frame if "scheduleupdate" is called, and the node is "live" */virtual void Update (float delta);/*** performs OpenGL view-matrix transformation based on position, scale, rotation and other attributes. */void transform (void);/*** performs OpenGL view-matrix transformation of It's ancestors. * generally the ancestors are already transformed, but in certain cases (eg: attaching a FBO) * it's necessary to transform the ancestors again. */void transformancestors (void);/*** callchildren's updatetransform () method recursively. ** this method is moved from ccsprite, so it's no longer specific to ccsprite. * As the result, you apply ccspritebatchnode's optimization on your customed ccnode. * e.g ., batchnode-> addchild (mycustomnode), while you can only addchild (sprite) before. */virtual void updatetransform (void);/*** returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates. * The matrix is in pixels. */virtual ccaffinetransform nodetoparenttransform (void);/*** returns the matrix that transform parent's space coordinates to the node's (local) space coordinates. * The matrix is in pixels. */virtual ccaffinetransform parenttonodetransform (void);/*** returns the world affine transform matrix. the matrix is in pixels. */virtual ccaffinetransform nodetoworldtransform (void);/*** returns the inverse world affine transform matrix. the matrix is in pixels. */virtual ccaffinetransform worldtonodetransform (void);/*** converts a point to node (local) space coordinates. the result is in points. */ccpoint converttonodespace (const ccpoint & worldpoint);/*** converts a point to World Space coordinates. the result is in points. */ccpoint converttoworldspace (const ccpoint & nodepoint);/*** converts a point to node (local) space coordinates. the result is in points. * treating the returned/received node point as anchor relative. */ccpoint converttonodespacear (const ccpoint & worldpoint);/*** converts a local point to World Space coordinates. the result is in points. * treating the returned/received node point as anchor relative. */ccpoint converttoworldspacear (const ccpoint & nodepoint);/*** convenience methods which take a cctouch instead of ccpoint */ccpoint converttouchtonodespace (cctouch * Touch ); /*** converts a cctouch (World coordinates) into a local coordinate. this method is AR (anchor relative ). */ccpoint converttouchtonodespacear (cctouch * Touch);/*** sets the additional transform. */void setadditionaltransform (const component & additionaltransform); // obtain the component cccomponent * getcomponent (const char * pname) const; // Add the component virtual bool addcomponent (cccomponent * pcomponent ); // remove the component virtual bool removecomponent (const char * pname) by name; // remove the component virtual bool removecomponent (cccomponent * pcomponent) by pointer ); // remove all components virtual void removeallcomponents ();}

The above methods are provided in ccnode. The methods in the public block mainly consist of the following parts:
1) read and write the attribute information displayed for the node
2) read and write the changed node attributes
3) how to manage subnodes
4) related methods for binding node data
5) related methods for node Lifecycle
6) methods related to ccaction for node Processing
7) related methods for node scheduled tasks
8) related methods for node Coordinate Transformation

Each node in ccnode has its own coordinate system and becomes a node coordinate system. After the node coordinate system is changed, all the subnodes of the node are changed accordingly.
When a node is added to the scene, it will refer to the node's anchor. By default, the anchor is defined as the center of the node. During the map, the anchor is centered. Changing the anchor does not change the location of the node, only changed the location of the texture,
The world coordinate system of the Cocos2d-x is the same as the coordinate system of OpenGL, which is a lower left corner of the 0, 0 points, the X axis to the right, the Y axis to the up.

Okay, this is the end of this section. The next section will learn about cclayer ~

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.