【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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章