Movie Clips

Sparrow contains a very lightweight movie class: SPMovieClip. You can imagine this class as an SPImage with changing textures. (As it extends SPImage, it is really just that.)

This sample shows how to create a movie clip. Here, we load the textures from a texture atlas, but you can of course use any textures you want. However, each texture should have the same size.

// load frames from atlas
SPTextureAtlas *atlas = [SPTextureAtlas atlasWithContentsOfFile:@"atlas.xml"];
SPTexture *texture0 = [atlas textureByName:@"frame0"];
SPTexture *texture1 = [atlas textureByName:@"frame1"];
SPTexture *texture2 = [atlas textureByName:@"frame2"];
// create movie clip
SPMovieClip *movie = [[SPMovieClip alloc] initWithFrame:texture0 fps:10];
[movie addFrame:texture1];
[movie addFrame:texture2];
// control playback:
[movie play];
[movie pause];
// looping:
movie.loop = NO; // default: YES
// important: add clip to juggler
[self.stage.juggler addObject:movie]

We created a movie with 3 frames and a playback rate of 10 frames per second. Like all animations, it has to be handed over to a juggler — don't forget that!

You can also use custom durations for certain frames, and start playback of a sound whenever a certain frame is displayed.

// add another frame that is displayed for half a second
[movie addFrame:frame3 withDuration:0.5];
// add sound at frame 0
SPSound *sound = [SPSound soundWithContentsOfFile:@"sound.caf"];
[movie setSound:[sound createChannel] atIndex:0];

A movie clip dispatches the event SP_EVENT_TYPE_MOVIE_COMPLETED whenever it has displayed its final frame (once per loop).

Sparrow 1.0 added new methods to SPMovieClip and SPTextureAtlas, which make it extremely simple to load a movie. Here's how it's done in the demo project:

NSArray *frames = [atlas texturesStartingWith:@"walk_"];
mMovie = [[SPMovieClip alloc] initWithFrames:frames fps:12];

Next section: Audio Playback

  manual_v1/movie_clips.txt · Last modified: 2013/03/05 10:19 (external edit)
Powered by DokuWiki