2010-06-06 7 views
2

J'essaie de faire un sprite animate en cocos2D. Je crois que j'ai l'animation mis en place, mais comment puis-je dessiner le animating sprite sur l'écran? Voici ce que j'ai:Animation dans Cocos2d.

id anim = [[[CCAnimation alloc] initWithName:@"char_walking" delay:1/12.0] autorelease]; 

[anim addFrame:@"run2.png"]; 
[anim addFrame:@"run1.png"]; 
[anim addFrame:@"run3.png"]; 
[anim addFrame:@"run4.png"]; 
[anim addFrame:@"run3.png"]; 
[anim addFrame:@"run1.png"]; 

id myAction = [CCAnimate actionWithAnimation:anim]; 
id repeating = [CCRepeatForever actionWithAction:myAction]; 

[character do:repeating]; 

character = [CCSprite spriteWithSpriteFrame:anim]; 
character.position = ccp(160, 240); 
[self addChild:character]; 

Merci à l'avance, John

Répondre

1

Peut-être qu'il était juste une erreur coupe-coller, mais il semble que vous racontez l'image-objet de répéter l'animation AVANT vous le créez, ainsi le sprite de caractère que vous ajoutez au nœud ne reçoit jamais l'action CCAnimate qui lui est envoyée.

+0

Ouais c'est ce que j'ai fait, je me sens stupide. – tallen11

0

Vous n'ajoutez pas de spriteFrames comme l'exige la méthode addFrame.

avec cette ligne:

[character faire: répéter];

vous êtes peut-être à la recherche de [character runAction:repeating];

caractère = [CCSprite spriteWithSpriteFrame: anim];

Ici, anim n'est pas un spriteFrame, c'est un CCanimation.

En gros, vous avez quelques problèmes.

vous pouvez essayer quelque chose comme ceci en utilisant zwoptex pour créer votre fichier .plist:

CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache]; 

[cache addSpriteFramesWithFile:@"runImages.plist"]; 

CCSprite *startingImage = [CCSprite spriteWithSpriteFrameName:@"run1.png"]; 

[self addChild:startingImage]; 

// créer vos cadres de sprite

NSArray *animFrames = [[NSArray alloc] initWithCapacity:6]; 

[animFrames addFrame:[cache spriteFrameByName:@"run2.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run1.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run3.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run4.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run3.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run1.png"]]; 

// lancer l'animation

CCAnimation *animation = [CCAnimation animationWithName:@"char_walking" delay:1/12.0 frames:animFrames]; 
id anim = [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];  
[startingImage runAction:anim];