Cocos2d API 解析之AtlasSpriteTest

本文深入探讨了AtlasSprite Manager的特性、优势及使用方法,并提供了几个具体的应用示例,包括精灵动画、颜色与透明度控制、层级顺序调整等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AtlasSpriteManager :定义一个的精灵集合
AtlasSprite是一个CocosNode对象实现CocosNodeFrames和CocosNodeRGBA协议。
 

AtlasSprite可以作为替代的Sprite。

AtlasSprite has all the features from CocosNode with the following additions and limitations:

  • New features(新的特征)
    • It is MUCH faster than Sprite
    • supports flipX, flipY

 

  • Limitations(一些限制)
    • Their parent can only be an AtlasSpriteManager
    • They can't have children
    • Camera is not supported yet (eg: OrbitCamera action doesn't work)
    • GridBase actions are not supported (eg: Lens, Ripple, Twirl)
    • The Alias/Antialias property belongs to AtlasSpriteManager, so you can't individually set the aliased property.
    • The Blending function property belongs to AtlasSpriteManager, so you can't individually set the blending function property.
    • Parallax scroller is not supported, but can be simulated with a "proxy" sprite.

 

例子一
-(id) init
{
    if( (self=[super init]) ) {
       
        self.isTouchEnabled = YES;  //TouchEnabled事件可用

  //设定AtlasSpriteManager
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:50];
        [self addChild:mgr z:0 tag:kTagSpriteManager];  //设定AtlasSpriteManager的引用值kTagSpriteManager,方便后面调用
       
        //设定木sprite的座标点(ccp)
        CGSize s = [[Director sharedDirector] winSize];
       //定用自定义的方法 将定议的ccp传入过去
        [self addNewSpriteWithCoords:ccp(s.width/2, s.height/2)];
       
    }   
    return self;
}
-(void) addNewSpriteWithCoords:(CGPoint)p
{
    AtlasSpriteManager *mgr = (AtlasSpriteManager*) [self getChildByTag:kTagSpriteManager];    //通过kTagSpriteManager调用AtlasSpriteManager值
   
  int idx = CCRANDOM_0_1() * 1400 / 100;  //定义一个0-14之间的随机数以便随机产生一个精灵
    int x = (idx%5) * 85;    //5:每行5个  宽度为85
    int y = (idx/5) * 121;    //高度为121
    //以此来确定点的x,y座标
   
    //通过CGRectMake值从spriteManager中取得相应的精灵
   ///CGRect CGRectMake ( float x, float y, float width, float height )
    AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(x,y,85,121) spriteManager:mgr];
    [mgr addChild:sprite];  //添加精灵

    sprite.position = ccp( p.x, p.y); 

    id action;
     float rand = CCRANDOM_0_1();  //取0-1之间的随机数
   
   //当rand值在不同的范围就执行不同的动作
    if( rand < 0.20 )
        action = [ScaleBy actionWithDuration:3 scale:2];
    else if(rand < 0.40)
        action = [RotateBy actionWithDuration:3 angle:360];
    else if( rand < 0.60)
        action = [Blink actionWithDuration:1 blinks:3];
    else if( rand < 0.8 )
        action = [TintBy actionWithDuration:2 red:0 green:-255 blue:-255];
    else
        action = [FadeOut actionWithDuration:2];
    id action_back = [action reverse];
    id seq = [Sequence actions:action, action_back, nil];
   
    [sprite runAction: [RepeatForever actionWithAction:seq]];
}

//设置ccTouchesEnded事件
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for( UITouch *touch in touches ) {

       //获得当前touch的位置
        CGPoint location = [touch locationInView: [touch view]];
       
        location = [[Director sharedDirector] convertCoordinate: location];
       
       //重新调用函数执行
        [self addNewSpriteWithCoords: location];
    }
    return kEventHandled;
}

例子二
@implementation Atlas2

-(id) init
{
    if( (self=[super init]) ) {
       
        AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:50];
        [self addChild:mgr z:0 tag:kTagSpriteManager];       
       
        AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite2 = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite3 = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 85, 121) spriteManager: mgr];
       
        //为sprite增加一个动画
        AtlasAnimation *animation = [AtlasAnimation animationWithName:@"dance" delay:0.2f];
        for(int i=0;i<14;i++) {
            int x= i % 5;
            int y= i / 5;
         //增加了一个帧的动画   
        [animation addFrameWithRect: CGRectMake(x*85, y*121, 85, 121) ];

        }
       
        [mgr addChild:sprite];
        [mgr addChild:sprite2];
        [mgr addChild:sprite3];
       
       //设置精灵在场景中的位置
       CGSize s = [[Director sharedDirector] winSize];
        sprite.position = ccp( s.width /2, s.height/2);
        sprite2.position = ccp( s.width /2 - 100, s.height/2);
        sprite3.position = ccp( s.width /2 + 100, s.height/2);
       
        //定义三个一样的动画
        id action = [Animate actionWithAnimation: animation];
        id action2 = [[action copy] autorelease];
        id action3 = [[action copy] autorelease];
       
        sprite.scale = 0.5f;
        sprite2.scale = 1.0f;
        sprite3.scale = 1.5f;
       
        [sprite runAction:action];
        [sprite2 runAction:action2];
        [sprite3 runAction:action3];
       
       
    }   
    return self;
}

-(NSString *) title
{
    return @"AtlasSprite: Animation";
}
@end
  
例子三

#pragma mark Example Atlas 3

@implementation Atlas3

-(id) init
{
    if( (self=[super init]) ) {
       
        // small capacity. Testing resizing.
        // Don't use capacity=1 in your real game. It is expensive to resize the capacity
        AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:1];
        [self addChild:mgr z:0 tag:kTagSpriteManager];       
       
      //注意以下图片的取法,Y值不变的情况下 x值变化
        AtlasSprite *sprite1 = [AtlasSprite spriteWithRect:CGRectMake(85*0, 121*1, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite2 = [AtlasSprite spriteWithRect:CGRectMake(85*1, 121*1, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite3 = [AtlasSprite spriteWithRect:CGRectMake(85*2, 121*1, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite4 = [AtlasSprite spriteWithRect:CGRectMake(85*3, 121*1, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite5 = [AtlasSprite spriteWithRect:CGRectMake(85*0, 121*1, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite6 = [AtlasSprite spriteWithRect:CGRectMake(85*1, 121*1, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite7 = [AtlasSprite spriteWithRect:CGRectMake(85*2, 121*1, 85, 121) spriteManager: mgr];
        AtlasSprite *sprite8 = [AtlasSprite spriteWithRect:CGRectMake(85*3, 121*1, 85, 121) spriteManager: mgr];
       
        CGSize s = [[Director sharedDirector] winSize];
        //注意点位置的设法
        sprite1.position = ccp( (s.width/5)*1, (s.height/3)*1);
        sprite2.position = ccp( (s.width/5)*2, (s.height/3)*1);
        sprite3.position = ccp( (s.width/5)*3, (s.height/3)*1);
        sprite4.position = ccp( (s.width/5)*4, (s.height/3)*1);
        sprite5.position = ccp( (s.width/5)*1, (s.height/3)*2);
        sprite6.position = ccp( (s.width/5)*2, (s.height/3)*2);
        sprite7.position = ccp( (s.width/5)*3, (s.height/3)*2);
        sprite8.position = ccp( (s.width/5)*4, (s.height/3)*2);

        id action = [FadeIn actionWithDuration:2];
        id action_back = [action reverse];
        id fade = [RepeatForever actionWithAction: [Sequence actions: action, action_back, nil]];

        id tintred = [TintBy actionWithDuration:2 red:0 green:-255 blue:-255];
        id tintred_back = [tintred reverse];
        id red = [RepeatForever actionWithAction: [Sequence actions: tintred, tintred_back, nil]];

        id tintgreen = [TintBy actionWithDuration:2 red:-255 green:0 blue:-255];
        id tintgreen_back = [tintgreen reverse];
        id green = [RepeatForever actionWithAction: [Sequence actions: tintgreen, tintgreen_back, nil]];

        id tintblue = [TintBy actionWithDuration:2 red:-255 green:-255 blue:0];
        id tintblue_back = [tintblue reverse];
        id blue = [RepeatForever actionWithAction: [Sequence actions: tintblue, tintblue_back, nil]];
       
       
        [sprite5 runAction:red];
        [sprite6 runAction:green];
        [sprite7 runAction:blue];
        [sprite8 runAction:fade];
       
        // late add: test dirtyColor and dirtyPosition
        [mgr addChild:sprite1 z:0 tag:kTagSprite1];
        [mgr addChild:sprite2 z:0 tag:kTagSprite2];
        [mgr addChild:sprite3 z:0 tag:kTagSprite3];
        [mgr addChild:sprite4 z:0 tag:kTagSprite4];
        [mgr addChild:sprite5 z:0 tag:kTagSprite5];
        [mgr addChild:sprite6 z:0 tag:kTagSprite6];
        [mgr addChild:sprite7 z:0 tag:kTagSprite7];
        [mgr addChild:sprite8 z:0 tag:kTagSprite8];
       
       
        [self schedule:@selector(removeAndAddSprite:) interval:2];
       
    }   
    return self;
}

// this function test if remove and add works as expected:
//   color array and vertex array should be reindexed
//展示定时删除和添加一个精灵
-(void) removeAndAddSprite:(ccTime) dt
{
    id mgr = [self getChildByTag:kTagSpriteManager];
    id sprite = [mgr getChildByTag:kTagSprite5];
   
    [sprite retain];

    [mgr removeChild:sprite cleanup:NO];
    [mgr addChild:sprite z:0 tag:kTagSprite5];
   
    [sprite release];
}

-(NSString *) title
{
    return @"AtlasSprite: Color & Opacity";
}
@end

例子四:AtlasSprite: Z order
-(void) reorderSprite:(ccTime) dt
{
    id mgr = [self getChildByTag:kTagSpriteManager];  //众Layer中得到
SpriteManager
    id sprite = [mgr getChildByTag:kTagSprite1];  //从 SpriteManager得到精灵
   
    int z = [sprite zOrder];
   
    if( z < -1 )
        dir = 1;
    if( z > 10 )
        dir = -1;
   
    z += dir * 3;

    //重新更改精灵的显示顺序,顺序越大显示越前面
    [mgr reorderChild:sprite z:z];
   
}

例子五:AtlasZVertex

  
      for(int i=0;i<5;i++) {
            AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(85*0, 121*1, 85, 121) spriteManager: mgr];
            sprite.position = ccp( 50 + i*40, s.height/2);
            //形成队列效果,vertexz值越大精灵越大,显示越前面
            sprite.vertexZ = 10 + i*40;  
            [mgr addChild:sprite z:0];
           
        }

//转动特效
[self runAction:[OrbitCamera actionWithDuration:10 radius: 1 deltaRadius:0 angleZ:0 deltaAngleZ:360 angleX:0 deltaAngleX:0]];


例子六:anchorPoint

anchorPoint :
特定的锚点坐标锚点。(0,0)指左下角,(1,1)指右上角,(0.5,0.5)指中心。精灵和其他“textured”节点有一个缺省值(0.5f,0.5f默认anchorPoint)

//以下例子精灵将以
anchorPoint的点的位置为中心进行转动
  @implementation Atlas5

-(id) init
{
    if( (self=[super init]) ) {

        // small capacity. Testing resizing.
        // Don't use capacity=1 in your real game. It is expensive to resize the capacity
        AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:1];
        [self addChild:mgr z:0 tag:kTagSpriteManager];       
       
        CGSize s = [[Director sharedDirector] winSize];
       
       
        id rotate = [RotateBy actionWithDuration:10 angle:360];
        id action = [RepeatForever actionWithAction:rotate];
        for(int i=0;i<3;i++) {
            AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(85*i, 121*1, 85, 121) spriteManager: mgr];
            sprite.position = ccp( 90 + i*150, s.height/2);

           
            Sprite *point = [Sprite spriteWithFile:@"r1.png"];
            point.scale = 0.25f;
            point.position = sprite.position;
            [self addChild:point z:1];

            switch(i) {
                case 0:
                    sprite.anchorPoint = CGPointZero;
                    break;
                case 1:
                    sprite.anchorPoint = ccp(0.5f, 0.5f);
                    break;
                case 2:
                    sprite.anchorPoint = ccp(1,1);
                    break;
            }

            point.position = sprite.position;
           
            id copy = [[action copy] autorelease];
            [sprite runAction:copy];
            [mgr addChild:sprite z:i];
        }       
    }   
    return self;
}

-(NSString *) title
{
    return @"AtlasSprite: anchor point";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值