{"id":623,"date":"2012-05-20T14:33:29","date_gmt":"2012-05-20T13:33:29","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=623"},"modified":"2012-07-02T21:54:48","modified_gmt":"2012-07-02T20:54:48","slug":"sparrow-framework-animationsequence-class","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/sparrow-framework-animationsequence-class\/","title":{"rendered":"Sparrow Framework, AnimationSequence class"},"content":{"rendered":"<p>Coming from AS3 &#038; <a href=\"http:\/\/gamua.com\/starling\/\" target=\"_blank\">Starling<\/a> framework, it is really easy to handle <a href=\"http:\/\/gamua.com\/sparrow\/\" target=\"_blank\">Sparrow<\/a>. I was surprised to don&#8217;t find a class to manage several animations (like the gotoAndPlay in as3) in Starling and it is the same with Sparrow. So I&#8217;ve ported the AnimationSequence class (used in the <a href=\"http:\/\/citrusengine.com\/\" target=\"_blank\">Citrus Engine<\/a> V3) from Starling to Sparrow.<\/p>\n<p>Basically, the class extends SPSprite with all SPMovieClip animations added into a Dictionary which are addChild\/removeChild (with the Juggler).<\/p>\n<p>An AnimationSequence is defined with a texture atlas, an array with animation states and a simple string with the first animation :<\/p>\n<pre lang=\"objc\">AnimationSequence *mc = [[AnimationSequence alloc] initWithTextureAtlas:[SPTextureAtlas atlasWithContentsOfFile:@\"Hero.xml\"] andAnimations:[NSArray arrayWithObjects:@\"walk\", @\"jump\", @\"idle\", nil] andFirstAnimation:@\"idle\"];<\/pre>\n<p>To change an animation :<\/p>\n<pre lang=\"objc\">[mc changeAnimation:@\"walk\" withLoop:YES];<\/pre>\n<pre lang=\"objc\" line=\"1\">#import \"SPSprite.h\"\r\n \r\n@interface AnimationSequence : SPSprite {\r\n \r\n    NSMutableDictionary *mcSequences;\r\n \r\n    SPTextureAtlas *textureAtlas;\r\n    NSArray *animations;\r\n    NSString *previousAnimation;\r\n}\r\n \r\n@property (nonatomic) NSMutableDictionary *mcSequences;\r\n@property (nonatomic) SPTextureAtlas *textureAtlas;\r\n@property (nonatomic) NSArray *animations;\r\n@property (nonatomic) NSString *previousAnimation;\r\n \r\n- (id) initWithTextureAtlas:(SPTextureAtlas *) textAtlas andAnimations:(NSArray *) multiAnimations andFirstAnimation:(NSString *) firstAnim;\r\n \r\n- (void) changeAnimation:(NSString *) animation withLoop:(BOOL)animLoop;\r\n \r\n@end<\/pre>\n<pre lang=\"objc\" line=\"1\">\/\/\r\n\/\/  AnimationSequence.m\r\n\/\/  ChipmunkWrapper\r\n\/\/\r\n\/\/  Created by Aymeric Lamboley on 06\/03\/12.\r\n\/\/\r\n\r\n#import \"AnimationSequence.h\"\r\n\r\n@implementation AnimationSequence\r\n\r\n@synthesize mcSequences, textureAtlas, animations, previousAnimation;\r\n\r\n- (id) initWithTextureAtlas:(SPTextureAtlas *)textAtlas andAnimations:(NSArray *)multiAnimations andFirstAnimation:(NSString *)firstAnim {\r\n    \r\n    if (self = [super init]) {\r\n        \r\n        textureAtlas = textAtlas;\r\n        animations = multiAnimations;\r\n        mcSequences = [[NSMutableDictionary alloc] init];\r\n        \r\n        for (NSString *animation in multiAnimations) {\r\n            \r\n            if ([textureAtlas texturesStartingWith:animation].count == 0) {\r\n                NSLog(@\"One object doesn't have the %@ animation in its TextureAtlas\", animation);\r\n            }\r\n            \r\n            [mcSequences setObject:[[SPMovieClip alloc] initWithFrames:[textureAtlas texturesStartingWith:animation] fps:25] forKey:animation];\r\n        }\r\n        \r\n        [self addChild:[mcSequences objectForKey:firstAnim]];\r\n        [[SPStage mainStage].juggler addObject:[mcSequences objectForKey:firstAnim]];\r\n        \r\n        previousAnimation = firstAnim;\r\n    }\r\n    \r\n    return self;\r\n}\r\n\r\n- (void) changeAnimation:(NSString *)animation withLoop:(BOOL)animLoop {\r\n    \r\n    if (!([mcSequences objectForKey:animation])) {\r\n\r\n        NSLog(@\"One object doesn't have the %@ animation set up in its initial array?\", animation);\r\n    }\r\n    \r\n    [self removeChild:[mcSequences objectForKey:previousAnimation]];\r\n    [self.stage.juggler removeObject:[mcSequences objectForKey:previousAnimation]];\r\n    \r\n    [self addChild:[mcSequences objectForKey:animation]];\r\n    [self.stage.juggler addObject:[mcSequences objectForKey:animation]];\r\n    ((SPMovieClip *)[mcSequences objectForKey:animation]).loop = animLoop;\r\n    ((SPMovieClip *)[mcSequences objectForKey:animation]).currentFrame = 0;\r\n    \r\n    previousAnimation = animation;\r\n}\r\n\r\n- (void) dealloc {\r\n    \r\n    [self removeChild:[mcSequences objectForKey:previousAnimation]];\r\n    [self.stage.juggler removeObject:[mcSequences objectForKey:previousAnimation]];\r\n}\r\n\r\n@end<\/pre>\n<p>Be careful, this version uses the ARC mode!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coming from AS3 &#038; Starling framework, it is really easy to handle Sparrow. I was surprised to don&#8217;t find a class to manage several animations (like the gotoAndPlay in as3) in Starling and it is the same with Sparrow. So I&#8217;ve ported the AnimationSequence class (used in the Citrus Engine V3) from Starling to Sparrow. &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/sparrow-framework-animationsequence-class\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Sparrow Framework, AnimationSequence class<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[98,97,6,129],"tags":[131,99,132,100,130,133],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/623"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=623"}],"version-history":[{"count":5,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/623\/revisions"}],"predecessor-version":[{"id":659,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/623\/revisions\/659"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}