Menu help?

V

[void]

Guest
I've been trying to create a menu lately, but it just doesn't seem to work right and I don't know why. Maybe someone else can spot something?

Here's the event that creates it, in the player object's [press Enter]:

if room=room_2 or room=room_2_post{
if place_meeting(x,y,obj_wall_room2){
if obj_room2_text_controller.active=0{
instance_create(0,60,obj_textbox);
instance_create(10,70,obj_room2_text);
}
}
else
{
if !instance_exists(obj_menu_box){
instance_create(view_xview[0],view_yview[0],obj_menu_box);
instance_create(view_xview[0]+10,view_yview[0]+20,obj_menu);
}
}
}
else
{
}

And here's the code from each of the three objects that make up the menu:

Information about object: obj_menu_box
Sprite: spr_menu
Solid: false
Visible: true
Depth: 0
Persistent: true
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

selecttalk=1;
selectreset=0;
selectquit=0;
selectsave=0;
selectnmm=0;
selectchrys=0;
selecttir=0;
selectsom=0;
image_index=0;
selectgroup=0;
selectruby=0;
selectyes=0;
selectno=0;

Step Event:
execute code:

obj_player.canmove=0;
if sprite_index=spr_menu{
if selectsave=1{
image_index=2;
}
if selectreset=1{
image_index=1;
}
if selecttalk=1{
image_index=0;
}
if selectquit=1{
image_index=3;
}
}
else
{
if sprite_index=spr_menu_decision{
if selectyes=1{
image_index=1;
}
if selectno=1{
image_index=0;
}
}
}






Information about object: obj_menu
Sprite:
Solid: false
Visible: true
Depth: -1
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

text=1
str[1]=''
textmax=0
i=0
imax=0
nextsound=0
written=0
obj_player.canmove=0;
instance_create(x,y,obj_menu_select);

Destroy Event:
execute code:

obj_player.canmove=1;

Step Event:
execute code:

x=view_xview[0]+10;
y=view_yview[0]+10;
if i=imax{
written=1;
}
obj_player.canmove=0;

execute code:

imax=string_length(str[text])
if i<imax
{
i+=1
}
if keyboard_check_pressed(ord('Z'))
{
if i!=imax
{
i=imax
}
else
{
if text<textmax
{
text+=1
i=0
}
}
}

if i is less than imax
execute code:

nextsound += .5;

if nextsound = 1{
audio_play_sound(snd_plaintext,10,0);
nextsound = 0;
}

if i is equal to imax
stop sound snd_plaintext
Draw Event:
execute code:

draw_set_color(c_white)
draw_set_font(global.hudfontnew)
draw_text_transformed(x,y,string_copy(str[text],0,i),1,1,0)





Information about object: obj_menu_select
Sprite: spr_marker
Solid: false
Visible: true
Depth: -2
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Key Press Event for <Enter> Key:
execute code:

if obj_menu_box.selectsave=1{
game_save(savefile);
obj_menu.str[1]="[GAME SAVED]"
}


if obj_menu_box.selectreset=1{
obj_menu.str[1]="Are you sure? This will destroy all your progress."
obj_menu_box.sprite_index=spr_menu_decision;
obj_menu_box.selectno=1;
}


if obj_menu_box.selecttalk=1{
if global.nmmspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon"
}
if global.chrysspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis"
}
if global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Sombra"
}
if global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Tirek"
}
if global.chrysspared=1 and global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Sombra
-Talk to group"
}
if global.chrysspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Tirek
-Talk to group"
}
if global.somspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Sombra
-Talk to Tirek
-Talk to group"
}
if global.nmmspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Tirek
-Talk to group"
}
if global.chrysspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Tirek
-Talk to group"
}
if global.somspared=1 and global.nmmspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Sombra
-Talk to Nightmare Moon
-Talk to group"
}
if global.chrysspared=1 and global.somspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Sombra
-Talk to Tirek
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1 and global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to Sombra
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to Tirek
-Talk to group"
}
if global.nmmspared=1 and global.tirspared=1 and global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Tirek
-Talk to Sombra
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1 and global.somspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to Sombra
-Talk to Tirek
-Talk to group"
}
else
{
obj_menu.str[1]="-Call Ruby"
}
}


if obj_menu_box.selectquit=1{
room_goto(rm_gameover);
}


else
{
obj_menu.str[1]="What would you like to do?"
}

Key Press Event for <Left> Key:
execute code:

audio_play_sound(snd_select,1,false);
if obj_menu_box.sprite_index=spr_menu{
if obj_menu_box.selectreset=1{
obj_menu_box.selectreset=0;
obj_menu_box.selecttalk=1;
}
if obj_menu_box.selecttalk=1{
obj_menu_box.selectquit=1;
obj_menu_box.selecttalk=0;
}
if obj_menu_box.selectsave=1{
obj_menu_box.selectreset=1;
obj_menu_box.selectsave=0;
}
if obj_menu_box.selectquit=1{
obj_menu_box.selectsave=1;
obj_menu_box.selectquit=0;
}
}
else
{
if obj_menu_box.sprite_index=spr_menu_decision{
if obj_menu_box.selectyes=1{
obj_menu_box.selectyes=0;
obj_menu_box.selectno=1;
}
if obj_menu_box.selectno=1{
obj_menu_box.selectyes=1;
obj_menu_box.selectno=0;
}
}
}

Key Press Event for <Right> Key:
execute code:

audio_play_sound(snd_select,1,false);
if obj_menu_box.sprite_index=spr_menu{
if obj_menu_box.selectreset=1{
obj_menu_box.selectreset=0;
obj_menu_box.selectsave=1;
}
if obj_menu_box.selecttalk=1{
obj_menu_box.selectreset=1;
obj_menu_box.selecttalk=0;
}
if obj_menu_box.selectsave=1{
obj_menu_box.selectsave=0;
obj_menu_box.selectquit=1;
}
if obj_menu_box.selectquit=1{
obj_menu_box.selectquit=0;
obj_menu_box.selecttalk=1;
}
}
else
{
if obj_menu_box.sprite_index=spr_menu_decision{
if obj_menu_box.selectyes=1{
obj_menu_box.selectyes=0;
obj_menu_box.selectno=1;
}
if obj_menu_box.selectno=1{
obj_menu_box.selectyes=1;
obj_menu_box.selectno=0;
}
}
}





So where I'm having trouble is during the key press Left and Right events for obj_menu_select.
upload_2016-7-22_14-32-25.png

When right is pressed and RESET is selected, it goes to TALK instead of SAVE. When left is pressed and RESET is selected, it goes to SAVE instead of TALK. It never, ever goes to QUIT, if right is pressed when on SAVE or left is pressed when on TALK.

Ideas?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
It's difficult to see what the issue is when the code is presented as a massive wall of text like that... However I can give you some tips on what you can do... First off would be to run the game in DEBUG MODE and then use the pause and step buttons along with breakpoints (search the manual for info on these things) so you can see what the code is all doing one line at a time. That should permit you to find the error yourself. :)

I would suggest that you take a completely different approach to making the menu though... My own solution to menus is to use a controller object to keep track of everything and only have the actual button object for the graphics. So you'd create a controller that stores the current position of the "active" button and then as the player presses keys or moves the mouse it polls this input and changes the active position accordingly.

So, for example (quick and dirty "pseudo code" to give the idea):
Code:
// CREATE EVENT
active = 0;
button_x[0] = 0;
button_y[0] = 0;
button_x[1] = 32;
button_y[1] = 0;
button_w = 32;
button_h = 16;

// STEP EVENT
for (var i = 0; i < 2; i++;)
{
if point_in_rectangle(mouse_x, mouse_y, button_x[i], button_y[i], button_x[i] + button_w, button_y[i] + button_h)
    {
    // Mouse is within one of the button bounds so set the active variable
    active = i;
    break;
    }
}

if keyboard_check_pressed(vk_right)
{
active++;
if active > 1 active = 0;
}

if keyboard_check_pressed(vk_left)
{
active--;
if active < 0 active = 1;
}

if keyboard_check_pressed(vk_enter) || mouse_check_button_pressed(mb_left)
{
switch (active)
    {
    case 0: Button 0 pressed so do something; break;
    case 1: button 1 pressed so do something; break;
    }
}
That's about it. I would probably also use the controller for drawing the buttons themselves but that's up to you... instances can be easier for nice transition effects, etc...

Hope that helps!
 
V

[void]

Guest
Mine doesn't use a mouse, but I ended up changing it to something that worked:

In the Left and Right events, I added in a line that increased or decreased obj_menu_box's image_index by 1, and then the following in the Enter event below:

Information about object: obj_menu_select
Sprite: spr_marker
Solid: false
Visible: true
Depth: -2
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Key Press Event for <Enter> Key:
execute code:

if obj_menu_box.image_index=0{
if obj_menu_box.sprite_index=spr_menu{
obj_menu_box.selecttalk=1;
}
else
{
obj_menu_box.selectyes=1;
}
}
if obj_menu_box.image_index=1{
if obj_menu_box.sprite_index=spr_menu{
obj_menu_box.selectreset=1;
}
else
{
obj_menu_box.selectno=1;
}
}
if obj_menu_box.image_index=2{
obj_menu_box.selectsave=1;
}
if obj_menu_box.image_index=3{
obj_menu_box.selectquit=1;
}




if obj_menu_box.selectsave=1{
game_save("savefile.dat");
obj_menu.str[1]="[GAME SAVED]"
}


if obj_menu_box.selectreset=1{
obj_menu.str[1]="Are you sure? This will destroy all your progress."
obj_menu_box.sprite_index=spr_menu_decision;
obj_menu_box.image_index=0;
}


if obj_menu_box.selecttalk=1{
if global.nmmspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon"
}
if global.chrysspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis"
}
if global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Sombra"
}
if global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Tirek"
}
if global.chrysspared=1 and global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Sombra
-Talk to group"
}
if global.chrysspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Tirek
-Talk to group"
}
if global.somspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Sombra
-Talk to Tirek
-Talk to group"
}
if global.nmmspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Tirek
-Talk to group"
}
if global.chrysspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Tirek
-Talk to group"
}
if global.somspared=1 and global.nmmspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Sombra
-Talk to Nightmare Moon
-Talk to group"
}
if global.chrysspared=1 and global.somspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Chrysalis
-Talk to Sombra
-Talk to Tirek
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1 and global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to Sombra
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to Tirek
-Talk to group"
}
if global.nmmspared=1 and global.tirspared=1 and global.somspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Tirek
-Talk to Sombra
-Talk to group"
}
if global.nmmspared=1 and global.chrysspared=1 and global.somspared=1 and global.tirspared=1{
obj_menu.str[1]="-Call Ruby
-Talk to Nightmare Moon
-Talk to Chrysalis
-Talk to Sombra
-Talk to Tirek
-Talk to group"
}
else
{
obj_menu.str[1]="-Call Ruby"
}
}


if obj_menu_box.selectquit=1{
room_goto(rm_gameover);
with obj_menu instance_destroy();
with obj_menu_box instance_destroy();
instance_destroy();
}


else
{
obj_menu.str[1]="What would you like to do?"
}

Key Press Event for <Left> Key:
execute code:

audio_play_sound(snd_select,1,false);
obj_menu_box.image_index--;
if obj_menu_box.sprite_index=spr_menu{
if obj_menu_box.selectreset=1{
obj_menu_box.selectreset=0;
obj_menu_box.selecttalk=1;
}
if obj_menu_box.selecttalk=1{
obj_menu_box.selectquit=1;
obj_menu_box.selecttalk=0;
}
if obj_menu_box.selectsave=1{
obj_menu_box.selectreset=1;
obj_menu_box.selectsave=0;
}
if obj_menu_box.selectquit=1{
obj_menu_box.selectsave=1;
obj_menu_box.selectquit=0;
}
}
else
{
if obj_menu_box.sprite_index=spr_menu_decision{
if obj_menu_box.selectyes=1{
obj_menu_box.selectyes=0;
obj_menu_box.selectno=1;
}
if obj_menu_box.selectno=1{
obj_menu_box.selectyes=1;
obj_menu_box.selectno=0;
}
}
}

Key Press Event for <Right> Key:
execute code:

audio_play_sound(snd_select,1,false);
obj_menu_box.image_index++;
if obj_menu_box.sprite_index=spr_menu{
if obj_menu_box.selectreset=1{
obj_menu_box.selectreset=0;
obj_menu_box.selectsave=1;
}
if obj_menu_box.selecttalk=1{
obj_menu_box.selectreset=1;
obj_menu_box.selecttalk=0;
}
if obj_menu_box.selectsave=1{
obj_menu_box.selectsave=0;
obj_menu_box.selectquit=1;
}
if obj_menu_box.selectquit=1{
obj_menu_box.selectquit=0;
obj_menu_box.selecttalk=1;
}
}
else
{
if obj_menu_box.sprite_index=spr_menu_decision{
if obj_menu_box.selectyes=1{
obj_menu_box.selectyes=0;
obj_menu_box.selectno=1;
}
if obj_menu_box.selectno=1{
obj_menu_box.selectyes=1;
obj_menu_box.selectno=0;
}
}
}

Still a few bugs to work out, but I can at least use the menu now!
 
A

Aura

Guest
For future reference, you might want to put the code in the code boxes:

Code:
//Your code here
To do that, go to Insert >> Code. You may use the [code]...[/code] BB code. Also, do not post all the code; post the relevant code only. If there's too much of code, put it in spoilers. It would make it easier for people to help you.

As a side note, if the problem has been solved, you might want to set the status of the topic as solved. To do that, go to Edit Title/Thread Tools >> Edit Title >> Set the topic prefix to solved.
 
Top