setScreen

Category:UI Controls

Sets the screen to the given screenId.

Most apps are not constructed with only one screen, but have multiple screens for the user to interact with. Maybe first a logon screen, then the screens for the app itself, ending with "game over" or "thank you" screen possibly including a "play again" option. Each screen has its own UI elements with corresponding event handlers.

Examples

                            // Screen swap. screen2, textLabels and buttons were added in Design mode.
onEvent("screen1button", "click", function(event) {
  setScreen("screen2");
});
onEvent("screen2button", "click", function(event) {
  setScreen("screen1");
});
                        

Syntax

setScreen(screenId)

Parameters

NameTypeRequired?Description
screenId string The unique identifier for the screen. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _.

Returns

No return value. Modifies screen only.

Tips

  • The default screenId is "screen1".
  • It is important to use meaningful screen IDs, and meaningful anmd unique UI element IDs, so it is easy to tell which UI elements are on which screen.

Found a bug in the documentation? Let us know at documentation@code.org