Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Created October 23, 2012 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rutcreate/3938923 to your computer and use it in GitHub Desktop.
Save rutcreate/3938923 to your computer and use it in GitHub Desktop.
Sparrow: Animated Sprite with MovieClip
#import <Foundation/Foundation.h>
@class SPSprite;
@class SPMovieClip;
@interface RCAnimatedSprite : SPSprite <SPAnimatable> {
NSMutableDictionary *mAnimations;
SPMovieClip *mCurrentMovieClip;
NSString *mCurrentAnimation;
SPJuggler *mJuggler;
}
- (id)initWithMovieClip:(SPMovieClip *)movieClip animation:(NSString *)animation;
- (void)addMovieClip:(SPMovieClip *)movieClip animation:(NSString *)animation;
- (void)prepare:(NSString *)animation;
- (void)play:(NSString *)animation;
- (void)pause;
- (void)stop;
@property (nonatomic, retain) SPMovieClip *currentMovieClip;
@property (nonatomic, copy) NSString *currentAnimation;
@end
#import "RCAnimatedSprite.h"
@implementation RCAnimatedSprite
@synthesize currentMovieClip = mCurrentMovieClip;
@synthesize currentAnimation = mCurrentAnimation;
- (id)initWithMovieClip:(SPMovieClip *)movieClip animation:(NSString *)animation
{
if ((self = [super init])) {
self.currentMovieClip = movieClip;
self.currentAnimation = animation;
mAnimations = [[NSMutableDictionary alloc] initWithObjectsAndKeys:movieClip, animation, nil];
[self addChild:mCurrentMovieClip];
mJuggler = [[SPJuggler alloc] init];
[mJuggler addObject:mCurrentMovieClip];
}
return self;
}
- (void)dealloc
{
[mCurrentMovieClip release];
[mCurrentAnimation release];
[mAnimations release];
[mJuggler release];
[super dealloc];
}
- (void)addMovieClip:(SPMovieClip *)movieClip animation:(NSString *)animation
{
[mAnimations setValue:movieClip forKey:animation];
}
- (void)prepare:(NSString *)animation
{
SPMovieClip *movieClip = [mAnimations valueForKey:animation];
if (movieClip == nil)
return;
[self stop];
[self removeChild:mCurrentMovieClip];
[mJuggler removeObject:mCurrentMovieClip];
self.currentMovieClip = movieClip;
[self addChild:mCurrentMovieClip];
[mJuggler addObject:mCurrentMovieClip];
self.currentAnimation = animation;
}
- (void)play:(NSString *)animation
{
[self prepare:animation];
[mCurrentMovieClip play];
}
- (void)pause
{
[mCurrentMovieClip pause];
}
- (void)stop
{
[mCurrentMovieClip stop];
}
#pragma Animatable Delegate Methods
- (void)advanceTime:(double)seconds
{
[mJuggler advanceTime:seconds];
}
- (BOOL)isComplete
{
return NO;
}
@end
#import "RCAnimatedSprite.h"
@implementation Game {
RCAnimatedSprite *anim;
}
- (id)init
{
if ((self = [super init])) {
SPTextureAtlas *atlas = [SPTextureAtlas atlasWithContentsOfFile:@"atlas.xml"];
SPMovieClip *walkMovieClip = [SPMovieClip movieWithFrames:[atlas texturesStartingWith:@"walk_"] fps:10];
SPMovieClip *runMovieClip = [SPMovieClip movieWithFrames:[atlas texturesStartingWith:@"run_"] fps:10];
SPMovieClip *jumpMovieClip = [SPMovieClip movieWithFrames:[atlas texturesStartingWith:@"jump_"] fps:10];
anim = [[RCAnimatedSprite alloc] initWithMovieClip:walkMovieClip animation:@"walk"];
[anim addMovieClip:runMovieClip animation:@"run"];
[anim addMovieClip:jumpMovieClip animation:@"jump"];
[self addChild:anim];
[self addEventListener:@selector(onEnterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
[self addEventListener:@selector(onAddedToStage:) atObject:self forType:SP_EVENT_TYPE_ADDED_TO_STAGE];
}
return self;
}
- (void)onAddedToStage:(SPEvent *)event
{
[anim play:@"walk"];
// [anim play:@"jump"];
// [anim play:@"run"];
}
- (void)onEnterFrame:(SPEnterFrameEvent *)event
{
double passedTime = event.passedTime;
[anim advanceTime:passedTime];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment