Fork me on GitHub

cocos2d精灵动画简单实例

参考http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d

用到zwoptex工具。

plist输出的时候,选择cocos2d格式。

 

流程简介:

先用plist创建一个spriteFrameCache

然后创建一个spriteBatchNode

然后创建一个队列存放Cache的图片

然后创建一个CCAnimation对象播放这个队列

然后创建一个action来播放动画。

over

if( (self=[super init])) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"bear.plist"];
CCSpriteBatchNode* spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:@"bear.png"];
[self addChild:spriteSheet];

NSMutableArray* walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 8;i++){
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"bear%d.png",i]]];
}
CCAnimation* walkAnim = [CCAnimation
animationWithFrames:walkAnimFrames delay:0.1f];
//just display it;
CGSize winSize = [CCDirector sharedDirector].winSize;
self.bear = [CCSprite spriteWithSpriteFrameName:@"bear1.png"];
_bear.position = ccp(winSize.width/2,winSize.height/2);
self.walkAction = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[_bear runAction:_walkAction];
[spriteSheet addChild:_bear];
}
return self;

 

 

转载地址:http://blog.csdn.net/ssihc0/article/details/6784819

CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [cache addSpriteFramesWithFile:@"t1.plist" textureFile:@"t1.png"];
    
    CCSprite *mySprite = [CCSprite spriteWithSpriteFrameName:@"a1.png"];
    mySprite.position = ccp(240,160);
    [self addChild:mySprite];
    

    NSMutableArray *climbFrames = [NSMutableArray array];
    for(int i = 1; i < 7; i++) {
        NSString *fn = [NSString stringWithFormat:@"a%d.png",i];
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fn];
        [climbFrames addObject:frame];
    }
    
    CCAnimation *animateClimb = [CCAnimation animationWithFrames:climbFrames delay:0.2f]; 
    id a1 = [CCAnimate actionWithAnimation:animateClimb restoreOriginalFrame:NO];
    [mySprite runAction:a1];

posted on 2012-04-06 19:27  pengyingh  阅读(562)  评论(0)    收藏  举报

导航