Cocos2d-x Study Notes (3) CCNode analysis, cocos2d-xccnode

Source: Internet
Author: User

Cocos2d-x Study Notes (3) CCNode analysis, cocos2d-xccnode

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


Using the previous two study notes, we can easily find that CCScene, CCLayer, CCSprite, and other elements are child classes of 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);/*** set/obtain the Z axis coordinate ** zOrder is independent of the drawing sequence, it only records the order of node in its parent class and related brothers. The sequence is relative to the Child class of its parent class, it has nothing to do with Z vertex of OpenGl * default Z vertex = 0. it only affects the order of nodes painting. The larger the number, the closer the drawing is to the back */virtual void setZOrder (int zOrder); virtual int getZOrder (); // sets the Z axis coordinate, the difference between setZOrder and setZOrder is that setZOrder first sets m_nZOrder and then reenters the self-node array of the parent class. 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/gets the zoom coefficient virtual void setScale (float scale); virtual float getScale (); // sets the zoom coefficient virtual void setScale (float fSc AleX, float fScaleY); // sets/gets 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 getPo SitionY (void); // set/obtain the X axis distortion angle virtual void setSkewX (float fSkewX); 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 virtual void setContentSize (const CCSize & co NtentSize); virtual const CCSize & getContentSize () const; // set/get node visibility virtual void setVisible (bool visible); virtual bool isVisible (); // set/obtain the node Rotation Angle virtual void setRotation (float fRotation); virtual float getRotation (); // set/obtain the node X axis rotation angle virtual void setRotationX (float fRotaionX ); virtual float getRotationX (); // sets/gets the node's Y axis rotation angle virtual void setRotationY (float fRotationY); virtual float getRotati OnY ();/*** set/get arrival order ** after a node calls addChild, it will get a larger arrival order. * If two subnodes have the same Z order, then the arrival order will be drawn later */virtual void setOrderOfArrival (unsigned int uOrderOfArrival); virtual unsigned int getOrderOfArrival (); // set/obtain the state of the OpenGL Server virtual void setGLServerState (ccGLServerState glServerState); virtual ccGLServerState getGLServerState ();/*** set/get when you set the Coordinate Position of the node, whether the anchor is treated as (0, 0) ** when set to true The anchor is still (0.5, 0.5), but when it is set to false through the starting point (0, 0), it is located through the anchor (0.5, 0.5) * if it is true, although the positioning is based on the starting point (0, 0) * but for scale, it is still through the anchorpointforposition (bool ignore ); virtual bool isIgnoreAnchorPointForPosition (); // Add a subnode virtual void addChild (CCNode * child); // Add a subnode, and set the subnode Z axis coordinate virtual void addChild (CCNode * child, int zOrder); // add a subnode, and set the zaxis coordinates of the subnode and the virtual void add Tag of the subnode. Child (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 number of current subnodes virtual unsigned int getChildrenCount (void) const; // set/obtain the virtual void setParent (CCNode * parent) of the parent node of the current node; virtual CCNode * getParent (); // remove the virtual void removeFromParent () from the parent node (); // remove this node from the parent class and clear the current node. When cleanup is set to true, the action will be stopped, including the act of the subclass. Ion also stops virtual void removeFromParentAndCleanup (bool cleanup); // removes the virtual void removeChild (CCNode * child) sub-node; // removes the sub-node, and set whether to clear the virtual void removeChild (CCNode * child, bool cleanup) of the current node; // remove the virtual void removeChildByTag (int Tag) of the subnode through the tag ); // remove sub-nodes by Tag and set whether to clear the virtual void removeChildByTag (int tag, bool cleanup) of the current node; // remove all sub-nodes virtual void removeAllChildren (); // remove all child nodes and set whether the virtual voi of the current node is clear D removeAllChildrenWithCleanup (bool cleanup); // reset the Z axis coordinate virtual void reorderChild (CCNode * child, int zOrder) of a subnode ); // sort all subnodes by virtual void sortAllChildren (); // obtain/set the Grid Object virtual CCGridBase * getGrid (); virtual void setGrid (CCGridBase * pGrid ); // obtain/set the Tag identifier virtual int getTag () const; virtual void setTag (int nTag); // obtain/set user data, which can be put into pointers, data blocks, and struct, objects. Note that the release virtual void * getUserData (); virtual Void setUserData (void * pUserData); // obtain/Set User Data Objects. You can put CCObject data. The added CCObject requires release virtual CCObject * getUserObject (); virtual void setUserObject (CCObject * pUserObject);/The *** engine provides the CCGLProgram class to process coloring machine operations and encapsulate the current plotting program, * The most frequently used method should be the const GLuint getProgram () interface for obtaining the colorant program. * This interface returns the identifier of the current colorant program. We will see later that when operating OpenGL, we often need to set different coloring program. * Note: Here, an unsigned integer identifier is returned, instead of a pointer or structure reference. This is a style of the OpenGL interface. * Objects (textures, coloring programs, or other non-standard types) are represented by integer identifiers. */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;}; // Handle (called by Lua) void scheduleUpdateWithPriorityLua (int nHandler, int priority) based on the priority ); // call virtual void onEnter () when the node enters the animation; // call virtual void onEnterTransitionDidFinish () when the node ends (); // 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); // you can use this method to draw your own node virtual void draw (void ); // recursively traverse the current node tree virtual void visit (void); // obtain the size of the external box after scaling and rotation virtual CCRect boundingBox (void ); /*** set/get an ActionManager for the current node ** when CCActionManager is added, the original actions will stop * When node Initialization is performed, CCActionManager uses director-> getActionManager () * Of course m_pActionManager is also retain () initialized, so all actions in runAction are managed by m_pActionManager of the current node */virtual void setActionManager (CCActionManager * actionManager ); 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 ); // get the total number of running actions unsigned int numberOfRunningActions (void); // set/obtain the task virtual void setScheduler (CCScheduler * scheduler); virtual CCScheduler * getScheduler (); // whether the scheduler bool isScheduled (SEL_SCHEDULE selector) is being executed; // The scheduler update method void scheduleUpdate (void); // Handle void scheduleUpdateWithPriority (int priority) is updated based on the priority ); // cancel the update plan void unscheduleUpdate (void);/*** execute a task ** @ param interval: the trigger interval. If interval is set to 0, we recommend that you use scheduleUpdate () instead of * @ param repeat times * @ param delay start time * @ lua NA */void schedule (SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay); void schedule (SEL_SCHEDULE selector, float interval); void schedule (SEL_SCHEDULE selector); // execute a single task void scheduleOnce (SEL_SCHEDULE selector, float delay ); // 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); // when scheduleUpdate is called and the node is active, this method automatically calls virtual void update (float delta) at each frame ); /*** 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

Nodes in CCNode have their own coordinate system and become the node coordinate system. When 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 position 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 CCScene ~
In the cocos2d-x, I have a class derived from CCNode, schedule in my derived class sets the timer, why not respond, solution,

This-> scheduleUpdate ();
Is this method not responding to your update (float dt?
In myNode, have you implemented onEnter ();?
If it is an empty implementation, You need to modify it by calling the onEnter () of CCNode ();
Hope to help you.

How to achieve the Cocos2d-x CCNode node touch?

It is recommended to look at the wheat college Cocos2d-x tutorial! There will be a solution! Mr. Long lingxiu said. Don't miss it!

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.