Navigating menus

Blitz3D Forums/Blitz3D Beginners Area/Navigating menus

Yahfree(Posted 2007) [#1]
Hmm, i'v tried a number of things to get the effect of navigation menus, none really work i get a MAV error with some, some just don't work, so how would you do it?

this is how i'm doing it:
i call this at the top of the code before everything else (after apptitle)

Global selection=1
If selection=1 Mainmenu()
If selection=2 options()


these are the funtions, it breaks the loop and goes to the main program when you click the play button, i didnt have enough time to make myself a options button, so i make the [1] key (up top not numberpad) the key to go to the options screen

heres the functions:

Mainmenu()


options()


its works, but when i go to the options screen and click on the back button its SUPPOST to go back to the main menu, but it goes to the game insted...

I'd like to hear your menu tips and of course how you do it.


tigerman(Posted 2007) [#2]
Hi Yahfree,

You need to put a loop around your menu and game. Lets see if I can visualise this for you:

You have:
selection = 1 ; show main menu
Mainmenu() ; this shows the main menu if selection = 1
Once mainmenu ends, be it by clicking on "Options" or "Play game" then next line (Options()) runs.
If you clicked on Options, selection now equals 3 and the options menu is displayed.
If you didn't click on Options, the options() function runs but quits immediately.
After the options() function has ended, it continues to the rest of you game.

What you need is something like this: (note this is not actual useable code, just pseudo code)

selection = 1 ; open main menu first
while not keyhit(1) ; loop until ESC is pressed
  if selection = 1 then
    mainmenu() ; main menu sets selection to 2 if the play game button is pressed or sets selection to 3 if the options button is pressed.
  elseif selection = 2 then
    playgame() ; move game code into its own function.  When game ends set selection = 1 again to show the main menu.
  elseif selection = 3 then
    options() ; display the options menu.  When the back button is pressed, set selection to 1 and the main menu will be shown.
  end if
wend


The top "while not keyhit(1)" loop is not great practice to be honest and you would be better assigning a variable to control the running of this main loop so you can set it where you like in your code (based on a keypress or on a menu button press).

Hope this helps.

Tim


Yahfree(Posted 2007) [#3]
This is what i tried:

on the main page (note not all my code just showing what has to do with the menus)

took almost everything out of my maincode page and put it in a function called playgame()
Global selection=1
;If selection=1 Mainmenu()
;If selection=2 playgame()
;If selection=3 options()

While Not KeyHit(1) ; loop until ESC is pressed
  If selection = 1 Then
    Mainmenu() ; main menu sets selection to 2 if the play game button is pressed or sets selection to 3 if the options button is pressed.
  ElseIf selection = 2 Then
    playgame() ; move game code into its own function.  When game ends set selection = 1 again to show the main menu.
  ElseIf selection = 3 Then
    options() ; display the options menu.  When the back button is pressed, set selection to 1 and the main menu will be shown.
  End If
Wend


My Mainmenu() function


my options() function



my playgame() function


Doesnt work.. it shows the main menu, but when clicking on the play button (change selection to = 2, in thoery calling the playgame() function.)

but it just sits there...


tigerman(Posted 2007) [#4]
Hi yahfree,

Thanks for the additional bits. I have found some mistakes and I have a working version here now however, I need some time to show you the changes so you can understand. I will attach the new code here for now, but I will explain some more if I get chance tomorrow.



I didn't have your pictures and I had to remove some other stuff, hopefully you will get the main idea, but if not, just wait until I can explain more.

Thanks
Tim


Yahfree(Posted 2007) [#5]
That works great on going back and forth between the options thing, but why doesnt it work for back and forth between the game and the main menu?

i want it so if you press esc in the game, it goes to the main menu, if you press esc, at the main menu it quits

so i did the simple

This in the playgame() loop:
If KeyHit(1) Then
selection=1
End If


at this point selection=1 so it SHOULD return to the main menu, but instead it closes, don't know why, theres no code in the playgame() function that says termanate if keyhit(1)..

and i put this in the Mainmenu() function:
;if esc is pressed ONLY AT THE MAIN MENU quit.
If KeyHit(1)
End


it closes ok at the main menu, but when i hit ESC in the game it also closes... any ideas?

End If


tigerman(Posted 2007) [#6]
If you still have all my code in, add this into the WHILE loop inside the MAINMENU function

If KeyHit(1) Then
	ingame = 0
	selection = -1
End If


Also, as you have managed without my explainations, do you want me to explain or do you understand the changes I made?

This way you can have a button on the main menu that sets the same thing and also causes the game to end.

Tim


Yahfree(Posted 2007) [#7]
hmm, alright, i made myself a "Options" button, but cant seem to get it to work, it only seems to work with the first button it checks, options button works if its ontop of the play button, vis versa:

;Collisions basicly making the buttons work
If ImageRectCollide(options,100,450,0,MouseX(),MouseY(),5,5) And MouseHit(1)
selection=3
End If
If ImageRectCollide(continue,100,400,0,MouseX(),MouseY(),5,5) And MouseHit(1)
selection=2
End If



Yahfree(Posted 2007) [#8]
Err, i just tested this code to put in the main menu, (instead of ending) and it ends the program, but ingame when i press keyhit(1) (ESC) it makes the veriable selection=1

this should bring me back to the main menu... but it decides to close it...


tigerman(Posted 2007) [#9]
The problem with the imagecollide bits is that the MouseHit() function resets after it has been tested the first time, so when the second IF statement runs the number of clicks held in this function is 0 and thus the second IF never evaluates to TRUE.

What you need is either:

if MouseHit(1) then
  imagecollide stuff excluding the AND MOUSEHIT(1)
end if


OR

mouseclicked = MouseHit(1)

If ImageRectCollide(options,100,450,0,MouseX(),MouseY(),5,5) And mouseclicked
...


I also took out the DELAY 5 which was causing the results to be eratic at best.

As for your other problem, a) I dont understand and b) it works OK for me now.

Tim


Yahfree(Posted 2007) [#10]
Ok, that seems to work on the some buttons work some don't problem, but i still cant figure out for the life of me why i cant get it when esc is pressed to go back to the mainmenu

i'll give you all my code:


PS. I have the Delay 5 in the main loops because it makes no difference in the program but a HUGE difference in how much CPU it uses


tigerman(Posted 2007) [#11]
Hi Yahfree,

At the end of your playgame function, after the Wend, comment out or delete the line that reads END. This is the problem. Sorry I didn't reply before, been busy. I removed this earlier when I was testing, and forgot to mention it.

Also you will want to add a ShowPointer in the If KeyHit(1) section. The playgame function should look something like this:




Yahfree(Posted 2007) [#12]
THANKS!!! thanks for helping me all this time, i must be a pain :D


Yahfree(Posted 2007) [#13]
Hmm, i must be doing somthing wierd here, it looks a bit wierd setting a graphics mode for each screen when you can just clear the screen for each graphics mode, so i called the graphics mode once before anything else, and took out the graphics modes in the functions, to my excitment it worked, i could go between the options screen and the main menu... but when i tried to go to the main menu from the game screen it freezes it lets the mouse go, kinda like i'm at the right screen... any ideas?

my code:



jfk EO-11110(Posted 2007) [#14]
Eh, that's a huge sourcecode to search for a bug.

I'd suggest to do the following:
Systematicly isolate the bug. One simple solution is to add an end command to a line that you know was still executed before freezing. Then move the end command forward, one by one. Soon you'll realize where it's freezing. That's already half of the work - or more.


Yahfree(Posted 2007) [#15]
Ok, its not "Freezing" per say, it going to the main menu like its suppost to, but not displaying the main menus media (buttons ect) instead it just freezes the gameplay media, strange thing is if i click where the "Play" button is suppost to be it resumes! wierd..