【麦可网】Cocos2d-X跨平台游戏开发---学习笔记
第八课:Cocos2D-X引擎框架3
=======================================================================================================================================================================
课程目标:
- Cocos2D-X设计思想
- Cocos2D-X架构方案
课程重点:
- Cocos2D-X设计思想
- Cocos2D-X架构方案
考核目标:
- Cocos2D-X设计思想
- Cocos2D-X架构方案
=======================================================================================================================================================================
一、节点
- class CC_DLL CCNode : public CCObject
- {
- // 变量属性
- /** z order节点相对于它的兄弟节点的顺序*/
- CC_PROPERTY_READONLY(int,m_nZOrder,ZOrder)
- /**真正的openGL Z 轴.
- openGL Z 轴和 cocos2d Z 顺序的区别:
- - OpenGL Z 改变Z 轴,而不是z order的顺序
- - OpenGL Z 可能要求设置 2D 投射
- - cocos2d Z order在所有的节点用相同的openGL Z 轴时工作正常。
- eg: vertexZ = 0
- @warning: 使用时由自己负责,因为z order会改变cocos2d 的显示顺序
- @since v0.8
- */
- CC_PROPERTY(float,m_fVertexZ,VertexZ)
- /** 节点旋转角度, 默认角度问0. 正值表示顺时针旋转 */
- CC_PROPERTY(float,m_fRotation,Rotation)
- /** 获得节点的旋转角度.
- @warning: 确保 m_fScaleX == m_fScaleY.
- */
- float getScale();
- /** 默认的旋转角度是1.0. 同时改变X和Y的旋转角度. */
- void setScale(float scale);
- /** 默认的旋转角度是1.0. 只改变X的旋转角度. */
- CC_PROPERTY(float,m_fScaleX,ScaleX)
- /** 默认的旋转角度是1.0. 只改变Y的旋转角度.*/
- CC_PROPERTY(float,m_fScaleY,ScaleY)
- /** OpenGL 的坐标系中,节点Position (x,y) ,(0,0)表示左下角*/
- CC_PROPERTY_PASS_BY_REF(CCPoint,m_tPosition,Position)
- CC_PROPERTY_PASS_BY_REF(CCPoint,m_tPositionInPixels,PositionInPixels)
- /** get/set Position for Lua (pass number faster than CCPoint object)
- lua code:
- local pos = node:getPositionLua() -- return CCPoint object from C++
- local x,y = node:getPosition() -- return x,y values from C++
- local x = node:getPositionX()
- local y = node:getPositionY()
- node:setPosition(x,y) -- pass x,y values to C++
- node:setPositionX(x)
- node:setPositionY(y)
- node:setPositionInPixels(x,y) -- pass x,y values to C++
- */
- const CCPoint& getPositionLua(void);
- void getPosition(float* x,float* y);
- float getPositionX(void);
- float getPositionY(void);
- void setPositionX(float x);
- void setPositionY(float y);
- void setPosition(float x,float y);
- void setPositionInPixels(float x,float y);
- /** X的倾斜角度.
- This angle describes the shear distortion in the X direction.
- Thus,it is the angle between the Y axis and the left edge of the shape
- The default skewX angle is 0. Positive values distort the node in a CW direction.
- */
- CC_PROPERTY(float,m_fSkewX,SkewX)
- /** Y的倾斜角度.
- This angle describes the shear distortion in the Y direction.
- Thus,it is the angle between the X axis and the bottom edge of the shape
- The default skewY angle is 0. Positive values distort the node in a CCW direction.
- */
- CC_PROPERTY(float,m_fSkewY,SkewY)
- CC_PROPERTY_READONLY(CCArray*,m_pChildren,Children)
- /** 获得子节点数量 */
- unsigned int getChildrenCount(void);
- /**摄像机对象用gluLookAt移动节点*/
- CC_PROPERTY_READONLY(CCCamera *,m_pCamera,Camera)
- /** 网格对象 */
- CC_PROPERTY(CCGridBase *,m_pGrid,Grid)
- /** 节点是否透明. 默认值是true */
- CC_PROPERTY(bool,m_bIsVisible,IsVisible)
- /**锚点是操作所有结点的变换和位置摆放
- It's like a pin in the node where it is "attached" to its parent.
- The anchorPoint is normalized,like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
- But you can use values higher than (1,1) and lower than (0,0) too.
- The default anchorPoint is (0.5,0.5),so it starts in the center of the node.
- @since v0.8
- */
- CC_PROPERTY_PASS_BY_REF(CCPoint,m_tAnchorPoint,AnchorPoint)
- /** 锚点是绝对像素点.
- Since v0.8 you can only read it. If you wish to modify it,use anchorPoint instead
- */
- CC_PROPERTY_READONLY_PASS_BY_REF(CCPoint,m_tAnchorPointInPixels,AnchorPointInPixels)
- /** 该节点的原始大小.
- The contentSize remains the same no matter the node is scaled or rotated.
- All nodes has a size. Layer and Scene has the same size of the screen.
- @since v0.8
- */
- CC_PROPERTY_PASS_BY_REF(CCSize,m_tContentSize,ContentSize)
- /** 该节点的原始像素值
- The contentSize remains the same no matter the node is scaled or rotated.
- All nodes has a size. Layer and Scene has the same size of the screen.
- @since v0.8
- */
- CC_PROPERTY_PASS_BY_REF(CCSize,m_tContentSizeInPixels,ContentSizeInPixels)
- /** 该节点是否在运行 */
- CC_PROPERTY_READONLY(bool,m_bIsRunning,IsRunning)
- /** 该节点的父节点 */
- CC_PROPERTY(CCNode *,m_pParent,Parent)
- /** 如果为真,则相对锚点变换.
- * Sprites,Labels and any other sizeble object use it have it enabled by default.
- * Scenes,Layers and other "whole screen" object don't use it,have it disabled by default.
- */
- CC_PROPERTY(bool,m_bIsRelativeAnchorPoint,IsRelativeAnchorPoint)
- /** 节点的标识 */
- CC_PROPERTY(int,m_nTag,Tag)
- /** A custom user data pointer */
- CC_PROPERTY(void *,m_pUserData,UserData)
- protected:
- // transform
- CCAffineTransform m_tTransform,m_tInverse;
- #ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
- GLfloat m_pTransformGL[16];
- #endif
- // To reduce memory,place bools that are not properties here:
- bool m_bIsTransformDirty;
- bool m_bIsInverseDirty;
- #ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
- bool m_bIsTransformGLDirty;
- #endif
- int m_nScriptHandler;
- private:
- //! lazy allocs
- void childrenAlloc(void);
- //! helper that reorder a child
- void insertChild(CCNode* child,int z);
- //! used internally to alter the zOrder variable. DON'T call this method manually
- void setZOrder(int z);
- void detachChild(CCNode *child,bool doCleanup);
- typedef void (CCNode::*callbackFunc)(void);
- void arrayMakeObjectsPerformSelector(CCArray* pArray,callbackFunc func);
- CCPoint convertToWindowSpace(const CCPoint& nodePoint);
- public:
- CCNode(void);
- virtual ~CCNode(void);
- char * description(void);
- /** 分配和初始化节点.
- The node will be created as "autorelease".
- */
- static CCNode * node(void);
- //scene managment
- /** 节点进入舞台时调用.
- If the CCNode enters the 'stage' with a transition,this callback is called when the transition starts.
- During onEnter you can't a "sister/brother" node.
- */
- virtual void onEnter();
- /** 节点在舞台时调用.
- If the CCNode enters the 'stage' with a transition,this callback is called when the transition finishes.
- @since v0.8
- */
- virtual void onEnterTransitionDidFinish();
- /** 节点退出舞台时调用.
- If the CCNode leaves the 'stage' with a transition,this callback is called when the transition finishes.
- During onExit you can't access a sibling node.
- */
- virtual void onExit();
- /** Register onEnter/onExit handler script function
- Script handler auto unregister after onEnter().
- */
- virtual void registerScriptHandler(int nHandler);
- virtual void unregisterScriptHandler(void);
- // composition: ADD
- /** 添加节点, z-order as 0.
- If the child is added to a 'running' node,then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
- @since v0.7.1
- */
- virtual void addChild(CCNode * child);
- /** 添加节点, 并设置z-order
- If the child is added to a 'running' node,then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
- @since v0.7.1
- */
- virtual void addChild(CCNode * child,int zOrder);
- /** 添加节点, 并设置z-order和tag
- If the child is added to a 'running' node,int zOrder,int tag);
- // composition: REMOVE
- /**从父节点中删除该节点,如果参数为true,则移除所有的动作和回调.
- If the node orphan,then nothing happens.
- @since v0.99.3
- */
- void removeFromParentAndCleanup(bool cleanup);
- /** 从删除该节点
- @since v0.7.1
- */
- virtual void removeChild(CCNode* child,bool cleanup);
- /** 根据tag删除该节点
- @since v0.7.1
- */
- void removeChildByTag(int tag,bool cleanup);
- /** 删除所有的节点
- @since v0.7.1
- */
- virtual void removeAllChildrenWithCleanup(bool cleanup);
- // composition: GET
- /** 依据tag获得节点
- @return returns a CCNode object
- @since v0.7.1
- */
- CCNode * getChildByTag(int tag);
- /** Reorders a child according to a new z value.
- * The child MUST be already added.
- */
- virtual void reorderChild(CCNode * child,int zOrder);
- /** Stops all running actions and schedulers
- @since v0.8
- */
- virtual void cleanup(void);
- // draw
- /** Override this method to draw your own node.
- The following GL states will be enabled by default:
- - glEnableClientState(GL_VERTEX_ARRAY);
- - glEnableClientState(GL_COLOR_ARRAY);
- - glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- - glEnable(GL_TEXTURE_2D);
- AND YOU SHOULD NOT DISABLE THEM AFTER DRAWING YOUR NODE
- But if you enable any other GL state,you should disable it after drawing your node.
- */
- virtual void draw(void);
- /** recursive method that visit its children and draw them */
- virtual void visit(void);
- // transformations
- /** 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.
- @since v0.7.2
- */
- void transformAncestors(void);
- /** returns a "local" axis aligned bounding Box of the node.
- The returned Box is relative only to its parent.
- @since v0.8.2
- */
- CCRect boundingBox(void);
- /** returns a "local" axis aligned bounding Box of the node in pixels.
- The returned Box is relative only to its parent.
- The returned Box is in Points.
- @since v0.99.5
- */
- CCRect boundingBoxInPixels(void);
- // actions
- /** Executes an action,and returns the action that is executed.
- The node becomes the action's target.
- @warning Starting from v0.8 actions don't retain their target anymore.
- @since v0.7.1
- @return An Action pointer
- */
- CCAction* runAction(CCAction* action);
- /** Removes all actions from the running action list */
- void stopAllActions(void);
- /** Removes an action from the running action list */
- void stopAction(CCAction* action);
- /** Removes an action from the running action list given its tag
- @since v0.7.1
- */
- void stopActionByTag(int tag);
- /** Gets an action from the running action list given its tag
- @since v0.7.1
- @return the Action the with the given 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.
- */
- unsigned int numberOfRunningActions(void);
- // timers
- /** check whether a selector is scheduled. */
- 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.
- @since v0.99.3
- */
- void scheduleUpdate(void);
- /** schedules the "update" selector with a custom priority. This selector will be called every frame.
- Scheduled selectors 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).
- @since v0.99.3
- */
- void scheduleUpdateWithPriority(int priority);
- /* unschedules the "update" method.
- @since v0.99.3
- */
- void unscheduleUpdate(void);
- /** schedules a selector.
- The scheduled selector will be ticked every frame
- */
- void schedule(SEL_SCHEDULE selector);
- /** schedules a custom selector with an interval time in seconds.
- If time is 0 it will be ticked every frame.
- If time is 0,it is recommended to use 'scheduleUpdate' instead.
- If the selector is already scheduled,then the interval parameter
- will be updated without scheduling it again.
- */
- void schedule(SEL_SCHEDULE selector,ccTime interval);
- /** unschedules a custom selector.*/
- void unschedule(SEL_SCHEDULE selector);
- /** unschedule all scheduled selectors: custom selectors,and the 'update' selector.
- Actions are not affected by this method.
- @since v0.99.3
- */
- void unscheduleAllSelectors(void);
- /** resumes all scheduled selectors and actions.
- Called internally by onEnter
- */
- void resumeSchedulerAndActions(void);
- /** pauses all scheduled selectors and actions.
- Called internally by onExit
- */
- void pauseSchedulerAndActions(void);
- // transformation methods
- /** Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates.
- The matrix is in Pixels.
- @since v0.7.1
- */
- CCAffineTransform nodeToParentTransform(void);
- /** Returns the matrix that transform parent's space coordinates to the node's (local) space coordinates.
- The matrix is in Pixels.
- @since v0.7.1
- */
- CCAffineTransform parentToNodeTransform(void);
- /** Retrusn the world affine transform matrix. The matrix is in Pixels.
- @since v0.7.1
- */
- CCAffineTransform nodeToWorldTransform(void);
- /** Returns the inverse world affine transform matrix. The matrix is in Pixels.
- @since v0.7.1
- */
- CCAffineTransform worldToNodeTransform(void);
- /** Converts a Point to node (local) space coordinates. The result is in Points.
- @since v0.7.1
- */
- CCPoint convertToNodeSpace(const CCPoint& worldPoint);
- /** Converts a Point to world space coordinates. The result is in Points.
- @since v0.7.1
- */
- 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.
- @since v0.7.1
- */
- 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.
- @since v0.7.1
- */
- CCPoint convertToWorldSpaceAR(const CCPoint& nodePoint);
- /** convenience methods which take a CCTouch instead of CCPoint
- @since v0.7.1
- */
- CCPoint convertTouchToNodeSpace(CCTouch * touch);
- /** converts a CCTouch (world coordinates) into a local coordiante. This method is AR (Anchor Relative).
- @since v0.7.1
- */
- CCPoint convertTouchToNodeSpaceAR(CCTouch * touch);
- };
- }//namespace cocos2d
二、摄像机
- class CC_DLL CCCamera : public CCObject
- {
- protected:
- float m_fEyeX;
- float m_fEyeY;
- float m_fEyeZ; //眼睛的位置
- float m_fCenterX;
- float m_fCenterY;
- float m_fCenterZ; //中心点的位置
- float m_fUpX;
- float m_fUpY;
- float m_fUpZ; //摄像机的摆放方向,是正放还是脚朝天的放置
- bool m_bDirty;
- public:
- CCCamera(void);
- ~CCCamera(void);
- void init(void);
- char * description(void);
- /** sets the dirty value */
- inline void setDirty(bool bValue) { m_bDirty = bValue; }
- /** get the dirty value */
- inline bool getDirty(void) { return m_bDirty; }
- /** sets the camera in the default position */
- void restore(void);
- /** Sets the camera using gluLookAt using its eye,center and up_vector */
- void locate(void);
- /** sets the eye values in points */
- void setEyeXYZ(float fEyeX,float fEyeY,float fEyeZ);
- /** sets the center values in points */
- void setCenterXYZ(float fCenterX,float fCenterY,float fCenterZ);
- /** sets the up values */
- void setUpXYZ(float fUpX,float fUpY,float fUpZ);
- /** get the eye vector values in points */
- void getEyeXYZ(float *pEyeX,float *pEyeY,float *pEyeZ);
- /** get the center vector values int points */
- void getCenterXYZ(float *pCenterX,float *pCenterY,float *pCenterZ);
- /** get the up vector values */
- void getUpXYZ(float *pUpX,float *pUpY,float *pUpZ);
- public:
- /** returns the Z eye */
- static float getZEye();
- private:
- DISALLOW_COPY_AND_ASSIGN(CCCamera);
- };
- }//namespace cocos2d
===================================================================
总结:
场景、图层、精灵都继承节点、证明掌握节点是掌握其它一切的基础。
开心一刻:
法官:你为什么要印假钞?
被告无辜地说:因为我不会印真钞。
【麦可网】Cocos2d-X跨平台游戏开发---下载地址:http://pan.baidu.com/s/1kTio1Av
【麦可网】Cocos2d-X跨平台游戏开发---笔记系列:http://blog.csdn.net/qiulanzhu