节点类CCNode可以说是游戏元素的祖宗了,基本上我们看得到的游戏元素都是以它为原型进行扩展的。像CCScene,CCLayer,CCSprite,CCMenu,CCSpriteBatchNode等等都是从CCNode继承而来。另外如果我们要自定义精灵,那么从CCNode继承也是一个很不错的选择。
概况
CCNode直接从CCObject继承而来,有如下几个特点:
(1)可以包含其他CCNode节点,可以进行添加/获取/删除子节点操作。
(2)可以执行周期性的回调任务。
(3)可以执行动作。
一些子类化的节点提供了更为丰富的特性和功能。
属性
Features of CCNode:
- position //位置,默认(0,0)
- scale (x, y) //缩放,默认(1,1)
- rotation (in degrees, clockwise) //旋转,默认为0
- skew //倾斜,默认为0
- CCCamera (an interface to gluLookAt ) //CCCamera,视点转换,每个节点都有,默认指向节点中心
- CCGridBase (to do mesh transformations)//CCGridBase,网类转变
- anchor point //锚点,默认(0,0)
- size //尺寸,默认(0,0)
- visible//可见
- z-order//z轴值
- openGL z position //OpenGL z值
接口
初始化
1 2 3 4 5 6 7 8
| virtual bool init();
static CCNode * create(void);
const char* description(void);
|
图形属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| virtual void setZOrder(int zOrder); virtual void _setZOrder(int z); virtual int getZOrder(); virtual void setVertexZ(float vertexZ); virtual float getVertexZ(); virtual void setScaleX(float fScaleX); virtual float getScaleX(); 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); virtual void setPositionX(float x); virtual float getPositionX(void); virtual void setPositionY(float y); virtual float getPositionY(void); virtual void setSkewX(float fSkewX); virtual float getSkewX(); 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(); virtual void setRotationX(float fRotaionX); virtual float getRotationX(); virtual void setRotationY(float fRotationY); virtual float getRotationY();
|
节点操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| virtual void addChild(CCNode * child); virtual void addChild(CCNode * child, int zOrder); virtual void addChild(CCNode* child, int zOrder, int tag); CCNode * getChildByTag(int tag); virtual CCArray* getChildren(); unsigned int getChildrenCount(void) const; virtual void setParent(CCNode* parent); virtual CCNode* getParent(); virtual void removeFromParent(); virtual void removeFromParentAndCleanup(bool cleanup); virtual void removeChild(CCNode* child); virtual void removeChild(CCNode* child, bool cleanup); virtual void removeChildByTag(int tag); virtual void removeChildByTag(int tag, bool cleanup); virtual void removeAllChildren(); virtual void removeAllChildrenWithCleanup(bool cleanup); virtual void reorderChild(CCNode * child, int zOrder);
|
标签和用户数据
1 2 3 4 5 6 7 8 9 10 11
| 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);
|
事件回调
1 2 3 4 5 6 7 8 9 10 11
| virtual void onEnter(); virtual void onEnterTransitionDidFinish(); virtual void onExit(); virtual void onExitTransitionDidStart(); virtual void cleanup(void);
|
动作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| virtual void setActionManager(CCActionManager* actionManager); virtual CCActionManager* getActionManager(); CCAction* runAction(CCAction* action); void stopAllActions(void); void stopAction(CCAction* action); void stopActionByTag(int tag); CCAction* getActionByTag(int tag); unsigned int numberOfRunningActions(void);
|
调度器和定时器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| virtual void setScheduler(CCScheduler* scheduler); virtual CCScheduler* getScheduler(); bool isScheduled(SEL_SCHEDULE selector); void scheduleUpdate(void); void scheduleUpdateWithPriority(int priority); void unscheduleUpdate(void); 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); virtual void update(float delta);
|
坐标转换
1 2 3 4 5 6 7
| CCPoint convertToNodeSpace(const CCPoint& worldPoint); CCPoint convertToWorldSpace(const CCPoint& nodePoint); CCPoint convertToNodeSpaceAR(const CCPoint& worldPoint); CCPoint convertToWorldSpaceAR(const CCPoint& nodePoint); CCPoint convertTouchToNodeSpace(CCTouch * touch); CCPoint convertTouchToNodeSpaceAR(CCTouch * touch);
|
其他
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| virtual CCGLProgram* getShaderProgram(); virtual void setShaderProgram(CCGLProgram *pShaderProgram);
virtual CCCamera* getCamera();
virtual bool isRunning();
virtual void draw(void);
virtual void visit(void);
CCRect boundingBox(void);
|
CCNodeRGBA
CCNodeRGBA继承于CCNode,所以它拥有CCNode的所有特性,并且它也继承于CCRGBAProtocol。从名字看来我们就知道它是一个带有颜色和透明度的节点。
所以它比起CCNode就多了2个特性,Opacity和RGB值。
如果要给子节点传递透明度属性,那么需要设置setCascadeOpacityEnabled(true),如果传递的过程中遇到了CCNode,那么传递会中断。颜色值的传递也是一样的道理。