本将主要介绍下CCNode这个类。CCNode是全部节点的基类,当中包含我们经常使用的CCScene(场景)、CCLayer(图层)、CCSprite(精灵)等。它是一个不可以可视化显示的抽象类,仅仅是用来定义全部节点的公共属性和方法的。本讲纯粹是理论。
首先来看看CCNode的继承结构图,仅仅列举了经常使用的类

节点的处理
1.创建一个新的节点
-
CCNode *node = [CCNode node];
2.加入子节点
-
-
CCNode *childNode = [CCNode node];
-
-
-
[node addChild:childNode];
-
-
-
-
[node addChild:childNode z:0];
-
-
-
[node addChild:childNode z:0 tag:100];
3.依据tag找到相应的子节点
-
-
[node getChildByTag:100];
4.删除子节点
-
-
-
[node removeChild:childNode cleanup:YES];
-
-
-
[node removeChildByTag:100 cleanup:YES];
-
-
-
[node removeAllChildrenWithCleanup:YES];
-
-
-
[childNode removeFromParentAndCleanup:YES];
5.又一次设置子节点的z值
-
[node reorderChild:childNode z:1];
6.停止节点执行的全部动作和消息调度
经常使用属性和方法
1.加入节点时设置的z值,决定了节点的绘制顺序
-
@property(nonatomic,readonly) NSInteger zOrder;
2.节点的旋转角度,默认是0,大于0是顺时针旋转,小于0则是逆时针旋转。子节点会继承父节点的这个属性
-
@property(nonatomic,readwrite,assign) float rotation;
既然是旋转,肯定是绕着一个点进行旋转。到底是绕着哪个点旋转。取决于anchorPoint
3.节点X和Y方向的缩放比例。同一时候影响宽度和高度。子节点会继承父节点的这个属性
-
@property(nonatomic,readwrite,assign) float scale;
既然是缩放,肯定是绕着一个点进行缩放,到底是绕着哪个点缩放,取决于anchorPoint
4.节点X方向(即宽度)的缩放比例。子节点会继承父节点的这个属性
-
@property(nonatomic,readwrite,assign) float scaleX;
5.节点Y方向(即高度)的缩放比例。
子节点会继承父节点的这个属性
-
@property(nonatomic,readwrite,assign) float scaleY;
6.节点的大小(不受scale和rotation影响)
-
@property (nonatomic,readwrite) CGSize contentSize
7.节点在父节点中的位置(以父节点左下角为(0, 0))
-
@property(nonatomic,readwrite,assign) CGPoint position;
cocos2d的坐标系:(0,0)在屏幕的左下角。x值向右正向延伸,y值向上正向延伸.
winSize代表屏幕的尺寸

认真思考一下,不难发现,事实上position的含义还是非常模糊的。
如果一个节点的大小是20x20。则包括了400个点,那么在400个点中到底是哪个点在position属性指定的位置上呢?
这个就取决于anchorPoint和isRelativeAnchorPoint属性,假设isRelativeAnchorPoint为NO,节点的左下角会在position属性指定的位置上;假设isRelativeAnchorPoint为YES。position的含义还会受anchorPoint的影响
8.能够称之为"定位点",这个anchorPoint影响的东西非常多,比方节点position的含义、节点绕着哪个点进行缩放或旋转。anchorPoint的x、y取值范围都是0到1
-
@property(nonatomic,readwrite) CGPoint anchorPoint;
默认情况下,CCSprite、CClayer、CCScene的anchorPoint都为(0.5, 0.5),即默认情况下它们的定位点都是自己的中心点。
以下我分别具体描写叙述下anchorPoint对position、缩放、旋转的影响
1> anchorPoint对position的影响
anchorPoint要对position造成影响,前提条件是isRelativeAnchorPoint为YES
我先做个总结:
* 假设anchorPoint = (0, 0),那么节点的左下角就会在position属性指定的位置上
* 假设anchorPoint = (0.5, 0.5),那么节点的中心点就会在position属性指定的位置上
* 假设anchorPoint = (1, 1),那么节点的右上角就会在position属性指定的位置上
* anchorPoint为其它值,以此类推
以下绘图解释一下
-
-
red.position = CGPointMake(50, 50);
由于anchorPoint的不同,改变了红色在蓝色中的位置

2> anchorPoint对缩放的影响
我先做个总结:
* 假设anchorPoint = (0, 0),那么节点就会绕着自己的左下角进行缩放
* 假设anchorPoint = (0.5, 0.5),那么节点就会绕着自己的中点进行缩放
* 假设anchorPoint = (1, 1),那么节点就会绕着自己的右上角进行缩放
* anchorPoint为其它值,以此类推
以下绘图解释一下
蓝色代表缩放前,红色代表缩放后

3> anchorPoint对旋转的影响
我先做个总结:
* 假设anchorPoint = (0, 0),那么节点就会绕着自己的左下角进行旋转
* 假设anchorPoint = (0.5, 0.5)。那么节点就会绕着自己的中点进行旋转
* 假设anchorPoint = (1, 1),那么节点就会绕着自己的右上角进行旋转
* anchorPoint为其它值。以此类推
以下绘图解释一下
蓝色代表旋转前,红色代表旋转后

9.这个属性决定了anchorPoint是否要影响position
-
@property(nonatomic,readwrite,assign) BOOL isRelativeAnchorPoint;
* 假设为YES。代表anchorPoint影响position。假设为NO,代表anchorPoint不影响position,那么节点的左下角就会在position属性指定的位置上
* 默认情况下,CCSprite的isRelativeAnchorPoint为YES,CCScene、CCLayer的isRelativeAnchorPoint为NO
10.父节点
-
@property(nonatomic,readwrite,assign) CCNode* parent;
11.全部的子节点
-
@property(nonatomic,readonly) CCArray *children;
12.是否可见
-
@property(nonatomic,readwrite,assign) BOOL visible;
13.添加节点时设置的标记
-
@property(nonatomic,readwrite,assign) NSInteger tag;
14.返回节点的边界(包括position和大小)
动作的处理
动作是指在特定时间内完毕移动、缩放、旋转等操作的行为。
CCNode能够执行动作实现一些动画效果。
1.运行动作
-
-(CCAction*) runAction: (CCAction*) action;
-
-
CCAction *action = [CCMoveBy actionWithDuration:2 position:CGPointMake(-100, 0)];
-
-
action.tag = 100;
-
-
[node runAction:action];
当动作运行完成后,会自己主动从节点上删除
2.停止动作
停止节点上的全部动作
停止某个特定的动作
-
-(void) stopAction: (CCAction*) action;
依据tag停止相应的动作
-
-(void) stopActionByTag:(NSInteger) tag;
3.依据tag获取相应的动作
-
-(CCAction*) getActionByTag:(NSInteger) tag;
4.节点上当前包括的动作总数
-
-(NSUInteger) numberOfRunningActions;
消息调度
节点能够进行消息调度,也就是指系统会每隔一段时间调用一次节点的某个方法。节点的消息调度是非经常常使用的,比方一个子弹射出去了。我们须要隔一段时间就调用子弹的某个方法来改变的子弹的位置
为了说明消息调度的使用方法。我定义一个子弹类。由于子弹是看得见的。所以应该继承CCSprite,而不是继承CCNode
-
-
#import "CCSprite.h"
-
-
@interface Bullet : CCSprite
-
-
@end
1.最简单的做法是直接调用节点的scheduleUpdate方法,就能够開始消息调度
-
#import "Bullet.h"
-
-
@implementation Bullet
-
- (id)init {
-
if (self = [super init]) {
-
-
[self scheduleUpdate];
-
}
-
return self;
-
}
-
-
- (void)update:(ccTime)delta {
-
-
-
-
}
-
@end
当调用了scheduleUpdate方法。系统会以每帧的频率调用一次update:方法(方法名和參数都是固定的),意思是每次刷帧都会调用一次。參数delta代表上次调用方法到如今所经过的时间
2.设置消息调度的优先级
-
-(void) scheduleUpdateWithPriority:(NSInteger)priority;
优先级默觉得0,系统是依照优先级从低到高的顺序调用update:方法
以下举个样例:
-
-
[nodeA scheduleUpdate];
-
-
-
[nodeB scheduleUpdateWithPriority:-1];
-
-
-
[nodeC scheduleUpdateWithPriority:1];
节点A、B、C都须要以每帧的频率调用update:方法。可是有顺序之分:最先调用节点B的update:方法,由于节点B的优先级最低;然后调用节点A的update:方法。由于节点A为默认优先级0;最后调用节点C的update:。由于节点C的优先级最高
3.假设想在消息调度时调用另外一个方法。或者不想以每帧的频率调用该方法,应该採取以下这样的做法
-
- (id)init {
-
if (self = [super init]) {
-
-
-
[self schedule:@selector(changePosition:) interval:0.2f];
-
}
-
return self;
-
}
-
-
- (void)changePosition:(ccTime)delta {
-
-
-
}
4.取消消息调度
取消调用update:方法
-
-(void) unscheduleUpdate;
取消调用特定的方法
-
-(void) unschedule: (SEL) s;
取消调用全部的方法(包含update:)
-
-(void) unscheduleAllSelectors;
原文地址:http://blog.csdn.net/q199109106q/article/details/8599069
感谢作者~。