Creating a Main Menu

Материал из Поле цифровой дидактики

A Main Menu is a menu, usually used in games, placed at the beginning of a project. It allows the user to decide to play the game, or use other features within the project, such as character customization. Main menus mostly consist of various buttons for accessing the different parts of a project. For this tutorial, it is assumed that the majority of the project's body is complete.

Basic Menu

Broadcasting is useful for main menus because it allows them to be easily pulled up multiple times in a project. It avoids the need to click the green flag to navigate back to the menu. The broadcast used in this tutorial is named open menu In order to start, one will need to replace most "When Green Flag Clicked" script with "When I receive [open menu]" scripts, likely with exceptions. It's recommended to plan for this in advance, and not use "When Green Flag Clicked" hats for the main menu, For example, one's original script might look like this:

when gf clicked
forever
if <key (left arrow v) pressed?> then
change x by (-10)
end
if <key (right arrow v) pressed?> then
change x by (10)
end
if <key (space v) pressed?> then
jump::custom
end 

But after the modification, it would look like this:

when I receive [start game v]
forever
if <key (left arrow v) pressed?> then
change x by (-10)
end
if <key (right arrow v) pressed?> then
change x by (10)
end
if <key (space v) pressed?> then
jump::custom
end

After this, make a separate backdrop for the title screen that includes the title and a new sprite named "Start Button". Inside this sprite, make two scripts:

when I receive [open menu v]
show
switch backdrop to (title backdrop v)

The "Play" button is used for triggering the project to begin. Most often, when the button is clicked a message will be sent signaling the game to begin. The following script represents this:

when this sprite clicked
broadcast (start game v)
switch backdrop to (gameplay backdrop v)
hide

Now when someone starts the project, it will show the menu instead of starting the game automatically.

Adding Title Music

Title music can be added to the game.

Make the following required Variable:

(main menu?)

In any sprite, create a script like this one:

Шаблон:Note

when gf clicked
set [main menu? v] to (1)
forever
if <(main menu?)>[0]> then
play sound (Title Music v) until done
end 

This script says that if the viewer is on the main menu, it will play the music.

Now, modify the Start Button script. Change it from this:

when this sprite clicked
broadcast (start game v)
switch backdrop to (gameplay backdrop v)
hide

to this:

when this sprite clicked
broadcast (start game v)
set [main menu? v] to (0)
stop all sounds
switch backdrop to (gameplay backdrop v)
hide

This prevents the title music from playing again, and then just stops it altogether.

External Links