Cocos2d-x学习笔记(三)“万物之父”——CCNode

原创文章,转载请注明出处:http://blog.csdn.net/sfh366958228/article/details/38701767

通过前两份学习笔记,我们不难发现CCScene、CCLayer、CCSprite、CCAction等一系列元素都是CCNode的子类,但其实“万物之父”这个标题还是有点夸大,毕竟还有像CCDirector、CCCamera之类并不继承自CCNode的组件。

但是CCNode绝对是Cocos2d-x中举足轻重的一个核心,我们可以把它理解为节点。它是一个不能够可视化显示的抽象类,只是用来定义所有节点的公共属性和方法的。

特征

1)每个节点都可以通过addChild方法包含其他节点作为子节点,也可以通过removeChild来移除子节点。CCNode就像是一棵自由的树。

2)每个子节点都可以通过setTag来设置标记,通过getChildByTag来获取该子节点。

3)每个节点都可以执行计划任务,在Cocos2d-x的系统循环中处理这些任务。

4)每个节点都可以通过runAction执行瞬间动作或延时动作。

5)每个节点添加到场景中,当所在场景为激活场景时,这个节点的绘图方法就会被自动调用完成自我绘制。

属性

class CC_DLL CCNode : public CCObject
{
public:
    CCNode(void);
    virtual ~CCNode(void);

    // 初始化节点
    virtual bool init();

    // 创建一个节点对象
    static CCNode * create(void);

    // 获取一个描述字符串,便于调试
    const char* description(void);

    // 设置/获取Z轴坐标
    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);

    // 设置/获取OpenGL真实Z轴坐标
    virtual void setVertexZ(float vertexZ);
    virtual float getVertexZ();

    // 设置/获取X轴缩放系数
    virtual void setScaleX(float fScaleX);
    virtual float getScaleX();

    // 设置/获取Y轴缩放系数
    virtual void setScaleY(float fScaleY);
    virtual float getScaleY();

    // 设置/获取缩放系数
    virtual void setScale(float scale);
    virtual float getScale();

    // 设置缩放系数哦
    virtual void setScale(float fScaleX,float fScaleY);

    // 设置/获取节点坐标
    virtual void setPosition(const CCPoint &position);
    virtual const CCPoint& getPosition();

    // 设置节点坐标
    virtual void setPosition(float x, float y);

    // 获取节点坐标至传参
    virtual void getPosition(float* x, float* y);

    // 设置/获取X轴Y轴坐标,这些方法用在与Lua、Javascript绑定
    virtual void  setPositionX(float x);
    virtual float getPositionX(void);
    virtual void  setPositionY(float y);
    virtual float getPositionY(void);

    // 设置/获取X轴扭曲角度
    virtual void setSkewX(float fSkewX);
    virtual float getSkewX();

    // 设置/获取Y轴扭曲角度
    virtual void setSkewY(float fSkewY);
    virtual float getSkewY();

    // 设置/获取锚点
    virtual void setAnchorPoint(const CCPoint& anchorPoint);
    virtual const CCPoint& getAnchorPoint();

    // 获取绝对坐标上的锚点
    virtual const CCPoint& getAnchorPointInPoints();

    // 设置/获取节点大小
    virtual void setContentSize(const CCSize& contentSize);
    virtual const CCSize& getContentSize() const;

    // 设置/获取节点可见性
    virtual void setVisible(bool visible);
    virtual bool isVisible();

    // 设置/获取节点旋转角度
    virtual void setRotation(float fRotation);
    virtual float getRotation();

    // 设置/获取节点X轴旋转角度
    virtual void setRotationX(float fRotaionX);
    virtual float getRotationX();

    // 设置/获取节点Y轴旋转角度
    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 setIgnoreAnchorPointForPosition(bool) or something with "set"
     */
    virtual void ignoreAnchorPointForPosition(bool ignore);
    virtual bool isIgnoreAnchorPointForPosition();

    // 添加子节点
    virtual void addChild(CCNode * child);

    // 添加子节点,同时设置子节点Z轴坐标
    virtual void addChild(CCNode * child, int zOrder);

    // 添加子节点,同时设置子节点Z轴坐标及子节点Tag标签
    virtual void addChild(CCNode* child, int zOrder, int tag);

    // 通过Tag获取子节点
    virtual CCNode * getChildByTag(int tag);

    // 获取当前节点所有子节点
    virtual CCArray* getChildren();

    // 获取当前子节点数量
    virtual unsigned int getChildrenCount(void) const;

    // 设置/获取当前节点父节点
    virtual void setParent(CCNode* parent);
    virtual CCNode* getParent();

    // 将当前节点从父节点中移除
    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 should be removed, false otherwise.
     * @js removeFromParent
     */
    virtual void removeFromParentAndCleanup(bool cleanup);

    // 移除子节点
    virtual void removeChild(CCNode* child);

    // 移除子节点,并设置是否清楚本节点
    virtual void removeChild(CCNode* child, bool cleanup);

    // 通过Tag移除子节点
    virtual void removeChildByTag(int tag);

    // 通过Tag移除子节点,并设置是否清楚本节点
    virtual void removeChildByTag(int tag, bool cleanup);

    // 移除所有子节点
    virtual void removeAllChildren();

    // 移除所有子节点,并设置是否清楚本节点
    virtual void removeAllChildrenWithCleanup(bool cleanup);

    // 重新设置某个子节点的Z轴坐标
    virtual void reorderChild(CCNode * child, int zOrder);

    // 给所有子节点排序
    virtual void sortAllChildren();

    // 获取/设置网格对象
    virtual CCGridBase* getGrid();
    virtual void setGrid(CCGridBase *pGrid);

    // 获取/设置Tag标识
    virtual int getTag() const;
    virtual void setTag(int nTag);

    // 获取/设置用户数据
    virtual void* getUserData();
    virtual void setUserData(void *pUserData);

    // 获取/设置用户数据对象
    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);

    // 获取摄像机对象
    virtual CCCamera* getCamera();

    // 获取当前节点是否在运行
    virtual bool isRunning();

    // 注册/取消注册脚本Handle
    virtual void registerScriptHandler(int handler);
    virtual void unregisterScriptHandler(void);

    //获取脚本Handle
    inline int getScriptHandler() { return m_nScriptHandler; };

    /**
     * Schedules for lua script.
     * @js NA
     */
    void scheduleUpdateWithPriorityLua(int nHandler, int priority);

    // 当节点进入时调用
    virtual void onEnter();

    // 当节点进入动画结束时调用
    virtual void onEnterTransitionDidFinish();

    // 当节点退出入时调用
    virtual void onExit();

    // 当节点退出动画结束时调用
    virtual void onExitTransitionDidStart();

    // 停止所有运行的动画及调度
    virtual void cleanup(void);

    // 重构这个方法能够绘制自己的节点
    virtual void draw(void);

    // 递归遍历当前节点树
    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);

    // 设置/获取当前节点的一个ActionManager
    virtual void setActionManager(CCActionManager* actionManager);
    virtual CCActionManager* getActionManager();

    // 执行Action
    CCAction* runAction(CCAction* action);

    // 停止所有Action
    void stopAllActions(void);

    // 停止指定Action
    void stopAction(CCAction* action);

    // 通过Tag停止/获取Action
    void stopActionByTag(int 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);

    // 设置/获取
    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 could 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 could 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);

    /**
     * 执行某个任务
     *
     * @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(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
    void schedule(SEL_SCHEDULE selector, float interval);
    void scheduleOnce(SEL_SCHEDULE selector, float delay);
    void schedule(SEL_SCHEDULE selector);

    // 取消任务
    void unschedule(SEL_SCHEDULE selector);

    // 取消所有任务
    void unscheduleAllSelectors(void);

    // 恢复/暂停节点的动作和任务
    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);

    /**
     * Calls children'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 CCAffineTransform& additionalTransform);

    // 获取组件
    CCComponent* getComponent(const char *pName) const;

    // 添加组件
    virtual bool addComponent(CCComponent *pComponent);

    // 通过名字移除组件
    virtual bool removeComponent(const char *pName);

    // 通过指针移除组件
    virtual bool removeComponent(CCComponent *pComponent);

    // 移除所有组件
    virtual void removeAllComponents();
}

以上方法为CCNode中的提供,在public块中的方法主要由以下几个部分:

1)针对节点显示的属性信息读写

2)针对节点变化的属性信息读写

3)针对子节点管理的相关方法

4)针对节点数据绑定的相关方法

5)针对节点生命周期的相关方法

6)针对节点处理动作CCAction的相关方法

7)针对节点定时任务的相关方法

8)针对节点坐标变换的相关方法

在CCNode中的节点都有自己的坐标系,成为节点坐标系,当节点坐标系变换后该节点的所有子节点都会随之变换。

节点添加到场景时会参考节点的锚点,锚点默认定义为该节点的中心,在贴图时会以锚点为中心,改变锚点并没有改变节点的位置,只是改变了贴图的位置、

Cocos2d-x的世界坐标系和openGL的坐标系一致,都是一左下角为0,0点,X轴向右,y轴向上。

好了,本节就到这了,下一节将学习CCLayer~

Cocos2d-x学习笔记(三)“万物之父”——CCNode

时间: 2024-11-03 22:43:39

Cocos2d-x学习笔记(三)“万物之父”——CCNode的相关文章

angular学习笔记(三十)-指令(5)-link

这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数:scope,iEle,iAttrs,ctrl,linker scope:指令所在的作用域,这个scope和指令定义的scope是一致的.至于指令的scope,会在讲解scope属性的时候详细解释 iEle:指令元素的jqLite封装.(也就是说iEle可以调用angular封装的简版jq的方法和属

Ajax学习笔记(三)

三.jQuery库详解 1.使用jQuery之后,javascript操作的不再是HTML元素对应的DOM对象,而是包装DOM对象的jQuery对象.js通过调用jQuery对象的方法来改变它所包装的DOM对象的属性,从而实现动态更新HTML页面. 由此可见,使用jQuery动态更新HTML页面只需以下两个步骤: (1)获取jQuery对象.jQuery对象通常是DOM对象的包装 (2)调用jQuery对象的方法来改变自身.当jQuery对象被改变时,jQuery包装的DOM对象随之改变,HTM

angular学习笔记(三十)-指令(7)-compile和link(2)

继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的代码: html: <body> <div ng-controller="compileCtrl"> <level-one> <level-two> <level-three> hello,{{name}} </level-

angular学习笔记(三十)-指令(10)-require和controller

本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐directive> <inner‐directive></inner‐directive> </outer‐directive> 这里有两个指令,一个outer-directive指令元素,它里面又有一个inner-directive指令元素. js: app.directiv

Caliburn.Micro学习笔记(三)----事件聚合IEventAggregator和 Ihandle&lt;T&gt;

Caliburn.Micro学习笔记(三)----事件聚合IEventAggregator和 Ihandle<T> 今天 说一下Caliburn.Micro的IEventAggregator和IHandle<T>分成两篇去讲这一篇写一个简单的例子 看一它的的实现和源码 下一篇用它们做一个多语言的demo 这两个是事件的订阅和广播,很强大,但用的时候要小心发生不必要的冲突. 先看一下它的实现思想 在Caliburn.Micro里EventAggregator要以单例的形式出现这样可以

OpenCV for Python 学习笔记 三

给源图像增加边界 cv2.copyMakeBorder(src,top, bottom, left, right ,borderType,value) src:源图像 top,bottem,left,right: 分别表示四个方向上边界的长度 borderType: 边界的类型 有以下几种: BORDER_REFLICATE # 直接用边界的颜色填充, aaaaaa | abcdefg | gggg BORDER_REFLECT # 倒映,abcdefg | gfedcbamn | nmabcd

NFC学习笔记——三(在windows操作系统上安装libnfc)

本篇翻译文章: 这篇文章主要是说明如何在windows操作系统上安装.配置和使用libnfc. 一.基本信息 1.操作系统: Windows Vista Home Premium SP 2 2.硬件信息: System: Dell Inspiron 1720 Processor: Intel Core 2 Duo CPU T9300 @ 2.5GHz 2.5GHz System type: 32-bit Operating System 3.所需软件: 在windows操作系统上安装软件需要下列

swift学习笔记(三)关于拷贝和引用

在swift提供的基本数据类型中,包括Int ,Float,Double,String,Enumeration,Structure,Dictionary都属于值拷贝类型. 闭包和函数同属引用类型 捕获则为拷贝.捕获即定义这些常量和变量的原作用域已不存在,闭包仍然可以在闭包函数体内引用和修改这些值 class属于引用类型. Array的情况稍微复杂一些,下面主要对集合类型进行分析: 一.关于Dictionary:无论何时将一个字典实例赋给一个常量,或者传递给一个函数方法时,在赋值或调用发生时,都会

加壳学习笔记(三)-简单的脱壳思路&amp;调试思路

首先一些windows的常用API: GetWindowTextA:以ASCII的形式的输入框 GetWindowTextW:以Unicaode宽字符的输入框 GetDlgItemTextA:以ASCII的形式的输入框 GetDlgItemTextW:以Unicaode宽字符的输入框 这些函数在使用的时候会有些参数提前入栈,如这函数要求的参数是字符串数目.还有大小写啦之类的东西,这些东西是要在调用该函数之前入栈,也就是依次push,就是说一般前面几个push接着一个call,那前面的push可能