节点类CCNode可以说是游戏元素的祖宗了,基本上我们看得到的游戏元素都是以它为原型进行扩展的。像CCScene,CCLayer,CCSprite,CCMenu,CCParticleBatchNode等等都是从CCNode继承而来。另外如果我们要自定义精灵,那么从CCNode继承也是一个很不错的选择。CCNode直接从CCObject继承而来,有如下几个特点:
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值
//初始化函数,成功返回true
virtual bool init();
//分配内存空间,调用init并添加autoRelease标记
static CCNode * create(void);
//返回描述字符串
const char* description(void);
//设置/获取Z轴顺序,Z轴大的覆盖Z轴小的
virtual void setZOrder(int zOrder);
virtual void _setZOrder(int z);
virtual int getZOrder();
//设置/获取OpenGL Z轴顶点
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();
//添加/获取子节点,可以带Z轴顺序(默认为0)和标签
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();
//从父节点中移除自身,默认cleanup为true
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);
//设置/获取tag
virtual int getTag() const;
virtual void setTag(int nTag);
//设置/获取userdata,它是一个指针可以指向你想要的任意数据块,不过记得要释放
virtual void* getUserData();
virtual void setUserData(void *pUserData);
//设置/获取CCObject,和上面一样,只是数据换成了CCObject对象
virtual CCObject* getUserObject();
virtual void setUserObject(CCObject *pUserObject);
//节点开始进入触发
virtual void onEnter();
//节点完成进入触发
virtual void onEnterTransitionDidFinish();
//节点退出触发
virtual void onExit();
//如果节点退出有过渡动画,动画开始时触发
virtual void onExitTransitionDidStart();
//停止动画和调度器
virtual void cleanup(void);
//获取/设置动作管理器
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);
//获取/设置调度器
virtual void setScheduler(CCScheduler* scheduler);
virtual CCScheduler* getScheduler();
//检测某个调度器是否有在运行
bool isScheduled(SEL_SCHEDULE selector);
//开启update调度
void scheduleUpdate(void);
//设置调度优先级
void scheduleUpdateWithPriority(int priority);
//关闭update调度器
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);
//坐标转换相关,这一部分后面再介绍
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);
//获取/设置着色程序
virtual CCGLProgram* getShaderProgram();
virtual void setShaderProgram(CCGLProgram *pShaderProgram);
//获取CCCamera对象
virtual CCCamera* getCamera();
//节点是否在运行
virtual bool isRunning();
//绘制节点
virtual void draw(void);
//递归访问节点
virtual void visit(void);
//返回所占矩形,节点坐标系
CCRect boundingBox(void);
Node API详解