Game Development Reference
In-Depth Information
3.
Add the following code to the setupAnimations method in ERGPLayer.m .
It does the same, in that we create an array and add frames to it:
self.jumpFrames = [[NSMutableArray alloc] init];
SKTextureAtlas *jumpAtlas = [SKTextureAtlas
atlasNamed:@"jump"];
for (int i = 0; i < [runAtlas.textureNames count]; i++) {
NSString *tempName = [NSString
stringWithFormat:@"jump%.3d", i];
SKTexture *tempTexture = [jumpAtlas
textureNamed:tempName];
if (tempTexture) {
[self.jumpFrames addObject:tempTexture];
}
}
4.
Add a new method to handle jumping:
- (void) startJumpingAnimation
{
if (![self actionForKey:@"jumping"]) {
[self runAction:[SKAction sequence:@[[SKAction
animateWithTextures:self.jumpFrames timePerFrame:0.03 resize:YES
restore:NO],[SKAction runBlock:^{
self.animationState = playerStateInAir;
}]]] withKey:@"jumping"];
}
}
The only interesting thing in this method is the new action—the runBlock
action. It lets you run any code in action, and here we set the player's state to
playerStateInAir , which means that the player is neither running nor jumping.
This is pretty much it. But how can we determine when to execute the jump
animation or run animation? We don't want our character to run while they
are in the air. This is why we need a state system for a player sprite.
 
Search WWH ::




Custom Search