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

首先来看看CCNode的继承结构图,仅仅列举了经常使用的类

节点的处理

1.创建一个新的节点

[java] view
plain
copy

  1. CCNode *node = [CCNode node];

2.加入子节点

[java] view
plain
copy

  1. // 先创建子节点
  2. CCNode *childNode = [CCNode node];
  3. // 方法1:直接加入
  4. [node addChild:childNode];
  5. // 方法2:z决定了节点的绘制顺序,依照z值从小到大的顺序来绘制节点,即先绘制z值小的节点。再绘制z值大的节点
  6. // 假设多个节点拥有同样的z值,就依照加入它们的先后顺序进行绘制
  7. [node addChild:childNode z:0];
  8. // 方法3:tag的作用是给节点设置一个标记,父节点能够依据设置的tag标记找到相应的子节点
  9. [node addChild:childNode z:0 tag:100];

3.依据tag找到相应的子节点

[java] view
plain
copy

  1. // 假设多个节点拥有同样的tag值,这种方法将返回最先匹配tag值的节点
  2. [node getChildByTag:100];

4.删除子节点

[java] view
plain
copy

  1. // 方法1:将子节点childNode从父节点node中移除
  2. // "cleanup"设置为YES代表停止子节点执行的全部动作和消息调度
  3. [node removeChild:childNode cleanup:YES];
  4. // 方法2:依据tag值将相应的子节点从父节点node中移除
  5. [node removeChildByTag:100 cleanup:YES];
  6. // 方法3:移除node中的全部子节点
  7. [node removeAllChildrenWithCleanup:YES];
  8. // 方法4:将childNode从它的父节点中移除
  9. [childNode removeFromParentAndCleanup:YES];

5.又一次设置子节点的z值

[java] view
plain
copy

  1. [node reorderChild:childNode z:1];

6.停止节点执行的全部动作和消息调度

[java] view
plain
copy

  1. [node cleanup];

经常使用属性和方法

1.加入节点时设置的z值,决定了节点的绘制顺序

[java] view
plain
copy

  1. @property(nonatomic,readonly) NSInteger zOrder;

2.节点的旋转角度,默认是0,大于0是顺时针旋转,小于0则是逆时针旋转。

子节点会继承父节点的这个属性

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) float rotation;

既然是旋转,肯定是绕着一个点进行旋转。到底是绕着哪个点旋转。取决于anchorPoint

3.节点X和Y方向的缩放比例。同一时候影响宽度和高度。子节点会继承父节点的这个属性

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) float scale;

既然是缩放,肯定是绕着一个点进行缩放,到底是绕着哪个点缩放,取决于anchorPoint

4.节点X方向(即宽度)的缩放比例。

子节点会继承父节点的这个属性

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) float scaleX;

5.节点Y方向(即高度)的缩放比例。

子节点会继承父节点的这个属性

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) float scaleY;

6.节点的大小(不受scale和rotation影响)

[java] view
plain
copy

  1. @property (nonatomic,readwrite) CGSize contentSize

7.节点在父节点中的位置(以父节点左下角为(0, 0))

[java] view
plain
copy

  1. @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

[java] view
plain
copy

  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为其它值,以此类推

以下绘图解释一下

[java] view
plain
copy

  1. // 红色(red)是蓝色的子节点,所以是以蓝色的左下角位置为坐标原点(0, 0)。

    如果蓝色的大小是100x100,红色的大小是50x50

  2. red.position = CGPointMake(50, 50); // 能够看出。(50, 50)是在蓝色的正中间

由于anchorPoint的不同,改变了红色在蓝色中的位置

2> anchorPoint对缩放的影响

我先做个总结:

* 假设anchorPoint = (0, 0),那么节点就会绕着自己的左下角进行缩放

* 假设anchorPoint = (0.5, 0.5),那么节点就会绕着自己的中点进行缩放

* 假设anchorPoint = (1, 1),那么节点就会绕着自己的右上角进行缩放

* anchorPoint为其它值,以此类推

以下绘图解释一下

[java] view
plain
copy

  1. node.scale = 0.5f; // 宽高变为原来的1/2

蓝色代表缩放前,红色代表缩放后

3> anchorPoint对旋转的影响

我先做个总结:

* 假设anchorPoint = (0, 0),那么节点就会绕着自己的左下角进行旋转

* 假设anchorPoint = (0.5, 0.5)。那么节点就会绕着自己的中点进行旋转

* 假设anchorPoint = (1, 1),那么节点就会绕着自己的右上角进行旋转

* anchorPoint为其它值。以此类推

以下绘图解释一下

[java] view
plain
copy

  1. node.rotation = 45; // 顺时针旋转45°

蓝色代表旋转前,红色代表旋转后

9.这个属性决定了anchorPoint是否要影响position

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) BOOL isRelativeAnchorPoint;

* 假设为YES。代表anchorPoint影响position。假设为NO,代表anchorPoint不影响position,那么节点的左下角就会在position属性指定的位置上

* 默认情况下,CCSprite的isRelativeAnchorPoint为YES,CCScene、CCLayer的isRelativeAnchorPoint为NO

10.父节点

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) CCNode* parent;

11.全部的子节点

[java] view
plain
copy

  1. @property(nonatomic,readonly) CCArray *children;

12.是否可见

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) BOOL visible;

13.添加节点时设置的标记

[java] view
plain
copy

  1. @property(nonatomic,readwrite,assign) NSInteger tag;

14.返回节点的边界(包括position和大小)

[java] view
plain
copy

  1. - (CGRect) boundingBox;

动作的处理

动作是指在特定时间内完毕移动、缩放、旋转等操作的行为。

CCNode能够执行动作实现一些动画效果。

1.运行动作

[java] view
plain
copy

  1. -(CCAction*) runAction: (CCAction*) action;
[java] view
plain
copy

  1. // 初始化一个平移动作,这是向左边移动100的距离
  2. CCAction *action = [CCMoveBy actionWithDuration:2 position:CGPointMake(-100, 0)];
  3. // 能够给动作设置一个标记
  4. action.tag = 100;
  5. // 执行动作
  6. [node runAction:action];

当动作运行完成后,会自己主动从节点上删除

2.停止动作

停止节点上的全部动作

[java] view
plain
copy

  1. -(void) stopAllActions;

停止某个特定的动作

[java] view
plain
copy

  1. -(void) stopAction: (CCAction*) action;

依据tag停止相应的动作

[java] view
plain
copy

  1. -(void) stopActionByTag:(NSInteger) tag;

3.依据tag获取相应的动作

[java] view
plain
copy

  1. -(CCAction*) getActionByTag:(NSInteger) tag;

4.节点上当前包括的动作总数

[java] view
plain
copy

  1. -(NSUInteger) numberOfRunningActions;

消息调度

节点能够进行消息调度,也就是指系统会每隔一段时间调用一次节点的某个方法。节点的消息调度是非经常常使用的,比方一个子弹射出去了。我们须要隔一段时间就调用子弹的某个方法来改变的子弹的位置

为了说明消息调度的使用方法。我定义一个子弹类。由于子弹是看得见的。所以应该继承CCSprite,而不是继承CCNode

[java] view
plain
copy

  1. // Bullet.h
  2. #import "CCSprite.h"
  3. @interface Bullet : CCSprite
  4. @end

1.最简单的做法是直接调用节点的scheduleUpdate方法,就能够開始消息调度

[java] view
plain
copy

  1. #import "Bullet.h"
  2. @implementation Bullet
  3. - (id)init {
  4. if (self = [super init]) {
  5. // 在节点初始化的时候開始消息调度
  6. [self scheduleUpdate];
  7. }
  8. return self;
  9. }
  10. - (void)update:(ccTime)delta {
  11. // 在这里改变子弹的位置
  12. // ....
  13. }
  14. @end

当调用了scheduleUpdate方法。系统会以每帧的频率调用一次update:方法(方法名和參数都是固定的),意思是每次刷帧都会调用一次。參数delta代表上次调用方法到如今所经过的时间

2.设置消息调度的优先级

[java] view
plain
copy

  1. -(void) scheduleUpdateWithPriority:(NSInteger)priority;

优先级默觉得0,系统是依照优先级从低到高的顺序调用update:方法

以下举个样例:

[java] view
plain
copy

  1. // 节点A
  2. [nodeA scheduleUpdate];
  3. // 节点B
  4. [nodeB scheduleUpdateWithPriority:-1];
  5. // 节点C
  6. [nodeC scheduleUpdateWithPriority:1];

节点A、B、C都须要以每帧的频率调用update:方法。可是有顺序之分:最先调用节点B的update:方法,由于节点B的优先级最低;然后调用节点A的update:方法。由于节点A为默认优先级0;最后调用节点C的update:。由于节点C的优先级最高

3.假设想在消息调度时调用另外一个方法。或者不想以每帧的频率调用该方法,应该採取以下这样的做法

[java] view
plain
copy

  1. - (id)init {
  2. if (self = [super init]) {
  3. // 開始消息调度
  4. // [self schedule:@selector(changePosition:)];  // 以每帧的频率调用changePosition:方法
  5. [self schedule:@selector(changePosition:) interval:0.2f]; // 每隔0.2秒就调用changePosition:方法
  6. }
  7. return self;
  8. }
  9. - (void)changePosition:(ccTime)delta {
  10. // do something here
  11. }

4.取消消息调度

取消调用update:方法

[java] view
plain
copy

  1. -(void) unscheduleUpdate;

取消调用特定的方法

[java] view
plain
copy

  1. -(void) unschedule: (SEL) s;

取消调用全部的方法(包含update:)

[java] view
plain
copy

  1. -(void) unscheduleAllSelectors;



原文地址:http://blog.csdn.net/q199109106q/article/details/8599069
感谢作者~。

最新文章

  1. c#如何判断webbrowser已经加载完毕
  2. 深入浅出WPF开发下载
  3. Oracle User Calls 和 Executions 两个概念的区别
  4. poj2407
  5. Linux 串口编程(转)
  6. FastDFS 安装
  7. PTPX Power Analysis Flow
  8. bind,unbing,on,live,delegate绑定和解绑事件
  9. Hadoop:使用Mrjob框架编写MapReduce
  10. How to move the user document folder to D disk[Windows 7]
  11. [ ] 字符组(Character Classes) (转)
  12. 如何学习php之吐槽
  13. 【JAVA零基础入门系列】Day14 Java对象的克隆
  14. MYSQL DISTINCT Optimization
  15. Linux系统网络性能实例分析
  16. Java第一次实训作业
  17. Unity 自定义导入时切割Sprite
  18. squid故障汇总
  19. [android] 表格布局和绝对布局
  20. SQL 中用户定义函数的使用方法

热门文章

  1. Swift 的类、结构体、枚举等的构造过程Initialization(下)
  2. DirectX Sample-ConfigSystem中采用配置文件进行游戏设置
  3. auto property synthesis will not synthesize proterty ;it will be implementedby its superclass, use @
  4. 【spring源代码分析】--Bean的解析与注冊
  5. 《Javascript高级程序设计》读书笔记之闭包
  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
  7. iOS Dev (55) 获得本年度、月、日本和其他信息
  8. JAVA: httpclient 具体解释——第五章;
  9. Perl中的单行凝视和多行凝视
  10. Div 滚动栏滚动到指定的位置