cocos2d-x游戏开发(九)重要的基类CCNode

欢迎转载:http://blog.csdn.net/fylz1125/article/details/8522523


这个CCNode是个很重要的基类,没有理由不把它搞一搞。

首先看下类结构图:

cocos2d-x游戏开发(九)重要的基类CCNode_第1张图片

它几乎是所有类的基类,官方注释如下:


CCNode是主要元素。任何一个能被绘制或者包含能被绘制的东西都是一个CCNode。

最常用的CCNode有:CCScene,CCLayer,CCSprite,CCMenu.

一个CCNode的主要特性包括:

1.他们能够容纳别的CCNode节点,别如能addChild, getChildByTag, removeChild 。

2.他们能定期的调度回调函数,比如能schedule,unschedule等。

3.他们能执行动作,比如runAciton,stopAction等。

一些节点能给自己或他们的子几点提供一些额外额功能。


继承一个CCNode节点通常意味着如下几条:

1.重写init()函数来初始化资源和回调

2.创建回调函数来处理时间片

3.重写draw来绘制节点

另外,一个CCNode是一个看不见对象,他没有纹理。每个节点都有一个Camera,默认指向节点的中心点。

看下这个类

class CC_DLL CCNode : public CCObject
{
protected:
    // rotation angle
    float m_fRotationX, m_fRotationY;
    
    // scaling factors 缩放因子
    float m_fScaleX, m_fScaleY;
    
    // openGL real Z vertex
    float m_fVertexZ;
    
    // position of the node
    CCPoint m_obPosition;
    
    // skew angles
    float m_fSkewX, m_fSkewY;
    
    // anchor point in points 锚点(points形式)
    CCPoint m_obAnchorPointInPoints;
    // anchor point normalized (NOT in points) 锚点(比例形式)
    CCPoint m_obAnchorPoint;
    
    // untransformed size of the node
    CCSize m_obContentSize;
    
    // transform
    CCAffineTransform m_sTransform, m_sInverse;
    
    // a Camera
    CCCamera *m_pCamera;
    
    // a Grid
    CCGridBase *m_pGrid;
    
    // z-order value
    int m_nZOrder;
    
    // array of children
    CCArray *m_pChildren;//关键数组,存子节点
    
    // weak ref to parent
    CCNode *m_pParent; // 父节点
    
    // a tag. any number you want to assign to the node
    int m_nTag; //tag标记
    
    // user data field
    void *m_pUserData;
    CCObject *m_pUserObject;
    
    // Shader
    CCGLProgram *m_pShaderProgram;// 着色器
    
    // Server side state
    ccGLServerState m_eGLServerState;
    
    // used to preserve sequence while sorting children with the same zOrder
    unsigned int m_uOrderOfArrival;
    
    // scheduler used to schedule timers and updates
    CCScheduler *m_pScheduler; //调度器,调度定时器执行一些函数
    
    // ActionManager used to handle all the actions
    CCActionManager *m_pActionManager; //动作管理器,处理所有动作
    
    // Is running
    bool m_bRunning; //状态标志
    
    bool m_bTransformDirty;
    bool m_bInverseDirty;
    
    // is visible
    bool m_bVisible; // 是否可见
    
    // If true, the Anchor Point will be (0,0) when you position the CCNode.
	// Used by CCLayer and CCScene
    bool m_bIgnoreAnchorPointForPosition;

    bool m_bReorderChildDirty;
    
    // Properties for script
    
    // script handler
    int m_nScriptHandler;
    int m_nUpdateScriptHandler;

    // script type, lua or javascript
    ccScriptType m_eScriptType;

public:
    // getter & setter
    
    /** The z order of the node relative to it's "brothers": children of the same parent */
    virtual int getZOrder(); // 取得z坐标,相对于同一父节点
    virtual void setZOrder(int nZOrder);

    /** The real openGL Z vertex.
     Differences between openGL Z vertex and cocos2d Z order:
     - OpenGL Z modifies the Z vertex, and not the Z order in the relation between parent-children
     - OpenGL Z might require to set 2D projection
     - cocos2d Z order works OK if all the nodes uses the same openGL Z vertex. eg: vertexZ = 0
     @warning: Use it at your own risk since it might break the cocos2d parent-children z order
     @since v0.8
     */
    virtual float getVertexZ();
    virtual void setVertexZ(float fVertexZ);

    /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the X scale factor. */
    virtual float getScaleX();
    virtual void setScaleX(float fScaleX);

    /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the Y scale factor. */
    virtual float getScaleY();
    virtual void setScaleY(float fScaleY);

    /** Position (x,y) of the node in OpenGL coordinates. (0,0) is the left-bottom corner. */
    virtual CCPoint getPosition();
    virtual void setPosition(const CCPoint &position);
    
    /** The X skew angle of the node in degrees.
     This angle describes the shear distortion in the X direction.
     Thus, it is the angle between the Y axis and the left edge of the shape
     The default skewX angle is 0. Positive values distort the node in a CW direction.
     */
    virtual float getSkewX();
    virtual void setSkewX(float fSkewX);
    
    /** The Y skew angle of the node in degrees.
     This angle describes the shear distortion in the Y direction.
     Thus, it is the angle between the X axis and the bottom edge of the shape
     The default skewY angle is 0. Positive values distort the node in a CCW direction.
     */
    virtual float getSkewY();
    virtual void setSkewY(float fSkewY);
    
    virtual CCArray* getChildren();
    
    /** A CCCamera object that lets you move the node using a gluLookAt
     */
    virtual CCCamera* getCamera();
    
    /** A CCGrid object that is used when applying effects */
    virtual CCGridBase* getGrid();
    virtual void setGrid(CCGridBase *pGrid);
    
    /** A tag used to identify the node easily */
    virtual int getTag();
    virtual void setTag(int nTag);
    
    /** A custom user data pointer */
    virtual void* getUserData();
    virtual void setUserData(void *pUserData);
    
    /** Similar to userData, but instead of holding a void* it holds an id */
    virtual CCObject* getUserObject();
    virtual void setUserObject(CCObject *pUserObject); //retain
    
    /** Shader Program
     @since v2.0
     */
    virtual CCGLProgram* getShaderProgram();
    virtual void setShaderProgram(CCGLProgram *pShaderProgram);
    
    /** used internally for zOrder sorting, don't change this manually */
    virtual unsigned int getOrderOfArrival();
    virtual void setOrderOfArrival(unsigned int uOrderOfArrival);
    
    /** GL server side state
     @since v2.0
     */
    virtual ccGLServerState getGLServerState();
    virtual void setGLServerState(ccGLServerState glServerState);
    
    /** CCActionManager used by all the actions.
     IMPORTANT: If you set a new CCActionManager, then previously created actions are going to be removed.
     @since v2.0
     */
    virtual CCActionManager* getActionManager();
    virtual void setActionManager(CCActionManager *pActionManager);
    
    /** CCScheduler used to schedule all "updates" and timers.
     IMPORTANT: If you set a new CCScheduler, then previously created timers/update are going to be removed.
     @since v2.0
     */
    virtual CCScheduler* getScheduler();
    virtual void setScheduler(CCScheduler *pScheduler);
    
    /** A weak reference to the parent */
    virtual CCNode* getParent();
    virtual void setParent(CCNode *pParent);
    
    /** anchorPoint is the point around which all transformations and positioning manipulations take place.
     It's like a pin in the node where it is "attached" to its parent.
     The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
     But you can use values higher than (1,1) and lower than (0,0) too.
     The default anchorPoint is (0.5,0.5), so it starts in the center of the node.
     @since v0.8
     */
    virtual CCPoint getAnchorPoint();
    virtual void setAnchorPoint(const CCPoint &anchorPoint);
    
    /** The anchorPoint in absolute pixels.
     Since v0.8 you can only read it. If you wish to modify it, use anchorPoint instead
     */
    virtual CCPoint getAnchorPointInPoints();
    
    /** The untransformed size of the node.
     The contentSize remains the same no matter the node is scaled or rotated.
     All nodes has a size. Layer and Scene has the same size of the screen.
     @since v0.8
     */
    virtual CCSize getContentSize();
    virtual void setContentSize(const CCSize &contentSize);

    virtual bool isVisible();
    virtual void setVisible(bool visible);
    
    /** Get the scale factor of the node.
     @warning: Assert when m_fScaleX != m_fScaleY.
     */
    virtual float getScale();
    /** The scale factor of the node. 1.0 is the default scale factor. It modifies the X and Y scale at the same time. */
    virtual void setScale(float scale);
    
    /** The rotation (angle) of the node in degrees. 0 is the default rotation angle. Positive values rotate node CW. */
    virtual float getRotation();
    virtual void setRotation(float fRotation);
    
    /** The rotation (angle) of the node in degrees. 0 is the default rotation angle. Positive values rotate node CW. It only modifies the X rotation performing a horizontal rotational skew . */
    virtual float getRotationX();
    virtual void setRotationX(float fRotaionX);
    /** The rotation (angle) of the node in degrees. 0 is the default rotation angle. Positive values rotate node CW. It only modifies the Y rotation performing a vertical rotational skew . */
    virtual float getRotationY();
    virtual void setRotationY(float fRotationY);

    /** whether or not the node is running */
    virtual bool isRunning();

    // If true, the Anchor Point will be (0,0) when you position the CCNode.
	// Used by CCLayer and CCScene
    virtual bool isIgnoreAnchorPointForPosition();
    virtual void ignoreAnchorPointForPosition(bool isIgnoreAnchorPointForPosition);

    /** Get children count */
    unsigned int getChildrenCount(void);
    
    void _setZOrder(int z);

    /** Get script handler for onEnter/onExit event. */
    inline int getScriptHandler() { return m_nScriptHandler; };
    
    /** get/set Position for Lua (pass number faster than CCPoint object)
     
     lua code:
     local pos  = node:getPositionLua() -- return CCPoint object from C++
     local x, y = node:getPosition()    -- return x, y values from C++
     local x    = node:getPositionX()
     local y    = node:getPositionY()
     node:setPosition(x, y)             -- pass x, y values to C++
     node:setPositionX(x)
     node:setPositionY(y)
     */
    const CCPoint& getPositionLua(void);
    void getPosition(float* x, float* y);
    float getPositionX(void);
    float getPositionY(void);
    void setPositionX(float x);
    void setPositionY(float y);
    void setPosition(float x, float y);

public:
    CCNode(void);

    virtual ~CCNode(void);

    const char* description(void);

	/** allocates and initializes a node.
     The node will be created as "autorelease".
     */
    static CCNode * create(void); // 自动释放对象构造器

    //scene management

    /** callback that is called every time the CCNode enters the 'stage'.
     If the CCNode enters the 'stage' with a transition, this callback is called when the transition starts.
     During onEnter you can't a "sister/brother" node.
     */
    virtual void onEnter(); //回调。如果节点进入时又过渡,那么在过渡开始时调度

    /** callback that is called when the CCNode enters in the 'stage'.
     If the CCNode enters the 'stage' with a transition, this callback is called when the transition finishes.
     @since v0.8
     */
    virtual void onEnterTransitionDidFinish();//回调,当过渡结束时调用

    /** callback that is called every time the CCNode leaves the 'stage'.
     If the CCNode leaves the 'stage' with a transition, this callback is called when the transition finishes.
     During onExit you can't access a sibling node.
     */
    virtual void onExit();//回调,节点离开时,如果有过渡,则在过渡结束时调用

    /** callback that is called every time the CCNode leaves the 'stage'.
     If the CCNode leaves the 'stage' with a transition, this callback is called when the transition starts.
     */
    virtual void onExitTransitionDidStart();// 回调,离开时,在过渡开始时调用

    /** Register onEnter/onExit handler script function
     
     Script handler auto unregister after onEnter().
     */
    virtual void registerScriptHandler(int nHandler);
    virtual void unregisterScriptHandler(void);

    // composition: ADD

    /** Adds a child to the container with z-order as 0.
     If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
     @since v0.7.1
     */
    virtual void addChild(CCNode * child);//添加一个子节点,默认z坐标为0.如果一个子节点被添加到一个正在运行的节点,那么这个子节点的onEnter和onEnterTransitionDidFinish会被立即调用

    /** Adds a child to the container with a z-order
     If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
     @since v0.7.1
     */
    virtual void addChild(CCNode * child, int zOrder);// 添加一个子节点,设置z坐标

    /** Adds a child to the container with z order and tag
     If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
     @since v0.7.1
     */
    virtual void addChild(CCNode * child, int zOrder, int tag);// 添加子节点,设置z坐标和tag标记

    // composition: REMOVE

    /** Remove itself from its parent node forcing a cleanup.
     If the node orphan, then nothing happens.
     @since v2.1
     */
    virtual void removeFromParent();//从父节点删除自己,如果没有父节点则什么都不干

    /** Remove itself from its parent node. If cleanup is true, then also remove all actions and callbacks.
     If the node orphan, then nothing happens.
     @since v0.99.3
     */
    virtual void removeFromParentAndCleanup(bool cleanup);// 从父节点删除自己,如果cleanup标记为true,则删除其所有动作和回调函数

    /** Removes a child from the container forcing a cleanup
     @since v2.1
     */
    virtual void removeChild(CCNode* child);// 删除一个子节点,强制cleanup为true

    /** Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
     @since v0.7.1
     */
    virtual void removeChild(CCNode* child, bool cleanup);

    /** Removes a child from the container by tag value forcing a cleanup.
     @since v2.1
     */
    virtual void removeChildByTag(int tag);

    /** Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter
     @since v0.7.1
     */
    virtual void removeChildByTag(int tag, bool cleanup);

    /** Removes all children from the container forcing a cleanup.
     @since v2.1
     */
    virtual void removeAllChildren();// 删除所有子节点及其动作和回调,即cleanup强置true

    /** Removes all children from the container and do a cleanup all running actions depending on the cleanup parameter.
     @since v0.7.1
     */
    virtual void removeAllChildrenWithCleanup(bool cleanup);

    // composition: GET
    /** Gets a child from the container given its tag
     @return returns a CCNode object
     @since v0.7.1
     */
    CCNode * getChildByTag(int tag);

    /** Reorders a child according to a new z value.
     * The child MUST be already added.
     */
    virtual void reorderChild(CCNode * child, int zOrder);

    /** performance improvement, Sort the children array once before drawing, instead of every time when a child is added or reordered
     don't call this manually unless a child added needs to be removed in the same frame */
    virtual void sortAllChildren();

    /** Stops all running actions and schedulers
     @since v0.8
     */
    virtual void cleanup(void); // 停止所有动作和任务调度

    // draw

    /** Override this method to draw your own node.
     The following GL states will be enabled by default:
     - glEnableClientState(GL_VERTEX_ARRAY);
     - glEnableClientState(GL_COLOR_ARRAY);
     - glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     - glEnable(GL_TEXTURE_2D);

     AND YOU SHOULD NOT DISABLE THEM AFTER DRAWING YOUR NODE

     But if you enable any other GL state, you should disable it after drawing your node.
     */
    virtual void draw(void);//画自己

    /** recursive method that visit its children and draw them */
    virtual void visit(void);//递归访问子节点并绘制它们

    // transformations
    // MARMALADE ADDED THIS... SO IT IS NO LONGER SPECIFIC TO CCSprite
    /** updates the quad according the the rotation, position, scale values. */
    virtual void updateTransform(void);

    /** 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.
     @since v0.7.2
     */
    void transformAncestors(void);

    /** returns a "local" axis aligned bounding box of the node.
     The returned box is relative only to its parent.

     @since v0.8.2
     */
    CCRect boundingBox(void);//返回节点的边框矩形区域,相对于其父节点

    // actions

    /** Executes an action, and returns the action that is executed.
     The node becomes the action's target.
     @warning Starting from v0.8 actions don't retain their target anymore.
     @since v0.7.1
     @return An Action pointer
     */

    CCAction* runAction(CCAction* action);// 执行一个动作。执行动作的节点将变成动作的target

    /** Removes all actions from the running action list */
    void stopAllActions(void);

    /** Removes an action from the running action list */
    void stopAction(CCAction* action);

    /** Removes an action from the running action list given its tag
     @since v0.7.1
     */
    void stopActionByTag(int tag);

    /** Gets an action from the running action list given its tag
     @since v0.7.1
     @return the Action the with the given 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.
     */
    unsigned int numberOfRunningActions(void);


    // timers

    /** check whether a selector is scheduled. */
    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 could be scheduled per node.

     @since v0.99.3
     */
    void scheduleUpdate(void);//采用0优先级调度update函数,这个函数每一帧都会被执行,优先级数字越小,权重越高,也就越优先调用。每个节点只能有一个update被调度。

    /** schedules the "update" selector with a custom priority. This selector will be called every frame.
     Scheduled selectors with a lower priority will be called before the ones that have a higher value.
     Only one "update" selector could be scheduled per node (You can't have 2 'update' selectors).

     @since v0.99.3
     */
    void scheduleUpdateWithPriority(int priority);// 用一个优先级调度update函数,每一帧都调用

    /* unschedules the "update" method.

     @since v0.99.3
     */
    void unscheduleUpdate(void);//停止调度update函数

    /** schedules a selector.
     The scheduled selector will be ticked every frame
     */
    void schedule(SEL_SCHEDULE selector);//选择器selector每一帧都被调一次

    /** schedules a custom selector with an interval time in seconds.
     If time is 0 it will be ticked every frame.
     If time is 0, it is recommended to use 'scheduleUpdate' instead.
     If the selector is already scheduled, then the interval parameter
     will be updated without scheduling it again.
     */
    void schedule(SEL_SCHEDULE selector, float interval);//自定义时间间隔调度选择器selector,比如每秒一次

    /**
     repeat will execute the action repeat + 1 times, for a continues action use kCCRepeatForever
     delay is the amount of time the action will wait before execution
     */
    void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
    
    /**
     Schedules a selector that runs only once, with a delay of 0 or larger
    */
    void scheduleOnce(SEL_SCHEDULE selector, float delay);//以一个延迟时间调度回调selector,仅调用一次

    /** unschedules a custom selector.*/
    void unschedule(SEL_SCHEDULE selector);//停止调度自定义的选择器

    /** unschedule all scheduled selectors: custom selectors, and the 'update' selector.
     Actions are not affected by this method.
     @since v0.99.3
     */
    void unscheduleAllSelectors(void);// 停止调用所有选择器,包括自定义的和内置的update

    /** resumes all scheduled selectors and actions.
     Called internally by onEnter
     */
    void resumeSchedulerAndActions(void);
    /** pauses all scheduled selectors and actions.
     Called internally by onExit
     */
    void pauseSchedulerAndActions(void);
    
    /* Update will be called automatically every frame if "scheduleUpdate" is called, and the node is "live"
     */
    virtual void update(float fDelta);//内置的选择器,如果调用了scheduleUpdate,那么这个选择器每一帧都会被调用

    
    // transformation methods

    /** Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
     The matrix is in Pixels.
     @since v0.7.1
     */
    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.
     @since v0.7.1
     */
    virtual CCAffineTransform parentToNodeTransform(void);

    /** Returns the world affine transform matrix. The matrix is in Pixels.
     @since v0.7.1
     */
    virtual CCAffineTransform nodeToWorldTransform(void);

    /** Returns the inverse world affine transform matrix. The matrix is in Pixels.
     @since v0.7.1
     */
    virtual CCAffineTransform worldToNodeTransform(void);

    /** Converts a Point to node (local) space coordinates. The result is in Points.
     @since v0.7.1
     */
    CCPoint convertToNodeSpace(const CCPoint& worldPoint);//将世界坐标系的点转换为节点的本地坐标系的点
    /** Converts a Point to world space coordinates. The result is in Points.
     @since v0.7.1
     */
    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.
     @since v0.7.1
     */
    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.
     @since v0.7.1
     */
    CCPoint convertToWorldSpaceAR(const CCPoint& nodePoint);// 将基于锚点的节点的本地坐标系的点转换为世界坐标系的点

    /** convenience methods which take a CCTouch instead of CCPoint
     @since v0.7.1
     */
    CCPoint convertTouchToNodeSpace(CCTouch * touch);//将一个屏幕坐标系的touch转换为节点的本地坐标系的点

    /** converts a CCTouch (world coordinates) into a local coordinate. This method is AR (Anchor Relative).
     @since v0.7.1
     */
    CCPoint convertTouchToNodeSpaceAR(CCTouch * touch);
        
    /** Schedules for script. */
    void scheduleUpdateWithPriorityLua(int nHandler, int priority);

其中有有些较重要的成员变量和函数,做了注释,后面陆续写写。

你可能感兴趣的:(节点,cocos2d-x,cocos2d-x,游戏开发,ccnode)