You need a flag that prevents it from detecting the spacekey multiple times before it's released. Whenever I tried to go back to a previous menu, it'd just clicked right back.
A large problem with your system is that it's not very flexible. You'd have to make another IF block every time you wanted to add a new menu or submenu.
I wrote this up for you. It's not intended to show up your code, but hopefully demonstrate another, more flexible, method for a similar type of menu system by using arrays. It might look like a lot of complex code at first but it really isn't.
REM ********************************************
REM Title: Menu System
REM Author: Phaelax
REM Created: Tuesday, October 30, 2012
REM ********************************************
Type MenuItem
parentId as integer
name as string
isSelected as integer
EndType
dim menuItems() as MenuItem
Global currentMenu = 0
Global lastMenu = 0
rootId = insertMenuItem("root", -1)
insertMenuItem("New", rootId)
insertMenuItem("Open", rootId)
insertMenuItem("Save", rootId)
settingsId = insertMenuItem("Settings", rootId)
insertMenuItem("Exit", rootId)
insertMenuItem("Customize", settingsId)
insertMenuItem("Options", settingsId)
insertMenuItem("Plugins", settingsId)
currentMenu = rootId
selectFirstItem(currentMenu)
selectedItem = -1
do
cls
if upkey() = 1 and vkFlag = 0
vkFlag = 1
selectPrevious(currentMenu)
endif
if downkey() = 1 and vkFlag = 0
vkFlag = 1
selectNext(currentMenu)
endif
if rightkey() and vkFlag = 0
currentMenu = openSubMenu(currentMenu)
vkFlag = 1
endif
if leftkey() and vkFlag = 0
vkFlag = 1
currentMenu = openParentMenu(currentMenu)
endif
if returnkey() and vkFlag = 0
vkFlag = 1
selectedItem = getSelectedItem(currentMenu)
endif
rem reset key flag
if upkey() + downkey() + rightkey() + leftkey() + returnkey() = 0 then vkFlag = 0
rem display menu
displayMenu(currentMenu, 100, 120)
print "'",getMenuItemName$(selectedItem), "' is currently active."
loop
REM ***********************************************
REM Returns the name of the menu item
REM ***********************************************
function getMenuItemName$(menuItemId)
if menuItemId > -1
s$ = menuItems(menuItemId).name
else
s$ = "Invalid Menu Item ID"
endif
endfunction s$
REM ***********************************************
REM Go up one level in the menu hierarchy
REM ***********************************************
function openParentMenu(menuItemId)
pid = getParentId(menuItemId)
if pid = -1
pid = menuItemId
else
id = getSelectedItem(menuItemId)
if id > -1 then menuItems(id).isSelected = 0
endif
endfunction pid
REM ***********************************************
REM Go one level deeper in the menu hierarchy
REM ***********************************************
function openSubMenu(menuItemId)
count = getChildCount(getSelectedItem(menuItemId))
if count > 0
lastMenu = menuItemId
menuItemId = getSelectedItem(menuItemId)
selectFirstItem(menuItemId)
endif
endfunction menuItemId
REM ***********************************************
REM Return the menu item ID to which this item
REM belongs to
REM ***********************************************
function getParentId(menuItemId)
pid = menuItems(menuItemId).parentId
endfunction pid
REM ***********************************************
REM Returns number of menu items that belong to
REM this one
REM ***********************************************
function getChildCount(menuItemId)
c = 0
for i = 0 to array count(menuItems())
if menuItems(i).parentId = menuItemId then inc c
next i
endfunction c
REM ***********************************************
REM Selects/highlights first item in the current
REM menu
REM ***********************************************
function selectFirstItem(menuItemId)
for i = 0 to array count(menuItems())
if menuItems(i).parentId = menuItemId then menuItems(i).isSelected = 1 : exitfunction
next i
endfunction
REM ***********************************************
REM Selects/highlights the previous menu item. If
REM the currently selected item is the first one,
REM if will wrap around to the end.
REM ***********************************************
function selectPrevious(menuItemId)
if getChildCount(menuItemId) = 0 then exitfunction
previousId = -1
currentlySelected = -1
for i = 0 to array count(menuItems())
if menuItems(i).parentId = menuItemId
if menuItems(i).isSelected = 0
previousId = i
else
if previousId > -1
menuItems(i).isSelected = 0
menuItems(previousId).isSelected = 1
exitfunction previousId
endif
currentlySelected = i
endif
endif
next i
if previousId > -1
menuItems(previousId).isSelected = 1
menuItems(currentlySelected).isSelected = 0
endif
endfunction previousId
REM ***********************************************
REM Selects/highlights the next menu item. If the
REM currently selected item is the last one, it
REM will wrap around to the beginning.
REM ***********************************************
function selectNext(menuItemId)
if getChildCount(menuItemId) = 0 then exitfunction
firstItemId = -1
currentlySelected = -1
newSelectedId = -1
for i = 0 to array count(menuItems())
if menuItems(i).parentId = menuItemId
if firstItemId = -1 then firstItemId = i
if currentlySelected > -1
newSelectedId = i
menuItems(currentlySelected).isSelected = 0
menuItems(newSelectedId).isSelected = 1
exitfunction newSelectedId
endif
if menuItems(i).isSelected then currentlySelected = i
endif
next i
if newSelectedId = -1
menuItems(currentlySelected).isSelected = 0
menuItems(firstItemId).isSelected = 1
endif
endfunction firstItemId
REM ***********************************************
REM Returns the menu item id of the currently
REM selected menu item
REM ***********************************************
function getSelectedItem(menuItemId)
for i = 0 to array count(menuItems())
if menuItems(i).parentId = menuItemId
if menuItems(i).isSelected then exitfunction i
endif
next i
endfunction -1
REM ***********************************************
REM Draws the menu, one level deep at a time.
REM
REM Improvements:
REM Ideally, the system should keep track to number
REM of children upon insertion and not have to
REM recalculate that number each time it is needed.
REM On a large, extensively complex menu array,
REM this could cause a performance hit and is quite
REM inefficient.
REM ***********************************************
function displayMenu(menuItemId, x, y)
c = 0
for i = 0 to array count(menuItems())
if menuItems(i).parentId = menuItemId
if menuItems(i).isSelected
ink rgb(255,255,0),0
else
ink rgb(255,255,255),0
endif
text x, y+c*18, menuItems(i).name
if getChildCount(i) > 0 then text x+text width(menuItems(i).name), y+c*18, " >"
inc c
endif
next i
`ink rgb(255,255,0),0
`if c = 0 then text x, y, "<empty list>"
endfunction
REM ***********************************************
REM Add menu items into the menu system
REM 'name' menu item name (this is what gets displayed)
REM 'parentId' what
REM ***********************************************
function insertMenuItem(name as string, parentId as integer)
array insert at bottom menuItems()
n = array count(menuItems())
menuItems(n).name = name
menuItems(n).parentId = parentId
endfunction n
"You're not going crazy. You're going sane in a crazy world!" ~Tick