zoukankan      html  css  js  c++  java
  • cocos2D(五岁以下儿童)---- CCNode

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

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



    节点的处理

    1.创建一个新的节点

    [java] view plaincopy
    1. CCNode *node = [CCNode node];  

    2.加入子节点

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

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

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

    4.删除子节点

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


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

    [java] view plaincopy
    1. [node reorderChild:childNode z:1];  


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

    [java] view plaincopy
    1. [node cleanup];  


    经常使用属性和方法

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

    [java] view plaincopy
    1. @property(nonatomic,readonly) NSInteger zOrder;  

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

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

    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) float rotation;  

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


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

    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) float scale;  

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


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

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

    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) float scaleX;  

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

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

    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) float scaleY;  


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

    [java] view plaincopy
    1. @property (nonatomic,readwrite) CGSize contentSize  

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

    [java] view plaincopy
    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 plaincopy
    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 plaincopy
    1. // 红色(red)是蓝色的子节点,所以是以蓝色的左下角位置为坐标原点(0, 0)。

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

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

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



    2> anchorPoint对缩放的影响

    我先做个总结:

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

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

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

    * anchorPoint为其它值,以此类推


    以下绘图解释一下

    [java] view plaincopy
    1. node.scale = 0.5f; // 宽高变为原来的1/2  
    蓝色代表缩放前,红色代表缩放后


    3> anchorPoint对旋转的影响

    我先做个总结:

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

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

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

    * anchorPoint为其它值。以此类推


    以下绘图解释一下

    [java] view plaincopy
    1. node.rotation = 45// 顺时针旋转45°  

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



    9.这个属性决定了anchorPoint是否要影响position
    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) BOOL isRelativeAnchorPoint;  
    * 假设为YES。代表anchorPoint影响position。假设为NO,代表anchorPoint不影响position,那么节点的左下角就会在position属性指定的位置上

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


    10.父节点

    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) CCNode* parent;  

    11.全部的子节点

    [java] view plaincopy
    1. @property(nonatomic,readonly) CCArray *children;  

    12.是否可见

    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) BOOL visible;  

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

    [java] view plaincopy
    1. @property(nonatomic,readwrite,assign) NSInteger tag;  

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

    [java] view plaincopy
    1. - (CGRect) boundingBox;  


    动作的处理

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

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

    1.运行动作

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

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

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


    2.停止动作

    停止节点上的全部动作

    [java] view plaincopy
    1. -(void) stopAllActions;  
    停止某个特定的动作

    [java] view plaincopy
    1. -(void) stopAction: (CCAction*) action;  
    依据tag停止相应的动作

    [java] view plaincopy
    1. -(void) stopActionByTag:(NSInteger) tag;  

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

    [java] view plaincopy
    1. -(CCAction*) getActionByTag:(NSInteger) tag;  

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

    [java] view plaincopy
    1. -(NSUInteger) numberOfRunningActions;  


    消息调度

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

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

    [java] view plaincopy
    1. // Bullet.h  
    2. #import "CCSprite.h"  
    3.   
    4. @interface Bullet : CCSprite  
    5.   
    6. @end  

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

    [java] view plaincopy
    1. #import "Bullet.h"  
    2.   
    3. @implementation Bullet  
    4. - (id)init {  
    5.     if (self = [super init]) {  
    6.         // 在节点初始化的时候開始消息调度  
    7.         [self scheduleUpdate];  
    8.     }  
    9.     return self;  
    10. }  
    11.   
    12. - (void)update:(ccTime)delta {  
    13.     // 在这里改变子弹的位置  
    14.     // ....  
    15.       
    16. }  
    17. @end  
    调用了scheduleUpdate方法。系统会以每帧的频率调用一次update:方法(方法名和參数都是固定的),意思是每次刷帧都会调用一次。參数delta代表上次调用方法到如今所经过的时间


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

    [java] view plaincopy
    1. -(void) scheduleUpdateWithPriority:(NSInteger)priority;  
    优先级默觉得0,系统是依照优先级从低到高的顺序调用update:方

    以下举个样例:

    [java] view plaincopy
    1. // 节点A  
    2. [nodeA scheduleUpdate];  
    3.           
    4. // 节点B  
    5. [nodeB scheduleUpdateWithPriority:-1];  
    6.           
    7. // 节点C  
    8. [nodeC scheduleUpdateWithPriority:1];  
    节点A、B、C都须要以每帧的频率调用update:方法。可是有顺序之分:最先调用节点B的update:方法,由于节点B的优先级最低;然后调用节点A的update:方法。由于节点A为默认优先级0;最后调用节点C的update:。由于节点C的优先级最高


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

    [java] view plaincopy
    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.   
    10. - (void)changePosition:(ccTime)delta {  
    11.     // do something here  
    12.       
    13. }  


    4.取消消息调度

    取消调用update:方法

    [java] view plaincopy
    1. -(void) unscheduleUpdate;  
    取消调用特定的方法

    [java] view plaincopy
    1. -(void) unschedule: (SEL) s;  
    取消调用全部的方法(包含update:)

    [java] view plaincopy
    1. -(void) unscheduleAllSelectors;  



    原文地址:http://blog.csdn.net/q199109106q/article/details/8599069
    感谢作者~。
  • 相关阅读:
    iOS开发objectc优势与补足
    UITableView详解
    ipad 、iphone开发-通过定时器显示进度条
    [Yii Framework][SHARE] The directory structure of the Yii project site
    [Yii Framework] Yii中事件和行为的区别和应用
    [Ubuntu] 创建桌面启动图标
    [Ubuntu] Access denied for user ‘debiansysmaint’@'localhost’ (using password: YES)
    [Yii Framework] Chive: which is developed base on Yii, and its aims to be an alternative to phpMyAdmin!
    [javascript] 数组扩展操作
    [php] Generate PhpDoc with NetBeans
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4807043.html
Copyright © 2011-2022 走看看