【Cocos2d-x】CCNode

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派生的类都可以执行动画操作;



  1. /**  
  2.     * 将对象添加到节点,默认Z座标为0 
  3.     * 
  4.     * 如果孩子被添加到“running”节点,然后onenter”和“onentertransitiondidfinish”会被立刻回调。 
  5.     * 
  6.     * @param child A child node 
  7.     */  
  8.    virtual void addChild(Node * child);  
  9.    /**  
  10.     * 将对象添加到节点,指定Z座标 
  11.     * 
  12.     * 如果孩子被添加到“running”节点,然后onenter”和“onentertransitiondidfinish”会被立刻回调。 
  13.     * 
  14.     * @param child     A child node 
  15.     * @param zOrder    Z order for drawing priority. Please refer to setZOrder(int) 
  16.     */  
  17.    virtual void addChild(Node * child, int zOrder);  
  18.    /**  
  19.     * 将对象添加到节点,指定Z座标和标志值 
  20.     * 
  21.     * 如果孩子被添加到“running”节点,然后onenter”和“onentertransitiondidfinish”会被立刻回调。 
  22.     * 
  23.     * @param child     A child node 
  24.     * @param zOrder    Z order for drawing priority. Please refer to setZOrder(int) 
  25.     * @param tag       A interger to identify the node easily. Please refer to setTag(int) 
  26.     */  
  27.    virtual void addChild(Node* child, int zOrder, int tag);  
  28.    /** 
  29.     * 获取标志值所表示的子节点 
  30.     * 
  31.     * @param tag   An identifier to find the child node. 
  32.     * 
  33.     * @return a Node object whose tag equals to the input parameter 
  34.     */  
  35.    Node * getChildByTag(int tag);  
  36.    /** 
  37.     * 返回所有子节点的数组 
  38.     * 
  39.     * Composing a "tree" structure is a very important feature of Node 
  40.     * Here's a sample code of traversing children array: 
  41.     * @code 
  42.     * Node* node = NULL; 
  43.     * CCARRAY_FOREACH(parent->getChildren(), node) 
  44.     * { 
  45.     *     node->setPosition(0,0); 
  46.     * } 
  47.     * @endcode 
  48.     * This sample code traverses all children nodes, and set there position to (0,0) 
  49.     * 
  50.     * @return An array of children 
  51.     */  
  52.    virtual Array* getChildren() { return _children; }  
  53.    virtual const Array *getChildren() const { return _children; }  
  54.      
  55.    /**  
  56.     * 获得子节点的数量 
  57.     * 
  58.     * @return The amount of children. 
  59.     */  
  60.    unsigned int getChildrenCount() const;  
  61.      
  62.    /** 
  63.     * 设置父节点 
  64.     * 
  65.     * @param parent    A pointer to the parnet node 
  66.     */  
  67.    virtual void setParent(Node* parent);  
  68.    /** 
  69.     * 返回父节点(爸爸去哪儿, 爸爸只有一个, 没有爸爸们) 
  70.     *  
  71.     * @see setParent(Node*) 
  72.     * 
  73.     * @returns A pointer to the parnet node 
  74.     */  
  75.    virtual Node* getParent() { return _parent; }  
  76.    virtual const Node* getParent() const { return _parent; }  
  77.   
  78.      
  79.    ////// REMOVES //////  
  80.      
  81.    /**  
  82.     * 从父节点移除,并且清除 
  83.     * If the node orphan, then nothing happens. 
  84.     * @see removeFromParentAndCleanup(bool) 
  85.     */  
  86.    virtual void removeFromParent();  
  87.    /**  
  88.     * 从父节点移除,并不一定清除 
  89.     * If the node orphan, then nothing happens. 
  90.     * @param cleanup   true if all actions and callbacks on this node should be removed, false otherwise. 
  91.     * @js removeFromParent 
  92.     * @lua removeFromParent 
  93.     */  
  94.    virtual void removeFromParentAndCleanup(bool cleanup);  
  95.   
  96.    /**  
  97.     * 通过对象指针移除子节点并清除 
  98.     *  
  99.     * @param child     The child node which will be removed. 
  100.     * @param cleanup   true if all running actions and callbacks on the child node will be cleanup, false otherwise. 
  101.     */  
  102.    virtual void removeChild(Node* child, bool cleanup = true);  
  103.   
  104.    /**  
  105.     * 通过标志值移除子节点并清除 
  106.     *  
  107.     * @param tag       An interger number that identifies a child node 
  108.     * @param cleanup   true if all running actions and callbacks on the child node will be cleanup, false otherwise.  
  109.     */  
  110.    virtual void removeChildByTag(int tag, bool cleanup = true);  
  111.    /**  
  112.     * AOE!! 子节点全清除 
  113.     * 
  114.     * @see removeAllChildrenWithCleanup(bool) 
  115.     */  
  116.    virtual void removeAllChildren();  
  117.    /**  
  118.     * AOE!! 子节点全移除,但不一定清除 
  119.     * 
  120.     * @param cleanup   true if all running actions on all children nodes should be cleanup, false oterwise. 
  121.     * @js removeAllChildren 
  122.     * @lua removeAllChildren 
  123.     */  
  124.    virtual void removeAllChildrenWithCleanup(bool cleanup);  
  125.      
  126.    /**  
  127.     * 重新设置子节点的Z座标 
  128.     * 
  129.     * @param child     An already added child node. It MUST be already added. 
  130.     * @param zOrder    Z order for drawing priority. Please refer to setZOrder(int) 
  131.     */  
  132.    virtual void reorderChild(Node * child, int zOrder);  
  133.      
  134.    /**  
  135.     * 通过排序所有子节点,提供渲染性能.  慎用! 
  136.     * This appraoch can improves the performance massively. 
  137.     * @note Don't call this manually unless a child added needs to be removed in the same frame  
  138.     */  
  139.    virtual void sortAllChildren();  
  140.   
  141.    /// @} end of Children and Parent  
  142.      
  143.   
  144.      


发布了4 篇原创文章 · 获赞 2 · 访问量 2万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章