Here is a re-worked version, that also makes sure the player is still clicking the button when released.
+ Code SnippetSetVirtualResolution ( 480, 320 )
SetOrientationAllowed ( 0, 0, 1, 1 )
global ButtonTidy = 0
global gameState = 1
// 1 = Splash Screen
// 2 = Main Menu
// 3 = How to Play
// 4 = Level Select
// 5 = Main Game
// 6 = Paused
// 7 = Level Failed
// 8 = Level Complete
// 9 = Game Over
do
Menus()
Sync()
loop
Function deleteButtons()
for i =1 to 12
if GetVirtualButtonExists(i) = 1 then DeleteVirtualButton(i)
next i
ButtonTidy = 1
EndFunction
Function ButtonClicked(ID,x,y,size)
Clicked = 0
mx = GetPointerX()
my = GetPointerY()
if mx >= x-(size/2) and mx <= x+(size/2) and my >= y-(size/2) and my <= y + (size/2) and GetVirtualButtonReleased(ID) = 1
Clicked = 1
ButtonTidy = 0
endif
EndFunction Clicked
Function Menus()
if ButtonTidy = 0 then deleteButtons()
Select gameState // Maximum of 12 Buttons
Case 1: // Splash Screen
// Do stuff
gameState = 2; ButtonTidy = 0
EndCase
Case 2: // Main Menu
if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
SetVirtualButtonText(1,"?") // How To Play
if GetVirtualButtonExists(2) = 0 then AddVirtualButton(2,200,100,40)
SetVirtualButtonText(2,"Level Select") // Level Select
if GetVirtualButtonExists(3) = 0 then AddVirtualButton(3,300,100,40)
SetVirtualButtonText(3,"Play") // Main Game
if ButtonClicked(1,100,100,40) = 1 then gameState = 3
if ButtonClicked(2,200,100,40) = 1 then gameState = 4
if ButtonClicked(3,300,100,40) = 1 then gameState = 5
EndCase
Case 3: // How to Play
if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
SetVirtualButtonText(1,"Main Menu") // Main Game
if ButtonClicked(1,100,100,40) = 1 then gameState = 2
EndCase
Case 4: // Level Select
if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
SetVirtualButtonText(1,"Play") // Main Game
if GetVirtualButtonExists(2) = 0 then AddVirtualButton(2,200,100,40)
SetVirtualButtonText(2,"Back") // Main Menu
if ButtonClicked(1,100,100,40) = 1 then gameState = 5
if ButtonClicked(2,200,100,40) = 1 then gameState = 2
EndCase
Case 5: // Main Game
if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,400,40,40)
SetVirtualButtonText(1,"||") // Paused
if ButtonClicked(1,400,40,40) = 1 then gameState = 6
EndCase
Case 6: // Paused
if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
SetVirtualButtonText(1,"Resume") // Main Game
if GetVirtualButtonExists(2) = 0 then AddVirtualButton(2,200,100,40)
SetVirtualButtonText(2,"Exit Game") // Main Menu
if ButtonClicked(1,100,100,40) = 1 then gameState = 5
if ButtonClicked(2,200,100,40) = 1 then gameState = 2
EndCase
Case 7: // Level Failed
if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
SetVirtualButtonText(1,"Retry") // Main Game
if GetVirtualButtonExists(2) = 0 then AddVirtualButton(2,200,100,40)
SetVirtualButtonText(2,"Main Menu") // Main Menu
if ButtonClicked(1,100,100,40) = 1 then gameState = 5
if ButtonClicked(2,200,100,40) = 1 then gameState = 2
EndCase
Case 8: // Level Complete
EndCase
Case 9: // Game Over
if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
SetVirtualButtonText(1,"Main Menu") // Main Menu
if ButtonClicked(1,100,100,40) = 1 then gameState = 2
EndCase
EndSelect
EndFunction