0

根据本教程, PVR 图像似乎是 iOS 精灵的最佳格式。然而,在使用 Texturepacker 创建一个精灵表并导出为这种格式后,我无法让动画在 cocos2d 中工作。根据此处的文档,我应该使用

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"LuckyCompiled.plist"];

但是教程和文档都没有解释如何做动画,除了这个。这是下面的代码所基于的。但这只会将基础图像放置到图层上,并且不会设置动画。

CCSprite *sprite = [[CCSprite alloc]init];
    sprite.position = ccp(player.contentSize.width/2+40, winSize.height/2+40);
    // Obtain the shared instance of the cache
    CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];

   // load the frames
   [cache addSpriteFramesWithFile:@"LuckyCompiled.plist"];

   // It loads the frame named "frame1.png".
    // IMPORTANT: It doesn't load the image "frame1.png". "frama1.png" is a just the name of the frame
    CCSpriteFrame *frame = [cache spriteFrameByName:@"lucky1.png"];
                                 [sprite setDisplayFrame:frame];
    [self addChild:sprite];

    NSMutableArray *animFrames = [NSMutableArray array];
        for(int i = 1; i < 10; i++) {

    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"lucky%d.png",i]];
            [animFrames addObject:frame];
        }
    NSLog(@"animaframes %@",animFrames);
        CCAnimation *animation = [CCAnimation animationWithSpriteFrames:[NSArray arrayWithArray:animFrames]];

    [sprite runAction:[CCAnimate actionWithAnimation:animation]];

回答:

需要延迟,否则动画不明显

[CCAnimation animationWithSpriteFrames:[NSArray arrayWithArray:animFrames]];

应该是(也不需要做nsarray,可变的很好)

[CCAnimation animationWithSpriteFrames:frames delay:0.1f];
4

1 回答 1

3

这是我尝试过的代码,它工作正常。

CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
        [frameCache addSpriteFramesWithFile:@"walkFrames.plist"];

        player = [CCSprite spriteWithSpriteFrameName:@"f1.png"];
        NSMutableArray *frames = [NSMutableArray arrayWithCapacity:8];
        for (int i = 1; i < 9; i++) {
            NSString *file = [NSString stringWithFormat:@"f%d.png", i];
            CCSpriteFrame *frame = [frameCache spriteFrameByName:file];
            [frames addObject:frame];
        }
        CCAnimation *walkAnim =[CCAnimation animationWithSpriteFrames:frames delay:0.1f];
        CCAnimate *animate = [CCAnimate actionWithAnimation:walkAnim];
        CCRepeatForever *rep = [CCRepeatForever actionWithAction:animate];
        player.position = ccp(23, 285);
        [player runAction:rep];

        [self addChild:player];
于 2012-07-20T18:30:20.540 回答