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()andisOn()ask for the current setting.- They are sprites:
setPosition,hideandremovework as usual. setLabelchanges the text,setEnabled(false)greys one out.setBackgroundColor,setFocusColorandsetTextColorchange 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_backis 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.setStagewith another stage, as in "Restart", closes the menu too.- Only one menu at a time.
Next
- Pause menu example: a whole game with options, key settings and full screen
- Saving: keep the settings for the next time
- Button, Slider and pauseWith in the reference