Sparrow Framework, AnimationSequence class

Coming from AS3 & Starling framework, it is really easy to handle Sparrow. I was surprised to don’t find a class to manage several animations (like the gotoAndPlay in as3) in Starling and it is the same with Sparrow. So I’ve ported the AnimationSequence class (used in the Citrus Engine V3) from Starling to Sparrow.

Basically, the class extends SPSprite with all SPMovieClip animations added into a Dictionary which are addChild/removeChild (with the Juggler).

An AnimationSequence is defined with a texture atlas, an array with animation states and a simple string with the first animation :

AnimationSequence *mc = [[AnimationSequence alloc] initWithTextureAtlas:[SPTextureAtlas atlasWithContentsOfFile:@"Hero.xml"] andAnimations:[NSArray arrayWithObjects:@"walk", @"jump", @"idle", nil] andFirstAnimation:@"idle"];

To change an animation :

[mc changeAnimation:@"walk" withLoop:YES];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#import "SPSprite.h"
 
@interface AnimationSequence : SPSprite {
 
    NSMutableDictionary *mcSequences;
 
    SPTextureAtlas *textureAtlas;
    NSArray *animations;
    NSString *previousAnimation;
}
 
@property (nonatomic) NSMutableDictionary *mcSequences;
@property (nonatomic) SPTextureAtlas *textureAtlas;
@property (nonatomic) NSArray *animations;
@property (nonatomic) NSString *previousAnimation;
 
- (id) initWithTextureAtlas:(SPTextureAtlas *) textAtlas andAnimations:(NSArray *) multiAnimations andFirstAnimation:(NSString *) firstAnim;
 
- (void) changeAnimation:(NSString *) animation withLoop:(BOOL)animLoop;
 
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
//  AnimationSequence.m
//  ChipmunkWrapper
//
//  Created by Aymeric Lamboley on 06/03/12.
//
 
#import "AnimationSequence.h"
 
@implementation AnimationSequence
 
@synthesize mcSequences, textureAtlas, animations, previousAnimation;
 
- (id) initWithTextureAtlas:(SPTextureAtlas *)textAtlas andAnimations:(NSArray *)multiAnimations andFirstAnimation:(NSString *)firstAnim {
 
    if (self = [super init]) {
 
        textureAtlas = textAtlas;
        animations = multiAnimations;
        mcSequences = [[NSMutableDictionary alloc] init];
 
        for (NSString *animation in multiAnimations) {
 
            if ([textureAtlas texturesStartingWith:animation].count == 0) {
                NSLog(@"One object doesn't have the %@ animation in its TextureAtlas", animation);
            }
 
            [mcSequences setObject:[[SPMovieClip alloc] initWithFrames:[textureAtlas texturesStartingWith:animation] fps:25] forKey:animation];
        }
 
        [self addChild:[mcSequences objectForKey:firstAnim]];
        [[SPStage mainStage].juggler addObject:[mcSequences objectForKey:firstAnim]];
 
        previousAnimation = firstAnim;
    }
 
    return self;
}
 
- (void) changeAnimation:(NSString *)animation withLoop:(BOOL)animLoop {
 
    if (!([mcSequences objectForKey:animation])) {
 
        NSLog(@"One object doesn't have the %@ animation set up in its initial array?", animation);
    }
 
    [self removeChild:[mcSequences objectForKey:previousAnimation]];
    [self.stage.juggler removeObject:[mcSequences objectForKey:previousAnimation]];
 
    [self addChild:[mcSequences objectForKey:animation]];
    [self.stage.juggler addObject:[mcSequences objectForKey:animation]];
    ((SPMovieClip *)[mcSequences objectForKey:animation]).loop = animLoop;
    ((SPMovieClip *)[mcSequences objectForKey:animation]).currentFrame = 0;
 
    previousAnimation = animation;
}
 
- (void) dealloc {
 
    [self removeChild:[mcSequences objectForKey:previousAnimation]];
    [self.stage.juggler removeObject:[mcSequences objectForKey:previousAnimation]];
}
 
@end

Be careful, this version uses the ARC mode!

Leave a Reply

Your email address will not be published. Required fields are marked *