Game Development Reference
In-Depth Information
3.
Add the following code at the end of the ERGPlayer.m init method:
[self setupAnimations];
[self runAction:[SKAction repeatActionForever:[SKAction
animateWithTextures:self.runFrames timePerFrame:0.05 resize:YES
restore:NO]] withKey:@"running"];
First, we will call the setupAnimations function, where we will create an
animation from the atlas. On the second line, we will create an action that
repeats forever, takes an array of animation frames, and animates the sprite,
showing each frame for 0.05 seconds. The resize property is needed to adjust
the size of the sprite if it is smaller or larger in a new frame of animation.
The restore property changes the sprite back to the texture it had before
animation. If we add a key to the animation, we will be able to find it later
and remove it if needed.
4.
The next thing to add is the method that makes that animation; add it
to ERGPlayer.m :
- (void) setupAnimations
{
self.runFrames = [[NSMutableArray alloc] init];
SKTextureAtlas *runAtlas = [SKTextureAtlas atlasNamed:@"run"];
for (int i = 0; i < [runAtlas.textureNames count]; i++) {
NSString *tempName = [NSString
stringWithFormat:@"run%.3d", i];
SKTexture *tempTexture = [runAtlas textureNamed:tempName];
if (tempTexture) {
[self.runFrames addObject:tempTexture];
}
}
}
 
Search WWH ::




Custom Search