http://www.cnblogs.com/lhming/archive/2012/07/01/2572227.html
可以看到CCNode 几乎是游戏中处理的大部分类的父类,其主要有以下函数:
virtual int getZOrder (void) //获取节点的顺序
virtual const CCPoint & getPosition (void) //获取节点的位置
virtual void setPosition (const CCPoint &var) //设置节点的位置
virtual CCArray * getChildren (void) //获取其所有子节点
virtual CCCamera * getCamera (void)//获取其对应的摄像机
virtual bool getIsVisible (void) //判断节点是否可见
virtual void setIsVisible (bool var) //设置节点可见性
virtual const CCPoint & getAnchorPoint (void) //获取节点的锚点的位置
virtual void setAnchorPoint (const CCPoint &var) //设置节点的锚点位置
virtual bool getIsRunning (void) //判断节点是否在运行
virtual CCNode * getParent (void)//获取父及节点指针
virtual void setParent (CCNode *var) //设置节点的父节点
virtual int getTag (void) //获取节点的tag
virtual void setTag (int var) //设置节点的tag
char * description (void) //返回节点的描述
virtual void onEnter () //进入节点时的回调函数
virtual void onEnterTransitionDidFinish ()//进入节点后的回调函数
virtual void onExit ()//离开节点时的回调函数
virtual void addChild (CCNode *child)//增加节点
virtual void addChild (CCNode *child, int zOrder) //通过顺序添加节点
virtual void addChild (CCNode *child, int zOrder, int tag)//通过顺序和tag添加节点
void removeFromParentAndCleanup (bool cleanup)//删除父节点中的当前节点并清除动作及回调函数
virtual void removeChild (CCNode *child, bool cleanup) //删除节点
void removeChildByTag (int tag, bool cleanup)//通过tag删除节点
virtual void removeAllChildrenWithCleanup (bool cleanup)//删除节点并清除动作及回调函数
CCNode * getChildByTag (int tag)//通过tag获取节点
virtual void reorderChild (CCNode *child, int zOrder)//根据order重新排序
virtual void cleanup (void)//清除动作
virtual void draw (void)//绘制自己
virtual void visit (void)//访问节点
CCAction * runAction (CCAction *action) //运行动作
void stopAllActions (void)//停止所有的动作
void stopAction (CCAction *action)//停止动作
void stopActionByTag (int tag) //通过tag停止动作
CCAction * getActionByTag (int tag)//通过tag获取动作的指针
unsigned int numberOfRunningActions (void)//正在运行的动作的总数
void schedule (SEL_SCHEDULE selector)//定义一个定时器
void schedule (SEL_SCHEDULE selector, ccTime interval)//定义一个定时器
void unschedule (SEL_SCHEDULE selector)//取消一个定时器
void unscheduleAllSelectors (void)//取消所有定时器
void resumeSchedulerAndActions (void)//恢复定时器和动作
void pauseSchedulerAndActions (void)//暂停定时器和动作
static CCNode * node (void)//生成一个节点
CCNode是cocos2d-x中一个很重要的类,CCNode是场景、层、菜单、精灵等的父类。而我们在使用cocos2d-x时,接触最多的就是场景、层、菜单、精灵等。所以有必要先弄懂CCNode类.CCObject类是cocos2d-x中所有对象的基类,CCObject封装了对象的引用计数和自动释放功能.因为场景、层、精灵、菜菜单是CCNode的子类,所以在使用它们时,有些方法来自CCNode.
CCNode是cocos2d-x的渲染链,写游戏基本上就是和他打交道了,cocos2d-x同时只能渲染一个CCScene,因此CCScene是渲染的根节点。在构建游戏时,一般是一个Scene中添加一个或者多个CCLayer,一个Layer中又添加多个CCSprite或者CCMenu,CCSprite中还可以添加CCParticleSystem等等。这样就构建了一个渲染树,cocos2d-x历遍这个树来将图像显示在屏幕上。
coco2d-x的渲染实际上是调用visit()函数来完成的:即visit()这个函数调用它包含的Child的zOrder<0的visit()函数,之后调用draw()函数,再调用Child的zOrder>=0的visit()函数,它实际上是一个深度优先的算法。他的Child是按照zOrder排序的,以保证渲染的正确性。draw()的作用是绘制自己。在CCSprite这些确实需要绘制的类中,draw()调用openGL的函数来完成绘制功能——把一个纹理映射到一个矩形上。
如果要自定义绘制一些图像,可以重写draw()函数,不过不要忘记调用父类的draw()函数。
可以进行网状关系的管理,其实CCScene,CCLayer也是一个CCNode.它提供节点增删,包含,提供节点缩放,每个节点有一个照相机,提供动画支持,也就是说每一个从CCNode派生的类都可以执行动画操作;
- /**
- * 将对象添加到节点,默认Z坐标为0
- *
- * 如果孩子被添加到“running”节点,然后onenter”和“onentertransitiondidfinish”会被立刻回调。
- *
- * @param child A child node
- */
- virtual void addChild(Node * child);
- /**
- * 将对象添加到节点,指定Z坐标
- *
- * 如果孩子被添加到“running”节点,然后onenter”和“onentertransitiondidfinish”会被立刻回调。
- *
- * @param child A child node
- * @param zOrder Z order for drawing priority. Please refer to setZOrder(int)
- */
- virtual void addChild(Node * child, int zOrder);
- /**
- * 将对象添加到节点,指定Z坐标和标志值
- *
- * 如果孩子被添加到“running”节点,然后onenter”和“onentertransitiondidfinish”会被立刻回调。
- *
- * @param child A child node
- * @param zOrder Z order for drawing priority. Please refer to setZOrder(int)
- * @param tag A interger to identify the node easily. Please refer to setTag(int)
- */
- virtual void addChild(Node* child, int zOrder, int tag);
- /**
- * 获取标志值所表示的子节点
- *
- * @param tag An identifier to find the child node.
- *
- * @return a Node object whose tag equals to the input parameter
- */
- Node * getChildByTag(int tag);
- /**
- * 返回所有子节点的数组
- *
- * Composing a "tree" structure is a very important feature of Node
- * Here's a sample code of traversing children array:
- * @code
- * Node* node = NULL;
- * CCARRAY_FOREACH(parent->getChildren(), node)
- * {
- * node->setPosition(0,0);
- * }
- * @endcode
- * This sample code traverses all children nodes, and set there position to (0,0)
- *
- * @return An array of children
- */
- virtual Array* getChildren() { return _children; }
- virtual const Array *getChildren() const { return _children; }
- /**
- * 获得子节点的数量
- *
- * @return The amount of children.
- */
- unsigned int getChildrenCount() const;
- /**
- * 设置父节点
- *
- * @param parent A pointer to the parnet node
- */
- virtual void setParent(Node* parent);
- /**
- * 返回父节点(爸爸去哪儿, 爸爸只有一个, 没有爸爸们)
- *
- * @see setParent(Node*)
- *
- * @returns A pointer to the parnet node
- */
- virtual Node* getParent() { return _parent; }
- virtual const Node* getParent() const { return _parent; }
- // REMOVES //
- /**
- * 从父节点移除,并且清除
- * If the node orphan, then nothing happens.
- * @see removeFromParentAndCleanup(bool)
- */
- virtual void removeFromParent();
- /**
- * 从父节点移除,并不一定清除
- * 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
- * @lua removeFromParent
- */
- virtual void removeFromParentAndCleanup(bool cleanup);
- /**
- * 通过对象指针移除子节点并清除
- *
- * @param child The child node which will be removed.
- * @param cleanup true if all running actions and callbacks on the child node will be cleanup, false otherwise.
- */
- virtual void removeChild(Node* child, bool cleanup = true);
- /**
- * 通过标志值移除子节点并清除
- *
- * @param tag An interger number that identifies a child node
- * @param cleanup true if all running actions and callbacks on the child node will be cleanup, false otherwise.
- */
- virtual void removeChildByTag(int tag, bool cleanup = true);
- /**
- * AOE!! 子节点全清除
- *
- * @see removeAllChildrenWithCleanup(bool)
- */
- virtual void removeAllChildren();
- /**
- * AOE!! 子节点全移除,但不一定清除
- *
- * @param cleanup true if all running actions on all children nodes should be cleanup, false oterwise.
- * @js removeAllChildren
- * @lua removeAllChildren
- */
- virtual void removeAllChildrenWithCleanup(bool cleanup);
- /**
- * 重新设置子节点的Z坐标
- *
- * @param child An already added child node. It MUST be already added.
- * @param zOrder Z order for drawing priority. Please refer to setZOrder(int)
- */
- virtual void reorderChild(Node * child, int zOrder);
- /**
- * 通过排序所有子节点,提供渲染性能. 慎用!
- * This appraoch can improves the performance massively.
- * @note Don't call this manually unless a child added needs to be removed in the same frame
- */
- virtual void sortAllChildren();
- /// @} end of Children and Parent