您的位置:首页 > 移动开发 > Cocos引擎

cocos2d学习之CCNode

2014-11-13 18:19 375 查看
CCNode是所有能展示在屏幕上的cocos2d对象的基类。所有节点按照树形结构组织,并且通常以场景(CCScene)对象为根节点。常用的场景(CCScene),精灵(CCSprite),按钮(CCButton)都是节点的例证。CCNode处理转换,为其子节点提供坐标系统。任何CCNode以及子类都可以处理用户输入或者响应用户触摸(从它继承自CCResponder就能看出来)。

下面谈一下坐标系统以及定位。在CCNode的坐标系统中,坐标点通过position属性值来设置。点度量提供了一个处理不同屏幕密度的方法。例如,在视网膜屏上,一个点等同于两个像素;在非视网膜屏上一个点等同于一个像素。使用positionType这个属性,可以指定节点位置如何被解释。例如,设置节点位置类型为CCPositionTypeNormalized,当节点位置属性值为(0.5,0.5)时,该节点将会被放置在父容器的中间位置.

CCNode的属性:

@property (nonatomic, assign) BOOL paused//如果为真,则没有回调函数被调用,没有动作会被执行

@property
(nonatomic, readwrite, assign) BOOL visible//是否显示 默认为yes

@property
(nonatomic, strong) NSString *name//节点的名称 用来快速寻找节点

@property
(nonatomic, readwrite, strong) id userObject//和userData相似

@property
(nonatomic, readwrite, assign) CGPoint position//节点的位置坐标

@property
(nonatomic, readonly) CGPoint positionInPoints//点坐标系(原点从左下角开始)中的坐标值

@property
(nonatomic, readwrite, assign) CCPositionType positionType//位置类型

@property
(nonatomic, readwrite, assign) float rotation//节点旋转角度

@property
(nonatomic, readwrite, assign) float scale//节点缩放比例 同时影响x和y坐标 1值为1时原比例,2是放大两倍,0.5时一半

@property
(nonatomic, readwrite, assign) float scaleX//节点x轴缩放比例

@property
(nonatomic, readwrite, assign) float scaleY//节点y轴缩放比例

@property
(nonatomic, readwrite, assign) CGSize contentSize//节点的尺寸

@property
(nonatomic, readwrite) CGPoint anchorPoint//锚点 节点的锚点在节点位置的比例,比如说一个节点的坐标为(0, 0),宽高各为100,那么当它的锚点坐标为(50, 50)的时候,anchorPoint_.x = 0.5,anchorPoint_.y = 0.5。

@property
(nonatomic, readwrite, assign) float skewX//节点横向扭曲的角度

@property
(nonatomic, readwrite, assign) float skewY//节点纵向扭曲的角度

@property
(nonatomic, readwrite, unsafe_unretained) CCNode *parent//节点的父节点

@property
(nonatomic, readonly) NSArray *children//节点的子节点集合

-
(CGRect)boundingBox//返回节点的碰撞框。由坐标和宽高组成。这个值会随着节点的旋转和缩放而改变

@property
(nonatomic, readonly) CCScene *scene//返回该节点加入的场景,如果没有被加入任何场景,则返回nil

@property
(nonatomic, strong) CCPhysicsBody *physicsBody//返回该节点附加的物理主体

@property
(nonatomic, readonly, getter=isRunningInActiveScene) BOOL runningInActiveScene//是否被加入了一个正在活动的场景,并且它和它的任意祖先都不是pause状态

CCNode的方法

+
(id)node//创建一个节点

-
(id)init//实例化一个节点

-
(void)addChild:(CCNode *)node//添加一个子节点 z-order为0 当被加入正在运行的节点时,onEnter’ and ‘onEnterTransitionDidFinish方法会被立即调用

-
(void)addChild:(CCNode *)node z:(NSInteger)z//
添加一个子节点 指定z值

- (void)addChild:(CCNode *)node z:(NSInteger)z name:(NSString
*)name// 添加一个子节点 指定z值和名字tag

- (void)removeFromParent//将自身从父容器中移除 并清除该节点注册的所有动作

- (void)removeFromParentAndCleanup:(BOOL)cleanup// 将自身从父容器中移除
并决定是否清除该节点注册的所有动作

- (void)removeChild:(CCNode *)child//移除某个子节点
并清除子节点的相关动作 该方法为检测参数节点是否是本节点的子节点

- (void)removeChild:(CCNode *)node cleanup:(BOOL)cleanup//移除某个子节点
并决定是否清除子节点的相关动作

-
(void)removeChildByName:(NSString *)name//通过名字移除某个子节点 并清除节点的相关动作

-
(void)removeChildByName:(NSString *)name cleanup:(BOOL)cleanup//通过名字移除某个子节点
并决定是否清除节点的相关动作

-
(void)removeAllChildren//移除所有子节点 并清除子节点的动作

-
(void)removeAllChildrenWithCleanup:(BOOL)cleanup 移除所有子节点 并决定是否清除子节点的动作

-
(CCNode *)getChildByName:(NSString *)name recursively:(bool)isRecursive//是否递归寻找名字匹配的子节点,如果为真,则会一直递归寻找下去(子节点寻找子节点);如果为假,则立即返回匹配的子节点

-
(BOOL)hitTestWithWorldPos:(CGPoint)pos//检测给出的位置点是否在节点内

-
(void)onEnter//当节点进入舞台的时候触发的事件

-
(void)onEnterTransitionDidFinish//当节点进入舞台,并且伴随的动画结束的时候

-
(void)onExit//当节点离开舞台的时候触发的事件

-
(void)onExitTransitionDidStart//当节点离开舞台,并伴随有动画,在动画的开始的时候触发的事件

-
(CCAction *)runAction:(CCAction *)action//执行一个动作,并返回该动作 该节点成为动作的目标

-
(void)stopAllActions//把正在运行的动作列表上的动作全部移除

-
(void)stopAction:(CCAction *)action//把正在运行的动作列表上的某个动作移除

-
(void)stopActionByTag:(NSInteger)tag//通过tag值把正在运行的动作列表上的某个动作移除

-
(CCAction *)getActionByTag:(NSInteger)tag//根据tag值获得某个动作

-
(NSUInteger)numberOfRunningActions//正在运行动作的个数

-
(CCTimer *)scheduleBlock:(CCTimerBlock)block delay:(CCTime)delay//一段时间后执行代码块

-
(CCTimer *)schedule:(SEL)s interval:(CCTime)seconds//隔一段时间执行方法
如果seconds为0,则每帧都执行

-
(CCTimer *)schedule:(SEL)selector interval:(CCTime)interval repeat:(NSUInteger)repeat delay:(CCTime)delay//重复执行方法repeat+1次,每次执行前会等待delay个时间

-
(CCTimer *)scheduleOnce:(SEL)selector delay:(CCTime)delay//在一段时间之后执行一个方法
只执行一次

-
(void)unschedule:(SEL)selector//终止执行某个方法

-
(void)unscheduleAllSelectors//终止执行所有方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: