0

On button "press" I execute an animation as follows:

//create sprite
animatedSprite = [[CCSprite alloc] initWithFile:@"transparentPixel.png" rect:CGRectMake(0, 0, 218, 218)];
SET_POS(animatedSprite, 167, 51);
[self addChild:animatedSprite z:5000];
//animate sprite            
[animatedSprite runAction:[CCAnimate actionWithAnimation:[[CCAnimationCache sharedAnimationCache] animationByName:@"skill1use"]]];
//run cleanup fnktion
[animatedSprite performSelector:@selector(removeFromParentAndCleanup:) withObject:[NSNumber numberWithBool:YES] afterDelay:3];            
[animatedSprite release];

previously I loaded the frames with:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[CCDirector sharedDirector].contentScaleFactor>1?@"skill_01_ani@2x.plist":@"skill_01_ani.plist"];
[[CCAnimationCache sharedAnimationCache] addAnimation:[CCAnimation animationWithSpriteSequence:[CCDirector sharedDirector].contentScaleFactor>1?@"skill_01_ani%04d@2x.png":@"skill_01_ani%04d.png" numFrames:34 delay:1/24.0] name:@"skill1use"];

However, the animation runs much smoother after the first time, the first time it takes a while until it starts. Am I preloading the animation wrong? Is there a way to have the animation run smoothly the first time too?

UPDATE

If I set following in the preview it runs fast the first time:

CCSprite *animatedSprite = [[CCSprite alloc] initWithFile:@"transparentPixel.png" rect:CGRectMake(0, 0, 218, 218)];
[self addChild:animatedSprite z:5000];
[animatedSprite runAction:[CCAnimate actionWithAnimation:[[CCAnimationCache sharedAnimationCache] animationByName:@"skill1use"]]];
[animatedSprite performSelector:@selector(removeFromParentAndCleanup:) withObject:[NSNumber numberWithBool:YES] afterDelay:3];
[animatedSprite release];

Since it is the same as running the animation. However, this only works if I actually show the animation (with addChild and everything)

Daniel
  • 20,420
  • 10
  • 92
  • 149
  • 1
    I cringe at the use of contentScaleFactor determining the @2x file load. Why don't you just use cocos2d's native -hd file extension? – CodeSmile Oct 10 '12 at 22:03
  • It is easier than renaming all the files I have ;) – Daniel Oct 11 '12 at 10:06
  • don't think that it is really easier. pack all of your files with zwoptex or texturepacker and you will have only few atlases with all your art – Morion Oct 11 '12 at 10:52

1 Answers1

1

As far as I know, at the first time this action creates CCAnimation object and stores it in CCAnimationCache. Try to precache your animation or initialize it in your init method, then just store it. And on your button click just recreate action, not animation.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • If I preload `[CCAnimate actionWithSpriteSequence:[CCDirector sharedDirector].contentScaleFactor>1?@"skill_01_ani%04d@2x.png":@"skill_01_ani%04d.png" numFrames:34 delay:1/24.0 restoreOriginalFrame:NO]` I still have the same issue. – Daniel Oct 10 '12 at 14:08
  • try to create CCAnimation object and add it to CCAnimationCache in your init method. then create CCAnimate with actionWithAnimation:restoreOriginalFrame constructor – Morion Oct 10 '12 at 14:17
  • I have updated the code (see question).. still the same result :( – Daniel Oct 11 '12 at 10:09
  • where do you make animation preloading? – Morion Oct 11 '12 at 10:53