Ios 设置多个精灵的动画
这是创建单个精灵然后设置其动画的代码:Ios 设置多个精灵的动画,ios,cocos2d-iphone,Ios,Cocos2d Iphone,这是创建单个精灵然后设置其动画的代码: [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"crowfl.plist"]; CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"crowfl.png"]; [self addChild:spriteSheet]; NSMutableArray *crowAnimFram
[[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];
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@“crowfl.plist”];
CCSpriteBatchNode*spriteSheet=[CCSpriteBatchNode batchNodeWithFile:@“crowfl.png”];
[自添加子项:精灵表];
NSMutableArray*crowAnimFrames=[NSMutableArray];
对于(int i=1;i在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];
CCAnimation*crowAnim=nil;
crowAnim=[[CCAnimationCache sharedAnimationCache]animationByName:@“crowAnim”];
如果(!crowAnim)
{
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@“crowfl.plist”];
CCSpriteBatchNode*spriteSheet=[CCSpriteBatchNode batchNodeWithFile:@“crowfl.png”];
[自添加子项:精灵表];
NSMutableArray*crowAnimFrames=[NSMutableArray];
对于(int i=1;i实际上我可以使用crowAnim
作为类成员并创建一次,但是这个示例帮助我确定创建一次需要动画。thx