I started working with virtual buttons while I build out my game. I like it because I'm not good with graphics and I can quickly place a button and interact with it. I preload all my buttons and leave them all inactive and invisible state until I'm ready to use them. I have a script that gets called when the user wants to display the "Instructions" for the game. It just pages back and forth between images. When the script runs, it activates and makes them visible. I then loop and check for the user to press the back, forward or exit buttons. Here is my code:
bPrevious = 2
bNext = 3
// Instructions - Create previous button
AddVirtualButton(bPrevious, 200, 1900, 100)
SetVirtualButtonText(bPrevious, "Previous")
SetVirtualButtonSize(bPrevious, 400, 200)
SetVirtualButtonVisible(bPrevious, 0)
SetVirtualButtonActive(bPrevious, 0)
// Instructions - Create next button
AddVirtualButton(bNext, 800, 1900, 100)
SetVirtualButtonText(bNext, "Next")
SetVirtualButtonSize(bNext, 400, 200)
SetVirtualButtonVisible(bNext, 0)
SetVirtualButtonActive(bNext, 0)
cPage = 1 // Current page
mPage = 10 // Max number of pages
// Enable next button
SetVirtualButtonVisible(bNext, 1)
SetVirtualButtonActive(bNext, 1)
// I got these numbers from my phone. I'm building everyting off this size.
screenWidth = 1080
screenHeight = 2016
// Set up the size so the screen matches the mobile phone.
SetWindowSize(screenWidth, screenHeight, 0)
SetVirtualResolution(screenWidth, screenHeight)
//*** Clear the screen ***
ClearScreen()
do
Print (cPage)
// Check if user pressed Previous button
If GetVirtualButtonPressed(bPrevious)
if cPage = mPage
SetVirtualButtonActive(bNext, 1)
SetVirtualButtonVisible(bNext, 1)
endif
cPage = cPage - 1
If cPage = 1
SetVirtualButtonActive(bPrevious, 0)
SetVirtualButtonVisible(bPrevious, 0)
EndIf
print ("Previous")
EndIf
// Check if user pressed Next button
If GetVirtualButtonPressed(bNext)
if cPage = 1
SetVirtualButtonActive(bPrevious, 1)
SetVirtualButtonVisible(bPrevious, 1)
endif
cPage = cPage + 1
If cPage = mPage
SetVirtualButtonActive(bNext, 0)
SetVirtualButtonVisible(bNext, 0)
EndIf
print ("Next")
EndIf
Sync()
loop
SetVirtualButtonVisible(bNext, 0)
SetVirtualButtonActive(bNext, 0)
SetVirtualButtonVisible(bPrevious, 0)
SetVirtualButtonActive(bPrevious, 0)
For the most part, it works. When I first load, the "previous" button is not activated. As I page through I check if I am at the first page or last and activate/deactivate buttons to prevent the user from going past the page boundaries. The problem starts when I reach page 1 or page 10. It seems to get stuck on that page. If I reach the past page and click "Previous", it flickers and stays on that page and the "Next" button disappears. It's as if the "Next" button is still pressed. I say it "flickers" because it looks like it first goes to the previous page and then goes back to the next page, essentially staying on that last page. I'm wondering if I'm "deactvating" the button before it completes the full action of "down" and "up". Maybe it's stuck "down". Is there a way to clear the action on a virtual button?