2012-07-19 2 views
1

J'anime mon personnage comme ça:Cocos2d Deux animations pour un sprite

-(void) createHero 
{ 


    _batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Snipe1.png"]; 
    [self addChild:_batchNode]; 
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Snipe1.plist"]; 

    //gather list of frames 
    NSMutableArray *runAnimFrames = [NSMutableArray array]; 
    for(int i = 1; i <= 7; ++i) 
    { 
     [runAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
     [NSString stringWithFormat:@"run000%d.png", i]]]; 
    } 

    //create sprite and run the hero 
    self.hero = [CCSprite spriteWithSpriteFrameName:@"run0001.png"]; 
    _hero.anchorPoint = CGPointZero; 
    _hero.position = self.heroRunningPosition; 

    //create the animation object 
    CCAnimation *runAnim = [CCAnimation animationWithFrames:runAnimFrames delay:1/30.0f]; 
    self.runAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:runAnim restoreOriginalFrame:YES]]; 

    [_hero runAction:_runAction]; 
    [_batchNode addChild:_hero z:0]; 
} 

Cela fonctionne bien un mon personnage est en cours d'exécution, mais maintenant je veux une deuxième animation quand il saute. En ce moment je fais comme ça:

-(void)changeHeroImageDuringJump 
{ 
    [_hero setTextureRect:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"run0007.png"].rect]; 
} 

Mais maintenant, je veux une deuxième plist avec un deuxième .png, donc je reçois une nouvelle animation tout lorsque le personnage saute. Comment puis-je mettre en œuvre cela?

Répondre

3

Dans mon cas, j'ai implémenté une classe AnimatedSprite qui va gérer cela pour moi. De cette façon, ajouter des fichiers comme ceci:

NSDictionary* anims = [NSDictionary dictionaryWithObjectsAndKeys: 
     @"Animations/Character/idle_anim.plist", @"Idle", 
     @"Animations/Character/walk_anim.plist", @"Walk", 
     @"Animations/Character/run_anim.plist", @"Run", nil]; 

CCNode* myNode = [[AnimatedSprite alloc] initWithDictionary:anims 
         spriteFrameName: @"character_idle_01.png" 
         startingIndex:@"Idle"]; 

Modification de l'animation est aussi simple que:

[myNode setAnimation: @"Run"]; 

Heres ma mise en œuvre Ceci est le .h

@interface AnimatedSprite : CCSprite 
{ 
    NSMutableDictionary* _spriteAnimations; 
    CCAction* _currentAnimation; 
    NSString* _currentAnimationName; 
    bool _initialized; 
} 
- (id) initWithTextureName:(NSString*) textureName; 
- (id) initWithArray: (NSArray*) animList spriteFrameName: (NSString*) startingSprite startingIndex: (int)startingIndex; 
- (id) initWithDictionary:(NSDictionary *)anims spriteFrameName:(NSString *)startingSprite startingIndex:(NSString *)startingAnim; 

- (void) addAnimation: (NSString*) animationName andFilename: (NSString*) plistAnim; 

- (void) setAnimationIndex: (int) index; 
- (void) setAnimation: (NSString*) animationName; 
@end 

Et c'est la .m

#import "AKHelpers.h" 

@implementation AnimatedSprite 

NSMutableDictionary* _spriteAnimations; 

- (id) initWithTextureName:(NSString*) textureName 
{ 
    CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:textureName]; 
    CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect: CGRectMake(0, 0, 1, 1)]; 

    if ((self=[super initWithSpriteFrame:frame])) 
    { 
     _currentAnimationName = nil; 
     _currentAnimation = nil; 
     _spriteAnimations = [[NSMutableDictionary alloc] init ]; 
     _initialized = true; 

    } 
    return self; 
} 
- (id) initWithArray: (NSArray*) animList spriteFrameName: (NSString*) startingSprite startingIndex: (int)startingIndex 
{ 
    _initialized = false; 
    _spriteAnimations = [[NSMutableDictionary alloc] init]; 

    // Add animations as numbers from 0 to animList.count 
    int i = 0; 
    for (NSString* anim in animList) 
    { 
     [self addAnimation: [NSString stringWithFormat:@"%d", i] andFilename:anim]; 
     i++; 
    } 

    if ((self = [super initWithSpriteFrameName:startingSprite])) 
    { 
     _currentAnimationName = nil; 
     _currentAnimation = nil; 
     [self setAnimationIndex: startingIndex]; 
     _initialized = true; 
    } 

    return self; 
} 

- (id) initWithDictionary:(NSDictionary *)anims spriteFrameName:(NSString *)startingSprite startingIndex:(NSString *)startingAnim 
{ 
    _initialized = false; 
    _spriteAnimations = [[NSMutableDictionary alloc] init];//[[NSMutableArray alloc] init]; 

    // Add animations 
    for (NSString* key in anims) 
    { 
     [self addAnimation: key andFilename: [anims objectForKey: key] ]; 
    } 

    if ((self = [super initWithSpriteFrameName:startingSprite])) 
    { 
     _currentAnimationName = nil; 
     _currentAnimation = nil; 
     [self setAnimation: startingAnim]; 
     _initialized = true; 

    } 
    return self; 
} 
- (void) dealloc 
{ 
    [_currentAnimationName release]; 
    [_spriteAnimations release]; 
    [super dealloc]; 
} 

- (void) setAnimation: (NSString*) animationName 
{ 
    if (![_currentAnimationName isEqualToString:animationName]) 
    { 
     [_currentAnimationName release]; 
     _currentAnimationName = [animationName copy]; 

     // Stop current animation 
     if (_currentAnimation != nil) 
      [self stopAction:_currentAnimation]; 
     //[self stopAllActions]; 

     // Apply animation 
     NSDictionary* clip = [_spriteAnimations objectForKey: animationName]; 
     if (clip) 
     { 
      _currentAnimation = [AKHelpers actionForAnimationClip:clip]; 
      if (_currentAnimation) 
       [self runAction:_currentAnimation]; 
     } 
     else 
     { 
      _currentAnimation = nil; 
     } 
    } 
} 

- (void) setAnimationIndex: (int) index 
{ 
    [self setAnimation: [NSString stringWithFormat:@"%d", index]]; 
} 

- (void) addAnimation: (NSString*) animationName andFilename: (NSString*) plistAnim 
{ 
    NSDictionary *clip = [AKHelpers animationClipFromPlist:plistAnim]; 

    if (clip) 
    { 
     [_spriteAnimations setObject:clip forKey:animationName];   

     if (_initialized && [_spriteAnimations count] == 1) 
     { 
      [self setAnimation:animationName]; 
     } 
    } 
} 

@end 
+0

J'ai ajouté vos deux classes dans mon projet mais j'ai ne comprends pas comment vous les implémentez j'ai Snipe1.plist Snipe2.plist et Snipe1.png et Snipe2.png. Dans le plist, il y a run0001-run0007.png et dans Snipe2, jump0001-jump0007.png. Comment puis-je ajouter ces deux animations maintenant? Merci pour votre réponse ! – dehlen

+0

Oh et qu'est ce que AKHelpers? Où puis-je obtenir ceci? – dehlen

+0

Vous pouvez trouver AKHelpers ici, http://www.cocos2d-iphone.org/forum/topic/13851, il a son propre format de plist pour les animations. Ce que vous faites est fondamentalement une plist qui référencera à la fois la texture et les coordonnées, puis définira les cadres et la durée. Vous pouvez faire votre plist semblable à l'échantillon trouvé ici: https://github.com/anton-nikan/iOS-Animation-Kit/blob/master/Samples/cocos2d/frog-menu-anim.plist –

1

Créez deux actions d'animation différentes pour courir et sauter. Exécutez ces actions en fonction des besoins.