by Jon (Updated on 2014-01-26)
As we described in the Scene Basics section, scenes control a game’s flow and can be thought of as various states in a game that you transition between, like a story.
- How To: Changing Scenes
- Example: Enter Door, Switch Scene
- Example: Level Selector
How To: Changing Scenes
Changing a scene involves 3 parts.
- What scene you want to go to next (or reload the same scene)
- The exit transition
- The entry transition
Use the following blocks (Scene > Game Flow) to perform a scene change.
(If it isn't apparent, a reload will "switch" to the current scene)
1) Resist the temptation to perform 0 second transitions. Instead, go with a low number such as 0.01, for stability.
2) You cannot perform multiple scene changes at the same time. Once a scene change is in progress, it must reach completion before another scene change is initiated. Any new requests for scene changes will be ignored.
Stencyl supports 7 different kinds of transitions for scene changes. Transitions are the visual effects applied
You can preview all of these transitions in the following demo.
Example: Enter Door, Switch Scene
In this simple example, we’ll make the hero enter the cave once he steps into the cave’s door, represented by a Region.
I opted to use a “Actor Type Enters Region” event and placed a Region over the door to detect entry.
Example: Level Selector
Building a Level Select screen is common to many games. Here’s a simple and elegant way to make one in just a couple blocks.
- Unzip and stick the project into your Games directory as "Level Selector Demo"
- Don't know where your Games directory is? Click the "View Games Directory" button at the bottom of the Welcome Center (the first screen you see after opening Stencyl)
To pull this off, I’ll use the “get scene with name” block (Scene > World). This block returns a scene, given a name.
Now, I can drag this block into our regular scene switch block. You knew you could do that, didn’t you?
Our goal is to make a simple behavior that we can reuse. To pull off a clean Level Select, our scenes have to have a predictable naming scheme. How about we just give each level a number as a name, starting from 1?
If we do that, we can then pass a Number Attribute into the “get scene with name” block, and it will magically switch to the scene with that “name”
The Implementation: Creating the Buttons
Putting this all together, here’s a behavior that creates 5 Level Select buttons in a row, spaced apart by 100 pixels.
In this case, we're effectively telling the block what Scene it's associated with, otherwise it wouldn't know which Scene to switch to and what to draw.
To learn about setting (and getting) Attribute values from outside their own behavior, we've got an article on that.
The Implementation: The Buttons' Logic
Each Level Select button contains 2 Events and a single Number Attribute (called “SceneNumber” that tells the button what scene it should represent in the Level Select.
1) One event that draws the button.
2) One event that switches the scene when the button is pressed.
That's it! If you’d like to play further this with example, download it above.
- Scenes are the states in a game.
- Changing scenes involves an exit transition and entry transition.
- You can only perform one scene change at a time.
Challenge: Actor’s Position in “Next” Scene
In Zelda, when Link walks off the edge of a room, he naturally appears at the “right” spot in the next room.
For example, if he steps off the right side of a room, he will usually appear at the left side of the next room.
This turns out to be a challenge because you need to reposition your hero, based on where he came from before.
Fortunately, there’s a block that can help you out (Scene > Game Flow).
Use this block to implement a generic, reliable system for placing an actor at the “right” spot, much like the Zelda game does.