根据本教程, 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];