Scratch for Java

Menus and pause menus

Buttons, switches, sliders and choices are ready-made sprites. Players can use them with the mouse, the keyboard or a controller. You only say what each one does.

A settings menu

Click the stage. Then use the mouse, or the arrow keys and Enter:

Each widget is made with new, and the code in { … } right after it says what happens:

Widget Changed by You write
Button("Start") click, Enter, Space, A whenPressed()
Toggle("Sounds", true) click, Enter, Space, A whenChanged(boolean on)
Slider("Music", 0, 100, 50) left and right, click or drag whenChanged(double value)
Choice("Difficulty", "Easy", …) left and right, or a click whenChanged(String option)
  • The arrow keys (or the D-pad) move the highlight to the nearest widget in that direction. focus() says where it starts.
  • getSelected(), getValue() and isOn() ask for the current setting.
  • They are sprites: setPosition, hide and remove work as usual.
  • setLabel changes the text, setEnabled(false) greys one out.
  • setBackgroundColor, setFocusColor and setTextColor change the colours.

A pause menu

A pause menu is a stage of its own. pauseWith(menu) shows it over the game. The game stands still and shows through, darkened. close() on the menu lets the game go on where it stopped. Click the stage and press Escape:

  • ui_back is Escape, Backspace or B on a controller. It is one of the actions every game has.
  • While the menu is open, only the menu runs and hears keys. Music, sounds and timers go on.
  • getPausedStage() on the menu gives you the game it paused.
  • setStage with another stage, as in "Restart", closes the menu too.
  • Only one menu at a time.

Next

Menus and pause menus

Create Shareable URL

Select Sections