As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

120,653

Views

Beginner

Level

25

Comments

Changing Scenes

by Jon (Updated on 2016-12-06)


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.

stencyl-scene-flow-diagram

Contents

  • How To: Changing Scenes
  • Gotchas
  • Transitions
  • 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 (under Scene > Game Flow) to perform a scene change.

stencyl-design-mode-switch-scene-blocks

If it isn't apparent, a reload will "switch" to the current scene.

Gotchas

  • Don't perform 0 second transitions. Instead, go with a low number such as 0.01, for stability.

  • 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.

Transitions

Transitions are visual effects that are applied during a scene change. You can select different transitions for the "out" and the "in" portion of a change.

Stencyl supports these transition types.

  • Fade
  • Blinds
  • Bubbles
  • Spotlight
  • Blur
  • Box
  • Crossfade

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.

stencyl-design-mode-switch-scene-using-door

Example: Level Selector

Building a Level Select screen is common to many games. Here’s a simple and elegant way to make one.

stencyl-design-mode-level-selector-example

The Concepts

To pull this off, I’ll use the “get scene with name” block (Scene > World). This block returns a scene, given a name.

stencyl-design-mode-get-scene-name-block

Now, I can drag this block into our regular scene switch block. (If you didn't know you could do that, now you know.)

stencyl-design-mode-switch-scene-drag-block

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?

stencyl-assigning-level-names

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”

stencyl-design-mode-switch-scene-get-scene-names

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.

Exercise: You should extend this, so that it wraps over and starts a new line upon hitting the 6th button)

stencyl-design-mode-create-scene-switching-buttons

Note: The "for Last Created Actor, set ..." block is a special block that sets the value of a different Behavior's attribute. In this case, we're telling the block what Scene it's associated with, otherwise it wouldn't know which Scene to switch to and what to draw. We cover the concept of setting attribute values of other behaviors in Chapter 5.

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.

stencyl-design-mode-draw-button

2) One event that switches the scene when the button is pressed.

stencyl-design-mode-switch-scene-on-mouse-click

That's it!

Summary

  • 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).

stencyl-design-mode-get-scene-name-block

Use this block to implement a generic, reliable system for placing an actor at the “right” spot, much like the Zelda game does.

Print Article Edit Article How to Edit an Article
Disclaimer: Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

25 Comments

always1j
I am working on a RPG style game. My problem is, if the player leaves a building, I need him to be standing outside the proper door when the town scene loads. Any ideas?
-1 10 years, 9 months ago
Karutobi
Has anyone completed the Mini-Challenge for 'The Implementation: Creating the buttons' - Slightly stuck on this...
0 10 years, 9 months ago
SkyBlaze34712
How do I make it so when there are no actors of a group alive, it goes to another scene?
0 10 years, 10 months ago
Nafees
How to make it go to the 'next' scene????
-2 11 years, 1 month ago
bigmike123
now I realize that I should have read the challenge more carefully
0 11 years, 3 months ago
bigmike123
I solved the zelda challenge by:
creating a number attribute,
in the first scene I added an event to set the attribute to the y of self, also in the first scene, I added an event to switch to the second level when the left side of the scene was reached.
In the second scene I added an event to create an actor at x:1 and y:the attribute I first created, that woud be the y of self when the actor left the last scene.
I didn't use the "get scene with name" block.

0 11 years, 3 months ago
spinnerbox
I really like the transitions :)
-1 11 years, 3 months ago
rogerbordini
Hey guys, I need to do exactly the same "Actor’s Position in `Next` Scene" as in Zelda. Please, anyone knows how to do that?
-1 11 years, 9 months ago
Bathrobeman
When i change scene, the tiles (but not the actor) in the next scene are invisible. I tried everything but it doesnt help. plzz help me!!!!!!

PS.: My game is a platform game

-1 12 years, 2 months ago
MrRoboman4321
@JamesJanzen I do. Make it so 'Set(LockedLevels) to FarthestOpenLevel on'.
-1 12 years, 3 months ago

Sign In to Comment