根据this tutorial PVR图像似乎是iOS精灵的最佳格式。然而,在使用Texturepacker创建精灵表并导出为此格式后,我无法使动画在cocos2d中工作。 根据文档here我应该使用
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"LuckyCompiled.plist"];
但是除了this之外,教程和文档都没有解释如何做动画。这是以下代码的基础。 但这只会将基本图像放在图层上,而不会制作动画。
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];
答案 0 :(得分: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];