Cocos2d-x學習筆記(三)CCNode分析,cocos2d-xccnode

來源:互聯網
上載者:User

Cocos2d-x學習筆記(三)CCNode分析,cocos2d-xccnode

原創文章,轉載請註明出處:http://blog.csdn.net/sfh366958228/article/details/38706483


通過前兩份學習筆記,我們不難發現CCScene、CCLayer、CCSprite等一系列元素都是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軸座標     *      * zOrder獨立於繪製順序,它僅僅只是記錄node在它父類以及相關兄弟之間的排序,其順序是相對於其父類的子類而言,跟OpenGl的Z vertex沒有關係     * 預設的Z vertex=0.它僅僅影響nodes的繪製順序,數字越大,繪製越靠後     */    virtual void setZOrder(int zOrder);    virtual int getZOrder();    //設定Z軸座標,與setZOrder的區別是,setZOrder先設定m_nZOrder,然後會重新錄入父類的自節點數組    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();        /**     * 設定/擷取arrival order     *     * 一個節點調用addChild後將得到一個更大的arrival order,     * 如果兩個子節點有相同的Z order,那麼arrival order大的將後繪製     */    virtual void setOrderOfArrival(unsigned int uOrderOfArrival);    virtual unsigned int getOrderOfArrival();        // 設定/擷取OpenGL服務端狀態    virtual void setGLServerState(ccGLServerState glServerState);    virtual ccGLServerState getGLServerState();        /**     * 設定/擷取當你設定節點座標位置的時候,錨點是否視作(0, 0)     *     * 當設定為true的時候,錨點還是(0.5,0.5),但是此時是通過起始點(0,0)來定位的     * 而設定為false的時候,是通過錨點定位(0.5,0.5)     * 如果為true的時候,雖然定位是通過起始點(0,0)來起作用的     * 但是對於scale,還是通過錨點來放大縮小     */    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();        // 移除此節點於父類中,並且清除本節點,當cleanup為true的時候,會將action停止,包括子類的action也一併停止    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);        // 擷取/設定使用者資料,可以放進指標,資料區塊,結構體,對象等,注意要release    virtual void* getUserData();    virtual void setUserData(void *pUserData);        // 擷取/設定使用者資料對象,可以放CCObject資料,加進來的CCObject需要release    virtual CCObject* getUserObject();    virtual void setUserObject(CCObject *pUserObject);        /**     * 引擎提供了CCGLProgram類來處理著色器相關操作,對當前繪圖程式進行了封裝,     * 其中使用頻率最高的應該是擷取著色器程式的介面:const GLuint getProgram();     * 該介面返回了當前著色器程式的標識符。後面將會看到,在操作OpenGL的時候,我們常常需要針對不同的著色器程式作設定。     * 注意,這裡返回的是一個無符號整型的標識符,而不是一個指標或結構引用,這是OpenGL介面的一個風格。     * 對象(紋理、著色器程式或其他非標準類型)都是使用整型標識符來表示的。     */    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; };        // 根據優先順序更新Handle(Lua調用)    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);        // 擷取經過縮放和旋轉之後的外框盒大小    virtual CCRect boundingBox(void);    /**     * 設定/擷取當前節點的一個ActionManager     *      * 當添加CCActionManager的時候,原先的action都將停止     * 初始化node時候CCActionManager是通過director->getActionManager()初始化的     * 當然m_pActionManager也retain()了,所以runAction中的一切action都是當前node的m_pActionManager管理的     */    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);        // 擷取正在啟動並執行動作的總數    unsigned int numberOfRunningActions(void);        // 設定/擷取任務    virtual void setScheduler(CCScheduler* scheduler);    virtual CCScheduler* getScheduler();        // 是否正在執行該計劃    bool isScheduled(SEL_SCHEDULE selector);        // 計劃更新方法    void scheduleUpdate(void);        // 根據優先順序更新Handle    void scheduleUpdateWithPriority(int priority);        // 取消更新計劃    void unscheduleUpdate(void);        /**     * 執行某個任務     *     * @param interval  觸發間隔,0為每幀都觸發,如果interval = 0,推薦使用scheduleUpdate()代替     * @param repeat    重複次數     * @param delay     延遲啟動時間     * @lua NA     */    void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);    void schedule(SEL_SCHEDULE selector, float interval);    void schedule(SEL_SCHEDULE selector);    // 執行任務單次    void scheduleOnce(SEL_SCHEDULE selector, float delay);        // 取消任務    void unschedule(SEL_SCHEDULE selector);        // 取消所有任務    void unscheduleAllSelectors(void);        // 恢複/暫停節點的動作和任務    void resumeSchedulerAndActions(void);    void pauseSchedulerAndActions(void);        // 當scheduleUpdate被調用,並且節點為活動狀態時,這個方法將在每幀自動調用    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軸向上。

好了,本節就到這了,下一節將學習CCScene~
cocos2d-x中,我有一個類派生自CCNode, 在我的衍生類別中schedule設定定時器,為何不響應,解,

this->scheduleUpdate();
是這個方法不響應您的update(float dt);嗎?
myNode中你是不是實現了onEnter();?
如果是空實現則需要修改,調用CCNode的onEnter();
希望能幫到你。
 
Cocos2d-x 怎實現CCNode節點的觸摸?

建議看看麥子學院的Cocos2d-x的教程!裡面會有解決方案哦!龍靈修老師講的。不要錯過哈!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.