iphone游戏开发之cocos2d(三)CCNode官方API翻译

        CCNode是cocos2d中最重要的一个基类,是一个抽象类,定义了所有节点的公共属性和方法,因为其重要性,所以这里把官方API对CCNode的属性方法描述粗略了翻译下,方便日后查询使用,也希望能帮到大家,如果有的地方描述不准确,大家可以直接参考官方英文原版的说明:点击打开链接

     CCNode是一个重要的元素,所有我们看到绘制下来的东西都是CCNode,最常见的CCNode有:CCScene,CCLayer,CCSprite,CCMenu; 

        CCNode的主要特征有:每个节点都可以包含子节点、可以定时执行回调、可以执行动作;

        部分CCNode子类提供了一些独特的功能;

        子类化一个CCNode通常意味着下面这些操作:重写初始化方法以初始化资源或回调、创建回调来控制时间、重写绘制着色;

         CCNode的特征有:position、scale(x,y)、rotation(in degrees,clockwise)、CCCamera(an interface to gluLookAt)、CCGridBase(to do mesh transformations)、anchor point 、size、visible、z-order、openGL z position

默认值为:rotation:0、position:(x=0,y=0)、scale:(x=1,y=0)、contentSize:(x=0,y=0)、anchorPoint:(x=0,y=0).

成员方法描述:

- (void) addChild:
(CCNode *) node
      以z轴为0在当前容器中添加一个子节点,如果该子节点添加到了一个自在运行的节点,“onEnter"和"onEnterTransitionDidFinish"会直接运行;

- (void) addChild:
(CCNode *) node
z:
(NSInteger) z 
     同上,不过z轴可手动设置; 

- (void) addChild:(CCNode *) nodez:(NSInteger) ztag:(NSInteger) tag 

同上,除了可设置z轴,可给要添加到子节点添加tag,方便在父节点中取;

- (CGRect) boundingBox


返回一个CGRect格式的边界范围,该范围仅与父节点相关; 该范围以坐标形式存在;

补充一下:该方法用来补充getContentSize方法,boundingBox可以获得节点缩放或者旋转之后的范围,getContentSize获取原始节点的大小;

- (void) cleanup
当一个正在运行的节点不再运行时(比如其所依附的场景被切换掉了),这时会发生cleanup事件,这时你要中断所有相关的循环引用,该方法移除所有可能的定时方法,动作等;

- (CGPoint) convertToNodeSpace:(CGPoint) worldPoint

将参数worldPoint转换为node的空间坐标

  补充一下:基本的两个坐标系:屏幕坐标系和GL坐标系。屏幕坐标系x轴朝右,y轴朝下。默认原点在左上角。GL坐标系x轴朝右,y轴朝上。默认原点在左下角。

- (CGPoint) convertToNodeSpaceAR:(CGPoint) worldPoint 

将参数以节点锚点为原点,转换为世界坐标;

- (void) draw
重写该方法以绘制自定义节点,需要用到cocos2d的 GL API,详细信息查看ccGLstate.h;另外,[super draw]不可调用; 

- (CCAction*) getActionByTag:
(NSInteger) tag

通过tag从当前正在运行的动作列表中获取动作;

- (CCNode*) getChildByTag:
(NSInteger) tag
通过tag从容器中取到一个子节点;

- (id) init

初始化节点; 

+ (id) node
声明并初始化一个节点,该节点以autorelease方式创建;

- (CGAffineTransform) nodeToParentTransform


节点的矩阵变换方法,返回在父节点中的空间矩阵坐标;

- (CGAffineTransform) nodeToWorldTransform

同上相反;

- (NSUInteger) numberOfRunningActions
返回当前正在运行的动作个数,包括正在运行的和计划运行的,动作组视为一个动作;例如:运行7个动作组成的动作序列,返回1;运行2个动作组成的七个动作序列,返回7;

- (void) onEnter
当节点进入"stage"时调用,如果伴随着transition,则在transition开始时调用;

- (void) onEnterTransitionDidFinish

当节点进入"stage"时调用,如果节点伴随着transition,则在transition结束后调用; 

- (void) onExit
当节点离开"stage"时调用,如果节点离开时伴随着transition,则在transition结束后调用;

- (void) onExitTransitionDidStart

当节点离开"stage"时调用,如果节点离开时伴随着transition,则在transition开始时调用; 

- (CGAffineTransform) parentToNodeTransform


将父节点中的空间坐标系转换为当前节点坐标系;

- (void) pauseSchedulerAndActions

暂停节点的所有调动和动作,被onExit内部调用;

- (void) removeAllChildrenWithCleanup:
(BOOL) cleanup

移除所有子节点,并根据参数决定是否清空所有动作;

- (void) removeChild:(CCNode *) node cleanup:(BOOL) cleanup 

移除指定的子节点,并根据cleanup参数决定是否清除所有的动作;

- (void) removeChildByTag:(NSInteger) tag cleanup:(BOOL) cleanup 

同上差不多;

- (void) removeFromParentAndCleanup:
(BOOL) cleanup
由本身调用,将其从父节点上移除,并根据cleanup决定是否清空所有动作; 

- (void) reorderChild:
(CCNode *) child
z:
(NSInteger) zOrder
将已存在的子节点重新插入,用于发变z轴;

- (void) resumeSchedulerAndActions


恢复节点的序列方法和动作,可以由onEnter内部调用;

- (CCAction*) runAction:(CCAction *) action

执行动作;

- (void) schedule:(SEL) s

执行一个SEL方法,该方法在每帧时都会调用;

- (void) schedule:
(SEL) s
interval:
(ccTimeseconds 
以seconds参数为判断定时执行一个SEL方法s,如果seconds为0,则该方法会在每一帧都运行,这时我们建议以“scheduleUpdate"方法来代替;

如果方法s已经被定义了,then the interval parameter will be updated without scheduling it again(不太明白);

- (void) schedule:
(SEL) selector
interval:
(ccTimeinterval
repeat:
(uint) repeat
delay:
(ccTimedelay 
重复执行方法repeat+1次,每次执行前会等待delay个时间;

- (void) scheduleOnce:
(SEL) selector
delay:
(ccTimedelay 
延迟delay个时间,执行方法一次;

- (void) scheduleUpdate

check whether a selector is scheduled. 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 "udpate" method could be scheduled per node.

这个方法我怕我翻译不好,大家看下官方说法吧;相当于自动生成的每帧都会执行的方法;
- (void) scheduleUpdateWithPriority:(NSInteger) priority

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 "udpate" selector could be scheduled per node (You can't have 2 'update' selectors).
带优先级的update,并且强调了每个节点上只能定义一个update方法(官方文档上也有错字,哇哈哈);

- (void) sortAllChildren


性能改进,在绘制前对子节点进行排序
- (void) stopAction:
(CCAction *) action
从动作运行序列中移除一个动作;
- (void) stopActionByTag:
(NSInteger) tag

同上
- (void) stopAllActions
从动作运行序列中移除所有动作;

- (void) transform
 position, scale, rotation and other attributes为基础,执行openGL 矩阵坐标转换;
- (void) transformAncestors
同上
- (void) unschedule:(SEL) s
停止指定selector
- (void) unscheduleAllSelectors
停止所有selector方法;
- (void) visit
用于访问并绘制children的递归方法;
- (CGAffineTransform) worldToNodeTransform
用于反转世界矩阵坐标;



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值