Cocos2d-x學習筆記(三)“萬物之父”——CCNode

來源:互聯網
上載者:User

標籤:cocos2d-x   遊戲   

原創文章,轉載請註明出處: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~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.