It's probably working right but toggling the menu on/off 40+ times from pressing ESC once. To stop that from happening with your switch you can do it in 3 ways.
1. Stop all action till the user lets go of ESC button.
disable escapekey
do
cls
` Show current state of menu
if UI_MENU_INGAME_ACTIVE = 1
text 0,0,"Menu Showing"
else
text 0,0,"Menu Not Showing"
endif
` Check for ESC key being pressed
if escapekey() = 1
` Check if the menu is off
if UI_MENU_INGAME_ACTIVE = 0
` Turn the menu on
UI_MENU_INGAME_ACTIVE = 1
else
` Turn the menu off
UI_MENU_INGAME_ACTIVE = 0
endif
` Stop all action till the user lets go of ESC
repeat
until escapekey()=0
endif
loop
2. Use a TIMER() to prevent the switch from instantly turning off. Change the amount of time to suit your needs (holding down ESC here will toggle the switch every 500 milliseconds).
disable escapekey
` Create a timer
tim=timer()
do
cls
` Show current state of menu
if UI_MENU_INGAME_ACTIVE = 1
text 0,0,"Menu Showing"
else
text 0,0,"Menu Not Showing"
endif
` Check for ESC key being pressed and if 500 milliseconds have gone by
if escapekey() = 1 and timer()>tim+500
` Check if the menu is off
if UI_MENU_INGAME_ACTIVE = 0
` Turn the menu on
UI_MENU_INGAME_ACTIVE = 1
else
` Turn the menu off
UI_MENU_INGAME_ACTIVE = 0
endif
` Reset timer
tim=timer()
endif
loop
3. The coolest method is using the GET KEY STATE() command that is an actual toggle for many keys. Pressing ESC once will toggle it on and pressing it again will toggle it off. No need to use TIMER() or freeze the action. You can use the GET KEY STATE() of the ESC button itself as the Menu toggle instead of using a variable switch.
disable escapekey
` Make sure the ESC button toggle starts off
set key state toggle 0x1B,0
do
cls
` Show the state of ESC
text 0,0,"Current state of ESC: "+str$(get key state(0x1B))
` Show if ESC currently being pressed
if escapekey()
text 0,20,"ESC is being pressed."
endif
` Show if the ESC button is toggled on (or it's off)
if get key state(0x1B)=1
text 0,40,"ESC Toggled On (Menu Showing)"
else
text 0,40,"ESC Toggled Off (Menu Not Showing)"
endif
loop