这是创建单个精灵的代码,然后为其设置动画:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"crowfl.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"crowfl.png"];
[self addChild:spriteSheet];
NSMutableArray *crowAnimFrames = [NSMutableArray array];
for (int i=1; i<=8; i++) {
[crowAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"crow%d.png", i]]];
}
CCAnimation *crowAnim = [CCAnimation animationWithSpriteFrames:crowAnimFrames delay:0.1f];
_crow = [CCSprite spriteWithSpriteFrameName:@"crow1.png"];
_crow.position = ccp(windowSize.width + _crow.contentSize.width/2, _crowFlightHeight);
id crowAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:crowAnim]];
[_crow runAction:crowAction];
[spriteSheet addChild:_crow];
...
id crowMoveAction = [CCSequence actions:
[CCMoveTo actionWithDuration:5.0 position:ccp(_flipCrow ? (windowSize.width + _crow.contentSize.width/2) : (-_crow.contentSize.width/2), _crowFlightHeight)],
crowMoveComplete,
nil];
[_crow runAction:crowMoveAction];
我需要的是创建多个精灵(_crow)并异步动画它们。我需要知道代码的哪些部分可以在多个精灵之间重复使用/共享,以及每个动画精灵的哪个部分应该是唯一的。
答案 0 :(得分:2)
在 CCAnimationCache 中缓存动画,这样可以减少一次又一次的加载延迟:
CCAnimation* crowAnim = nil;
crowAnim = [[CCAnimationCache sharedAnimationCache] animationByName:@"crowAnim"];
if(!crowAnim)
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"crowfl.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"crowfl.png"];
[self addChild:spriteSheet];
NSMutableArray *crowAnimFrames = [NSMutableArray array];
for (int i=1; i<=8; i++) {
[crowAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"crow%d.png", i]]];
}
crowAnim = [CCAnimation animationWithSpriteFrames:crowAnimFrames delay:0.1f];
[[CCAnimationCache sharedAnimationCache] addAnimation:crowAnim name:@"crowAnim"];
}
_crow = [CCSprite spriteWithSpriteFrameName:@"crow1.png"];
_crow.position = ccp(windowSize.width + _crow.contentSize.width/2, _crowFlightHeight);
id crowAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:crowAnim]];
[_crow runAction:crowAction];
[spriteSheet addChild:_crow];
...
id crowMoveAction = [CCSequence actions:
[CCMoveTo actionWithDuration:5.0 position:ccp(_flipCrow ? (windowSize.width + _crow.contentSize.width/2) : (-_crow.contentSize.width/2), _crowFlightHeight)],
crowMoveComplete,
nil];
[_crow runAction:crowMoveAction];