AppGameKit Classic

AppGameKit Classic

Not enough ratings
Using buttons to create a simple menu
By thescenecommander
Based on a user request, this guide shows a basic user how to create a simple menu. It also introduces the Text object and covers the use of Select/Case statements which are very useful when creating button systems.
   
Award
Favorite
Favorited
Unfavorite
Creating a simple menu
Another guide based on a request from a user, this time we're looking at using the default virtual button system to create a simple menu. In effect the code covers how to define buttons and assign a result to clicking them.

It also introduces the Text object and Select/Case statement, which can be thought of as a more advanced and streamlined IF-THEN loop.

This sample uses no extra media, so all you need to do is cut and paste into a new project and click run. As before I would encourage you to play around with the code, possibly mixing it with elements that you've learned from some of the previous guides. The code is well documented, but please ask any questions if you're having problems.

As always, here's the video of what you should be getting.


and here's the code.

// Project: Making a simple menu
// Created: 2015-01-06

// set window properties
SetWindowTitle( "Making a simple menu" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )

// Create a new type for each of our buttons,

Type differentbuttons

x as float
y as float
id as integer

endtype

// We are creating a new kind of object for this demo known as a text object, these are similar to sprites in that they can be hidden, positioned, scaled, rotated etc
// but they only display text and have no collisions or physics abilities.

// the first number is the text object id, and the second is the default text, "" indicates that it is currently empty.
CreateText (1,"")
//Set the text to approximately the middle of the screen based on the text we intend to display
SetTextPosition(1,230,400)
//Give the text a default size
SetTextSize(1,50)
//Make sure the it isn't visible yet.
SetTextVisible(1,0)



//AppGameKit limits users to 12 buttons on screen at any one time. I will be covering using sprite to
// create artificial buttons in another tutorial where this limit could in theory be limitless, but for now, we'll just cover standard buttons
// As we're going to create a line of buttons along the top, we'll only add 5 as that's the number that fits nicely
// and users will be able to see what's going on.

// create an array using the type defined in differentbuttons.
Dim button[5] as differentbuttons

// set up buttons
for makebutton=1 to 5

button[makebutton].x=80+(makebutton-1)*200 // Set the button position across the screen, starting at 80 and moving in steps of 200
button[makebutton].y=90 // Set the button position near the top of the screen
button[makebutton].id=makebutton //Give the button a unique ID based on the makebutton loop

// Make the button itself
AddVirtualButton(button[makebutton].id,button[makebutton].x,button[makebutton].y,60.0)
// Give the button some text, this also introduces a new command STR, this allows us to add a numeric value in place of text, in this case the value of button[makebutton].id
// You could also assign an image to a button, but as this demo uses no media, we'll stick to text for the moment.
SetVirtualButtonText(button[makebutton].id,str(button[makebutton].id))
//Make sure the button is visible, this is the default, but I've set it to demonstrate the commands.
SetVirtualButtonVisible(button[makebutton].id,1)

next makebutton

// main loop
do

// loop through all available buttons
For testbuttons=1 to 5

// get the state of the next button to be tested and assign it to the variable buttondown once the button has been click it will report as clicked
// until the mouse button is released
buttondown=GetVirtualButtonState(testbuttons)
//GetVirtualButtonPressed(testbuttons)// try this setting to record just a single click per loop


// if buttondown <> 0 then the button is being clicked
if buttondown<>0
// we're now going to look at another command that can be used for checking a single value and producing a different result based on the value.
// You can think of this as an extention of the IF-THEN command structure

// Select command take the value contained in testbuttons, we know that this is the value of the button being pressed as we
// are testing each button in turn

select testbuttons

// does the value of testbuttons=1, if so, exectute the commands between case 1 and the following endcase
// in all cases for this demo we will just change the text, but you could just as easily have very different
// code in each case.
case 1
SetTextString(1,"Last Button pushed 1")
endcase

// does the value of testbuttons=2, if so, exectute the commands between case 2 and the following endcase
case 2
SetTextString(1,"Last Button pushed 2")
endcase

// does the value of testbuttons=3, if so, exectute the commands between case 3 and the following endcase
case 3
SetTextString(1,"Last Button pushed 3")
endcase

// does the value of testbuttons=4, if so, exectute the commands between case 4 and the following endcase
case 4
SetTextString(1,"Last Button pushed 4")
endcase

// does the value of testbuttons=5, if so, exectute the commands between case 5 and the following endcase
case 5
SetTextString(1,"Last Button pushed 5")
endcase

// end the select-case loop
endselect

// if the text string isn't empty, display it.
if GetTextString(1)<>""
SetTextVisible(1,1)
endif

// end check
endif
//find next button to test
next testbuttons

// update screen.
sync()

// loop forever.
loop
1 Comments
파란색짜장면 1 Jul, 2016 @ 4:38pm 
ㅅㅅ