Posted: 24th Aug 2011 12:13
I've been playing around with AppGameKit and thought I'd see how easy it was to write a simple menu system for a game.
30 minutes later, here it is.
Obviously it would require the buttons placing where you want and images, etc... but it should be a good start for people.
+ Code Snippet
SetVirtualResolution ( 480, 320 )
SetOrientationAllowed ( 0, 0, 1, 1 )

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
 gosub Menus
 Sync()
loop

Function Menus()
if ButtonTidy = 0
    if GetVirtualButtonExists(1) = 1 then DeleteVirtualButton(1)
    if GetVirtualButtonExists(2) = 1 then DeleteVirtualButton(2)
    if GetVirtualButtonExists(3) = 1 then DeleteVirtualButton(3)
    if GetVirtualButtonExists(4) = 1 then DeleteVirtualButton(4)
    if GetVirtualButtonExists(5) = 1 then DeleteVirtualButton(5)
    if GetVirtualButtonExists(6) = 1 then DeleteVirtualButton(6)
    if GetVirtualButtonExists(7) = 1 then DeleteVirtualButton(7)
    if GetVirtualButtonExists(8) = 1 then DeleteVirtualButton(8)
    if GetVirtualButtonExists(9) = 1 then DeleteVirtualButton(9)
    if GetVirtualButtonExists(10) = 1 then DeleteVirtualButton(10)
    if GetVirtualButtonExists(11) = 1 then DeleteVirtualButton(11)
    if GetVirtualButtonExists(12) = 1 then DeleteVirtualButton(12)
    ButtonTidy = 1
endif
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 GetVirtualButtonReleased(1)
            gameState = 3
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(2)
            gameState = 4
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(3)
            gameState = 5
            ButtonTidy = 0
        endif
    EndCase
    Case 3: // How to Play
        if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
        SetVirtualButtonText(1,"Main Menu") // Main Game
        if GetVirtualButtonReleased(1)
            gameState = 2
            ButtonTidy = 0
        endif
    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 GetVirtualButtonReleased(1)
            gameState = 5
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(2)
            gameState = 2
            ButtonTidy = 0
        endif
    EndCase
    Case 5: // Main Game
        if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,400,40,40)
        SetVirtualButtonText(1,"||") // Paused
        if GetVirtualButtonReleased(1)
            gameState = 6
            ButtonTidy = 0
        endif
    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 GetVirtualButtonReleased(1)
            gameState = 5
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(2)
            gameState = 2
            ButtonTidy = 0
        endif
    EndCase
    Case 7: // Level Failed
    EndCase
    Case 8: // Level Complete
    EndCase
    Case 9: // Game Over
    EndCase
EndSelect
EndFunction


I am only just starting with this language, so there may be a lot better way of doing things that I haven't come across yet.
DIJ
Posted: 24th Aug 2011 14:13
Looks OK. I think the only thing I'd do is change the ButtonTidy code to a loop (less typing), and also move it into a seperate function so you can call it from other places if you need to.

I can't see the point in mixing functions and gosubs either...

Updated Menu code.
+ Code Snippet
SetVirtualResolution ( 480, 320 )
SetOrientationAllowed ( 0, 0, 1, 1 )

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

global ButtonTidy = 0

do
    Menus()
    Sync()
loop

Function deleteButtons()
    for i =1 to 12
        if GetVirtualButtonExists(i) = 1 then DeleteVirtualButton(i)
    next i
    ButtonTidy = 1
EndFunction

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 GetVirtualButtonReleased(1)
            gameState = 3
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(2)
            gameState = 4
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(3)
            gameState = 5
            ButtonTidy = 0
        endif
    EndCase
    Case 3: // How to Play
        if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,100,100,40)
        SetVirtualButtonText(1,"Main Menu") // Main Game
        if GetVirtualButtonReleased(1)
            gameState = 2
            ButtonTidy = 0
        endif
    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 GetVirtualButtonReleased(1)
            gameState = 5
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(2)
            gameState = 2
            ButtonTidy = 0
        endif
    EndCase
    Case 5: // Main Game
        if GetVirtualButtonExists(1) = 0 then AddVirtualButton(1,400,40,40)
        SetVirtualButtonText(1,"||") // Paused
        if GetVirtualButtonReleased(1)
            gameState = 6
            ButtonTidy = 0
        endif
    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 GetVirtualButtonReleased(1)
            gameState = 5
            ButtonTidy = 0
        endif
        if GetVirtualButtonReleased(2)
            gameState = 2
            ButtonTidy = 0
        endif
    EndCase
    Case 7: // Level Failed
    EndCase
    Case 8: // Level Complete
    EndCase
    Case 9: // Game Over
    EndCase
EndSelect
EndFunction


One nice thing to have (but I don't think AppGameKit currently supports it) would be to detect when the mouse is over the button.

You could also it to detect if the mouse button was released whilst it was still over the button. Currently clicking on a button, dragging the mouse over a different button and then releasing still activates the first button. There's no way to change your mind if you have clicked on an incorrect button.

Something like:
+ Code Snippet
if GetPointerX() >= GetVirtualButtonX(buttonID) and GetPointerX() <= GetVirtualButtonX(buttonID) + GetVirtualButtonWidth(ID) and GetPointerY >= GetVirtualButtonY(buttonID) and GetPointerY <= GetVirtualButtonY + GetVirtualButtonHeight(buttonID)

// Do some stuff like add a glow around the hovered button

endif
Posted: 24th Aug 2011 14:56
Thanks for your input, I'm still a newbie with AppGameKit but learning more all the time.
I wasn't too fussed about detecting if a button had a mouse over as this is mainly for iOS games, but that would be good for porting to windows.
I hadn't considered that a user could press a button, move their finger and release and it would still be counted as a press, thanks for highlighting that for me.
Posted: 24th Aug 2011 15:23
Here is a re-worked version, that also makes sure the player is still clicking the button when released.

+ Code Snippet
SetVirtualResolution ( 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