原创文章,转载请注明出处: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