2010年11月29日 星期一

Cocos2d - CCSprite, CCSpriteSheet and CCAnimation

//Adding an Animated Sprite to Scene


















animationSheet = [CCSpriteSheet spriteSheetWithFile:@"animation_sheet.png"];
sprite = [CCSprite spriteWithTexture:animationSheet.texture rect:CGRectMake(0, 0, 60, 90)];
[animationSheet addChild:sprite];
CCAnimation *animation = [CCAnimation animationWithName:@"nameOfYrAnimation" delay:0.1f];
int frameCount = 0;
for (int y = 0; y < 3; y++) {
    for (int x = 0; x < 5; x++) {
        CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:animationSheet.texture rect:CGRectMake(x*60,y*90,60,90)];
        [animation addFrame:frame];
frameCount++;
if (frameCount == 14)
    break;
}
}
CCAnimate *action = [CCAnimate actionWithAnimation:animation];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:action];
[sprite runAction:repeat];

沒有留言:

張貼留言