Rather than go round in circles, I've come up with a simple example of a menu system that has a main menu then a single layer of sub-menus.
// written in AGK V2.14b
// This is an example of a menu system where the player navigates a main menu and a single layer of sub-menus
// simply use the mouse or touch screen to select options from the menu
// This uses all text and the "buttons" have square brackets around the text
gosub _declare_variables
_f_screen()
_f_create_menu()
_f_initialize_variables()
// main loop
do
// it makes sense to hide everything and then show the buttons that are required in the separate functions
_f_set_all_menu_text_invisible()
_f_input()
select menu.state
// main menu
case 0
_f_menu_main()
endcase
// view burger
case 1
_f_menu_view_burger()
endcase
// choose meat
case 2
_f_menu_choose_meat()
endcase
// choose toppings
case 3
_f_menu_choose_topping()
endcase
// choose condiments
case 4
_f_menu_choose_condiments()
endcase
endselect
Sync()
loop
_declare_variables:
type _menu_type
main_title as integer
main_view_burger as integer
main_choose_meat as integer
main_choose_topping as integer
main_choose_condiments as integer
view_burger_title as integer
view_burger_meat as integer
view_burger_toppings as integer
view_burger_condiments as integer
choose_meat_title as integer
choose_meat_beef as integer
choose_meat_chicken as integer
choose_meat_spicy_bean as integer
choose_topping_title as integer
choose_topping_cheese as integer
choose_topping_bacon as integer
choose_topping_bacon_cheese as integer
choose_topping_none as integer
choose_condiment_title as integer
choose_condiment_ketchup as integer
choose_condiment_mayo as integer
choose_condiment_relish as integer
choose_condiment_none as integer
back_to_main_menu as integer
state as integer
endtype
global menu as _menu_type
type _ingredients_type
meat$ as string
topping$ as string
condiment$ as string
endtype
global ingredient as _ingredients_type
type _pointer_type
x as integer
y as integer
pressed as integer
endtype
global pointer as _pointer_type
return
function _f_create_menu()
menu.main_title = CreateText("MAIN MENU")
SetTextColor(menu.main_title, 255,255,0,255)
SetTextSize(menu.main_title, 40)
SetTextAlignment(menu.main_title,1)
SetTextPosition(menu.main_title, 512 , 40)
menu.main_view_burger = CreateText("[VIEW BURGER]")
SetTextColor(menu.main_view_burger, 255,255,0,255)
SetTextSize(menu.main_view_burger, 40)
SetTextAlignment(menu.main_view_burger,1)
SetTextPosition(menu.main_view_burger, 512 , 140)
menu.main_choose_meat = CreateText("[CHOOSE MEAT]")
SetTextColor(menu.main_choose_meat, 255,255,0,255)
SetTextSize(menu.main_choose_meat, 40)
SetTextAlignment(menu.main_choose_meat,1)
SetTextPosition(menu.main_choose_meat, 512 , 240)
menu.main_choose_topping = CreateText("[CHOOSE TOPPING]")
SetTextColor(menu.main_choose_topping, 255,255,0,255)
SetTextSize(menu.main_choose_topping, 40)
SetTextAlignment(menu.main_choose_topping,1)
SetTextPosition(menu.main_choose_topping, 512 , 340)
menu.main_choose_condiments = CreateText("[CHOOSE CONDIMENTS]")
SetTextColor(menu.main_choose_condiments, 255,255,0,255)
SetTextSize(menu.main_choose_condiments, 40)
SetTextAlignment(menu.main_choose_condiments,1)
SetTextPosition(menu.main_choose_condiments, 512 , 440)
menu.view_burger_title = CreateText("VIEW BURGER")
SetTextColor(menu.view_burger_title, 255,255,0,255)
SetTextSize(menu.view_burger_title, 40)
SetTextAlignment(menu.view_burger_title,1)
SetTextPosition(menu.view_burger_title, 512 , 40)
menu.view_burger_meat = CreateText("MEAT : please select")
SetTextColor(menu.view_burger_meat, 255,255,0,255)
SetTextSize(menu.view_burger_meat, 40)
SetTextAlignment(menu.view_burger_meat,1)
SetTextPosition(menu.view_burger_meat, 512 , 140)
menu.view_burger_toppings = CreateText("TOPPING : please select")
SetTextColor(menu.view_burger_toppings, 255,255,0,255)
SetTextSize(menu.view_burger_toppings, 40)
SetTextAlignment(menu.view_burger_toppings,1)
SetTextPosition(menu.view_burger_toppings, 512 , 240)
menu.view_burger_condiments = CreateText("CONDIMENT : please select")
SetTextColor(menu.view_burger_condiments, 255,255,0,255)
SetTextSize(menu.view_burger_condiments, 40)
SetTextAlignment(menu.view_burger_condiments,1)
SetTextPosition(menu.view_burger_condiments, 512 , 340)
menu.choose_meat_title = CreateText("CHOOSE YOUR MEAT")
SetTextColor(menu.choose_meat_title, 255,255,0,255)
SetTextSize(menu.choose_meat_title, 40)
SetTextAlignment(menu.choose_meat_title,1)
SetTextPosition(menu.choose_meat_title, 512 , 40)
menu.choose_meat_beef = CreateText("[BEEF]")
SetTextColor(menu.choose_meat_beef, 255,255,0,255)
SetTextSize(menu.choose_meat_beef, 40)
SetTextAlignment(menu.choose_meat_beef,1)
SetTextPosition(menu.choose_meat_beef, 512 , 140)
menu.choose_meat_chicken = CreateText("[CHICKEN]")
SetTextColor(menu.choose_meat_chicken, 255,255,0,255)
SetTextSize(menu.choose_meat_chicken, 40)
SetTextAlignment(menu.choose_meat_chicken,1)
SetTextPosition(menu.choose_meat_chicken, 512 , 240)
menu.choose_meat_spicy_bean = CreateText("[SPICY BEAN]")
SetTextColor(menu.choose_meat_spicy_bean, 255,255,0,255)
SetTextSize(menu.choose_meat_spicy_bean, 40)
SetTextAlignment(menu.choose_meat_spicy_bean,1)
SetTextPosition(menu.choose_meat_spicy_bean, 512 , 340)
menu.choose_topping_title = CreateText("CHOOSE A TOPPING")
SetTextColor(menu.choose_topping_title, 255,255,0,255)
SetTextSize(menu.choose_topping_title, 40)
SetTextAlignment(menu.choose_topping_title,1)
SetTextPosition(menu.choose_topping_title, 512 , 40)
menu.choose_topping_cheese = CreateText("[CHEESE]")
SetTextColor(menu.choose_topping_cheese, 255,255,0,255)
SetTextSize(menu.choose_topping_cheese, 40)
SetTextAlignment(menu.choose_topping_cheese,1)
SetTextPosition(menu.choose_topping_cheese, 512 , 140)
menu.choose_topping_bacon = CreateText("[BACON]")
SetTextColor(menu.choose_topping_bacon, 255,255,0,255)
SetTextSize(menu.choose_topping_bacon, 40)
SetTextAlignment(menu.choose_topping_bacon,1)
SetTextPosition(menu.choose_topping_bacon, 512 , 240)
menu.choose_topping_bacon_cheese = CreateText("[BACON AND CHEESE]")
SetTextColor(menu.choose_topping_bacon_cheese, 255,255,0,255)
SetTextSize(menu.choose_topping_bacon_cheese, 40)
SetTextAlignment(menu.choose_topping_bacon_cheese,1)
SetTextPosition(menu.choose_topping_bacon_cheese, 512 , 340)
menu.choose_topping_none = CreateText("[NONE]")
SetTextColor(menu.choose_topping_none, 255,255,0,255)
SetTextSize(menu.choose_topping_none, 40)
SetTextAlignment(menu.choose_topping_none,1)
SetTextPosition(menu.choose_topping_none, 512 , 440)
menu.choose_condiment_title = CreateText("CHOOSE A CONDIMENT")
SetTextColor(menu.choose_condiment_title, 255,255,0,255)
SetTextSize(menu.choose_condiment_title, 40)
SetTextAlignment(menu.choose_condiment_title,1)
SetTextPosition(menu.choose_condiment_title, 512 , 40)
menu.choose_condiment_ketchup = CreateText("[KETCHUP]")
SetTextColor(menu.choose_condiment_ketchup, 255,255,0,255)
SetTextSize(menu.choose_condiment_ketchup, 40)
SetTextAlignment(menu.choose_condiment_ketchup,1)
SetTextPosition(menu.choose_condiment_ketchup, 512 , 140)
menu.choose_condiment_mayo = CreateText("[MAYO]")
SetTextColor(menu.choose_condiment_mayo, 255,255,0,255)
SetTextSize(menu.choose_condiment_mayo, 40)
SetTextAlignment(menu.choose_condiment_mayo,1)
SetTextPosition(menu.choose_condiment_mayo, 512 , 240)
menu.choose_condiment_relish = CreateText("[RELISH]")
SetTextColor(menu.choose_condiment_relish, 255,255,0,255)
SetTextSize(menu.choose_condiment_relish, 40)
SetTextAlignment(menu.choose_condiment_relish,1)
SetTextPosition(menu.choose_condiment_relish, 512 , 340)
menu.choose_condiment_none = CreateText("[NONE]")
SetTextColor(menu.choose_condiment_none, 255,255,0,255)
SetTextSize(menu.choose_condiment_none, 40)
SetTextAlignment(menu.choose_condiment_none,1)
SetTextPosition(menu.choose_condiment_none, 512 , 440)
menu.back_to_main_menu = CreateText("[BACK TO MAIN MENU]")
SetTextColor(menu.back_to_main_menu, 255,255,0,255)
SetTextSize(menu.back_to_main_menu, 40)
SetTextAlignment(menu.back_to_main_menu,1)
SetTextPosition(menu.back_to_main_menu, 512 , 700)
endfunction
function _f_initialize_variables()
menu.state = 0
ingredient.meat$ = "Choose meat"
ingredient.topping$ = "Choose topping"
ingredient.condiment$ = "Choose condiment"
endfunction
function _f_set_all_menu_text_invisible()
SetTextVisible(menu.main_title,0)
SetTextVisible(menu.main_view_burger,0)
SetTextVisible(menu.main_choose_meat,0)
SetTextVisible(menu.main_choose_topping,0)
SetTextVisible(menu.main_choose_condiments,0)
SetTextVisible(menu.view_burger_title,0)
SetTextVisible(menu.view_burger_meat,0)
SetTextVisible(menu.view_burger_toppings,0)
SetTextVisible(menu.view_burger_condiments,0)
SetTextVisible(menu.choose_meat_title,0)
SetTextVisible(menu.choose_meat_beef,0)
SetTextVisible(menu.choose_meat_chicken,0)
SetTextVisible(menu.choose_meat_spicy_bean,0)
SetTextVisible(menu.choose_topping_title,0)
SetTextVisible(menu.choose_topping_cheese,0)
SetTextVisible(menu.choose_topping_bacon,0)
SetTextVisible(menu.choose_topping_bacon_cheese,0)
SetTextVisible(menu.choose_topping_none,0)
SetTextVisible(menu.choose_condiment_title,0)
SetTextVisible(menu.choose_condiment_ketchup,0)
SetTextVisible(menu.choose_condiment_mayo,0)
SetTextVisible(menu.choose_condiment_relish,0)
SetTextVisible(menu.choose_condiment_none,0)
SetTextVisible(menu.back_to_main_menu,0)
endfunction
function _f_input()
pointer.x = getpointerX()
pointer.y = getpointerY()
pointer.pressed = getpointerPressed()
endfunction
function _f_screen()
SetWindowTitle( "menu_example" )
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
endfunction
function _f_menu_main()
// set text visible
SetTextVisible(menu.main_title,1)
SetTextVisible(menu.main_view_burger,1)
SetTextVisible(menu.main_choose_meat,1)
SetTextVisible(menu.main_choose_topping,1)
SetTextVisible(menu.main_choose_condiments,1)
// set the color of the buttons to yellow
SetTextColor(menu.main_view_burger,255,255,0,255)
SetTextColor(menu.main_choose_meat,255,255,0,255)
SetTextColor(menu.main_choose_topping,255,255,0,255)
SetTextColor(menu.main_choose_condiments,255,255,0,255)
// when the point it over the buttont then set the text green. If the pointer is pressed then set the menu state
if GetTextHitTest(menu.main_view_burger, pointer.x,pointer.y) = 1
SetTextColor(menu.main_view_burger,0,255,0,255)
if pointer.pressed = 1
menu.state = 1
endif
endif
if GetTextHitTest(menu.main_choose_meat, pointer.x,pointer.y) = 1
SetTextColor(menu.main_choose_meat,0,255,0,255)
if pointer.pressed = 1
menu.state = 2
endif
endif
if GetTextHitTest(menu.main_choose_topping, pointer.x,pointer.y) = 1
SetTextColor(menu.main_choose_topping,0,255,0,255)
if pointer.pressed = 1
menu.state = 3
endif
endif
if GetTextHitTest(menu.main_choose_condiments, pointer.x,pointer.y) = 1
SetTextColor(menu.main_choose_condiments,0,255,0,255)
if pointer.pressed = 1
menu.state = 4
endif
endif
endfunction
function _f_menu_view_burger()
// set sub menu text visible
SetTextVisible(menu.view_burger_title,1)
SetTextVisible(menu.view_burger_meat,1)
SetTextVisible(menu.view_burger_toppings,1)
SetTextVisible(menu.view_burger_condiments,1)
SetTextVisible(menu.back_to_main_menu,1)
// set color of back button to yellow
SetTextColor(menu.back_to_main_menu,255,255,0,255)
// set the string for the burger ingredients
SetTextString(menu.view_burger_meat,ingredient.meat$)
SetTextString(menu.view_burger_toppings,ingredient.topping$)
SetTextString(menu.view_burger_condiments,ingredient.condiment$)
// when pointer is over back button highlight it green then change menu.state if pressed
if GetTextHitTest(menu.back_to_main_menu, pointer.x,pointer.y) = 1
SetTextColor(menu.back_to_main_menu,0,255,255,255)
if pointer.pressed = 1
menu.state = 0
endif
endif
endfunction
function _f_menu_choose_meat()
// set all sub menu text visible
SetTextVisible(menu.choose_meat_title,1)
SetTextVisible(menu.choose_meat_beef,1)
SetTextVisible(menu.choose_meat_chicken,1)
SetTextVisible(menu.choose_meat_spicy_bean,1)
SetTextVisible(menu.back_to_main_menu,1)
// set all sub menu text to yellow
SetTextColor(menu.choose_meat_beef,255,255,0,255)
SetTextColor(menu.choose_meat_chicken,255,255,0,255)
SetTextColor(menu.choose_meat_spicy_bean,255,255,0,255)
SetTextColor(menu.back_to_main_menu,255,255,0,255)
// if a selection has been made then highlight it green
select ingredient.meat$
case "beef"
SetTextColor(menu.choose_meat_beef,0,255,0,255)
endcase
case "chicken"
SetTextColor(menu.choose_meat_chicken,0,255,0,255)
endcase
case "spicy bean"
SetTextColor(menu.choose_meat_spicy_bean,0,255,0,255)
endcase
endselect
// select an option (highlight in cyan when pointer is over the text then set variable if pointer is pressed
if GetTextHitTest(menu.choose_meat_beef, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_meat_beef,0,255,255,255)
if pointer.pressed = 1
ingredient.meat$ = "beef"
endif
endif
if GetTextHitTest(menu.choose_meat_chicken, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_meat_chicken,0,255,255,255)
if pointer.pressed = 1
ingredient.meat$ = "chicken"
endif
endif
if GetTextHitTest(menu.choose_meat_spicy_bean, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_meat_spicy_bean,0,255,255,255)
if pointer.pressed = 1
ingredient.meat$ = "spicy bean"
endif
endif
// pressed button to return to main menu
if GetTextHitTest(menu.back_to_main_menu, pointer.x,pointer.y) = 1
SetTextColor(menu.back_to_main_menu,0,255,255,255)
if pointer.pressed = 1
menu.state = 0
endif
endif
endfunction
function _f_menu_choose_topping()
// set all sub menu text visible
SetTextVisible(menu.choose_topping_title,1)
SetTextVisible(menu.choose_topping_cheese,1)
SetTextVisible(menu.choose_topping_bacon,1)
SetTextVisible(menu.choose_topping_bacon_cheese,1)
SetTextVisible(menu.choose_topping_none,1)
SetTextVisible(menu.back_to_main_menu,1)
// set all sub menu text to yellow
SetTextColor(menu.choose_topping_cheese,255,255,0,255)
SetTextColor(menu.choose_topping_bacon,255,255,0,255)
SetTextColor(menu.choose_topping_bacon_cheese,255,255,0,255)
SetTextColor(menu.choose_topping_none,255,255,0,255)
SetTextColor(menu.back_to_main_menu,255,255,0,255)
// if selection has been made then set to green
select ingredient.topping$
case "cheese"
SetTextColor(menu.choose_topping_cheese,0,255,0,255)
endcase
case "bacon"
SetTextColor(menu.choose_topping_bacon,0,255,0,255)
endcase
case "bacon and cheese"
SetTextColor(menu.choose_topping_bacon_cheese,0,255,0,255)
endcase
case "no topping"
SetTextColor(menu.choose_topping_none,0,255,0,255)
endcase
endselect
// select an option (highlight cyan when pointer is over the text then set variable if pointer is pressed
if GetTextHitTest(menu.choose_topping_cheese, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_topping_cheese,0,255,255,255)
if pointer.pressed = 1
ingredient.topping$ = "cheese"
endif
endif
if GetTextHitTest(menu.choose_topping_bacon, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_topping_bacon,0,255,255,255)
if pointer.pressed = 1
ingredient.topping$ = "bacon"
endif
endif
if GetTextHitTest(menu.choose_topping_bacon_cheese, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_topping_bacon_cheese,0,255,255,255)
if pointer.pressed = 1
ingredient.topping$ = "bacon and cheese"
endif
endif
if GetTextHitTest(menu.choose_topping_none, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_topping_none,0,255,255,255)
if pointer.pressed = 1
ingredient.topping$ = "no topping"
endif
endif
// pressed button to return to main menu
if GetTextHitTest(menu.back_to_main_menu, pointer.x,pointer.y) = 1
SetTextColor(menu.back_to_main_menu,0,255,255,255)
if pointer.pressed = 1
menu.state = 0
endif
endif
endfunction
function _f_menu_choose_condiments()
// set all sub menu text visible
SetTextVisible(menu.choose_condiment_title,1)
SetTextVisible(menu.choose_condiment_ketchup,1)
SetTextVisible(menu.choose_condiment_mayo,1)
SetTextVisible(menu.choose_condiment_relish,1)
SetTextVisible(menu.choose_condiment_none,1)
SetTextVisible(menu.back_to_main_menu,1)
// set all sub menu text to yellow
SetTextColor(menu.choose_condiment_ketchup,255,255,0,255)
SetTextColor(menu.choose_condiment_mayo,255,255,0,255)
SetTextColor(menu.choose_condiment_relish,255,255,0,255)
SetTextColor(menu.choose_condiment_none,255,255,0,255)
SetTextColor(menu.back_to_main_menu,255,255,0,255)
// if selection has been made then highlight in green
select ingredient.condiment$
case "ketchup"
SetTextColor(menu.choose_condiment_ketchup,0,255,0,255)
endcase
case "mayo"
SetTextColor(menu.choose_condiment_mayo,0,255,0,255)
endcase
case "relish"
SetTextColor(menu.choose_condiment_relish,0,255,0,255)
endcase
case "no condiment"
SetTextColor(menu.choose_condiment_none,0,255,0,255)
endcase
endselect
// select an option (highlight green when pointer is over the text then set variable if pointer is pressed
if GetTextHitTest(menu.choose_condiment_ketchup, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_condiment_ketchup,0,255,255,255)
if pointer.pressed = 1
ingredient.condiment$ = "ketchup"
endif
endif
if GetTextHitTest(menu.choose_condiment_mayo, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_condiment_mayo,0,255,255,255)
if pointer.pressed = 1
ingredient.condiment$ = "mayo"
endif
endif
if GetTextHitTest(menu.choose_condiment_relish, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_condiment_relish,0,255,255,255)
if pointer.pressed = 1
ingredient.condiment$ = "relish"
endif
endif
if GetTextHitTest(menu.choose_condiment_none, pointer.x,pointer.y) = 1
SetTextColor(menu.choose_condiment_none,0,255,255,255)
if pointer.pressed = 1
ingredient.condiment$ = "no condiment"
endif
endif
// pressed button to return to main menu
if GetTextHitTest(menu.back_to_main_menu, pointer.x,pointer.y) = 1
SetTextColor(menu.back_to_main_menu,0,255,255,255)
if pointer.pressed = 1
menu.state = 0
endif
endif
endfunction
This example uses text instead of sprites and I would not advocate using such a long winded way of creating the various menus. I'd normally use multi-dimension arrays to make it easier to cycle through the various buttons but I thought this would be easier to follow and seemed more in keeping with the way you're already doing things. At this stage I thought it'd be more important to understand the logic.
I did try Crazy's code but it didn't work and, yes, I really did create this example whilst out having a bacon and cheese burger.