iOS-Cocos2d游戏开发】使用plist文件制作简单精灵


分类: 【IOS-cocos2d-iphone】 34人阅读 评论(0) 收藏 举报

最近学cocos2d,看一般都用Zwoptex,但是这个软件要钱,所以用了一个在线版本的http://zwoptexapp.com/flashversion/基本功能也很好,需要注意的是,在最后导出png图片和plist文件时,一定要取相同的名字(切记啊切记),下面是素材



//=================================================================================


上代码:
@interface HelloWorldLayer :CCLayer

{

    CCSprite *_sprite;

    CCAction *_walkAction;

    CCAction *_moveAction;

 

}



@property(nonatomic,retain)CCSprite*sprite;

@property(nonatomic,retain)CCAction*walkAction;

@property(nonatomic,retain)CCAction*moveAction;


// returns a CCScene that contains the HelloWorldLayer as the onlychild

+(CCScene *) scene;


@end


#import "HelloWorldLayer.h"


// HelloWorldLayer implementation

@implementationHelloWorldLayer

@synthesizesprite=_sprite;

@synthesize moveAction =_moveAction;

@synthesize walkAction =_walkAction;


+(CCScene *) scene

{

// 'scene' is an autorelease object.

CCScene *scene = [CCScenenode];

 

// 'layer' is an autorelease object.

HelloWorldLayer*layer =[HelloWorldLayernode];

 

// add layer as a child to scene

[scene addChild: layer];

 

// return the scene

return scene;

}

-(id)init{

     

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


        [[CCSpriteFrameCachesharedSpriteFrameCache]addSpriteFramesWithFile:

       @"AnimHero.plist"];//第1步

       CCSpriteBatchNode*spriteSheet =[CCSpriteBatchNode 

                                   batchNodeWithFile:@"AnimHero.png"];

       [selfaddChild:spriteSheet];//第2步

      NSMutableArray *walkAnimFrames =[NSMutableArrayarray];

      for(inti=1; i<=3; i++)

       {

          [walkAnimFramesaddObject:

           [[CCSpriteFrameCacheshared SpriteFrameCache]spriteFrameByName:

            [NSStringstringWithFormat:@"hero%i.png",i]]];

       } //第3步

                  

      CCAnimation*walkAnim = [CCAnimation 

                           animationWithFrames:walkAnimFramesdelay:0.1f];//第4步

       

      _sprite=[CCSprite spriteWithSpriteFrameName:@"hero1.png"];

    _sprite.position=ccp(240,160);

       self.walkAction=[CCRepeatForever actionWithAction:

                  [CCAnimateactionWithAnimation:walkAni restoreOriginalFrame:NO]];

       [_spriterunAction:_walkAction];        

    [spriteSheet addChild:_sprite]; //第5步 


     }

 

return self;

}

 

-(void) dealloc

{

   self.sprite=nil;

  self.moveAction=nil;

  self.walkAction=nil;

   [superdealloc];

}

@end


逐步解析:
第1步: 缓冲sprite帧和纹理。    [[CCSpriteFrameCache sharedSpriteFrameCacheaddSpriteFramesWithFile:

       @"AnimHero.plist"];


 

     首先,调用CCSpriteFrameCache的addSpriteFramesWithFile方法,然后把Zwoptex生成的plist文件当作参数传进去。这个方法做了以下几件事:1:寻找resource下和输入的参数名字一样但是后缀是.png的图片文件,然后把这个文件加入到共享的CCTextureCache中。就是AnimHero.png

   2:解析plist文件,追踪所有的sprite在spritesheet中的位置,内部使用CCSpriteFrame对象追踪这些信息。



第2步:创建一个精灵处理批节点。


CCSpriteBatchNode *spriteSheet= [CCSpriteBatchNode 

                                   batchNodeWithFile:@"AnimHero.png"];

       [self addChild:spriteSheet];



接下来,创建CCSpriteBatchNode对象,把spritesheet当作参数传进去,spritesheet工作原理:1:创建一个CCSpriteBatchNode对象,通过传递一个包含所有sprite的spritesheet的名字作为参数,并加入到当前场景中。2:在spritesheet中创建的任何一个sprite,都应该当作CCSpriteBatchNode的一个孩子加进去,只要sprite包含在sprite中,就ok了。3:CCSpriteBatchNode可以智能遍历所有的孩子节点。



第3步:收集帧列表。


NSMutableArray *walkAnimFrames =[NSMutableArray array];

       for(int i=1;i <=3;i++)

       {

          [walkAnimFrames addObject:

           [[CCSpriteFrameCache sharedSpriteFrameCachespriteFrameByName:

            [NSString stringWithFormat:@"hero%i.png",i]]];

       


  为了创建一系列的动画帧,我们简单地遍历我们的图片名字(它们是按照hero1.png-->hero3.png的方式命名的),然后使用共享的CCSpriteFrameCache来获得每一个动画帧。记住,它们已经在缓存里了,因为我们前面调用了addSpriteFramesWithFile方法。


第4步:创建对象动画

CCAnimation *walkAnim = [CCAnimation 

                            animationWithFrames:walkAnimFrames delay:0.1f]


接下来,我们通过传入sprite帧列表来创建一个CCAnimation对象,并且指定动画播放的速度。我们使用0.1来指定每个动画帧之间的时间间隔。

第5步:创建sprite并且让他run动画action


_sprite=[CCSprite spriteWithSpriteFrameName:@"hero1.png"];

    _sprite.position=ccp(240,160);

      self.walkAction =[CCRepeatForever actionWithAction:

                  [CCAnimate actionWithAnimation:walkAni restoreOriginalFrame:NO]];

       [_sprite runAction:_walkAction];        

    [spriteSheet addChild:_sprite];


我们首先通过spriteframe来创建一个sprite,并把它放在屏幕中间。然后,生成CCAnimationAction,并赋值给场景的walkAction属性,最后让熊来运行这个action。

  最后,我们把hero加个场景中--把它当作spritesheet的孩子加到spritesheet中去。注意,如果在这里我们没有把它加到spritsheet中,而是加到当前层里面的话。那么我们将得不到spritesheet为我们带来的性能提升!!




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值