2012-07-19 209 views
1

的Cocos2D兩個動畫動畫我我的性格像:一個精靈

-(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]; 
} 

這工作得很好了我的性格正在運行,但現在我想第二個動畫,當他跳躍。目前,我讓這樣的:

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

但現在我想用第二PNG第二的plist,所以我得到了一個全新的動畫當角色跳躍。我怎樣才能實現呢?

回答

3

在我的情況下,我實現了一個AnimatedSprite類來處理這個問題。這樣一來,我想補充文件,像這樣:

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"]; 

更改動畫很簡單,只要:

[myNode setAnimation: @"Run"]; 

我的繼承人履行這是.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 

這是.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

我在你的項目中添加了你的兩個類,但是我不知道你如何實現他們我有Snipe1.plist Snipe2.plist和Snipe1.png和Snipe2.png。在plist中有run0001-run0007.png,在Snipe2中是jump0001-jump0007.png。我現在如何添加這兩個動畫?感謝您的回答 ! – dehlen 2012-07-19 16:29:41

+0

哦,什麼是AKHelpers?我在哪裏可以得到這個? – dehlen 2012-07-19 16:40:17

+0

你可以在這裏找到AKHelpers,http://www.cocos2d-iphone.org/forum/topic/13851,它有它自己的動畫plist格式。你所做的基本上是一個將引用紋理和座標的plist,然後定義幀和持續時間。你可以讓你的plist類似於這裏找到的示例:https://github.com/anton-nikan/iOS-Animation-Kit/blob/master/Samples/cocos2d/frog-menu-anim.plist – 2012-07-19 16:59:25

1

爲跑步和跳躍創建兩個不同的動畫動作。根據需要運行這些操作。

相關問題