Using Transitions Between Scenes

Scenes are the basic building blocks of games. Typically, you design self-contained scenes for the parts of your game, and then transition between these scenes as necessary. For example, you might create different scene classes to represent any or all of the following concepts:

When you present a new scene in a view that is already presenting a scene, you have the option of using a transition to animate the change from the old scene to the new scene. Using a transition provides some continuity, so that the scene change is not quite so abrupt. The complete list of transitions is available in SKTransition Class Reference.

Transitioning Between Two Scenes

Typically, you transition to a new scene based on gameplay or user input. For example, if the user presses a button in your main menu scene, you might transition to a new scene to configure the match the player wants to play. Listing 5-1 shows how you might implement the event handler in a sprite. The handler first runs an animation on itself to highlight the button (not described here). Then, it creates a transition object and the new scene. Finally, it calls the view to present the new scene. The transition means that this change is animated.

Listing 5-1  Transitioning to a new scene

- (void)mouseUp:(NSEvent *)theEvent
{
    [self runAction: self.buttonPressAnimation];
    SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
    GameConfigScene *newScene = [[GameConfigScene alloc] initWithSize: CGSizeMake(1024,768)]];
//  Optionally, insert code to configure the new scene.
    [self.scene.view presentScene: newScene transition: reveal];
}

When the transition occurs, the scene property is immediately updated to point to the new scene. Then, the animation occurs. Finally, the strong reference to the old scene is removed. If you need to keep the scene around after the transition occurs, your app needs to keep its own strong reference to the old scene.

When organizing your game, it can be helpful to create a diagram that shows all the scenes in a game, the transitions that occur between scenes, and the data that must be passed to the new scene when a transition occurs. Unlike view controllers in iOS, Sprite Kit does not provide a built-in mechanism for passing data between scenes. If you need to provide data during a scene transition, you need to implement your own mechanism to configure the new scene. Typically, this means defining your own custom methods and properties on each scene or doing so in a protocol implemented by the custom scene class.

Configuring Whether Animations Play During the Transition

The pausesIncomingScene and pausesOutgoingScene properties on the transition object define which animations are played during the transition. By default, both scenes continue to process animation during the transition. However, you might want to pause one or both of the scenes until the transition completes. For example, consider the code again in Listing 5-1. Because the button is going to run an action, this code expects the outgoing scene to be animated. But perhaps the incoming scene should not animate its content until the transition completes. Adding the code in Listing 5-2 has the desired effect.

Listing 5-2  Pausing frame processing during a transition

reveal.pausesIncomingScene = NO;

Detecting When a Scene is Presented

Sometimes you need to be able to detect that a scene has been presented or removed from a view. You do so by implementing one or both of the following methods on your custom scene class:

When a scene is presented without a transition, the old scene is removed first, and then the new scene is presented. When a transition is used, the new scene is added first, then the transition occurs, and finally the old scene is removed.