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