Scene changing issue
I am working on a menu page for my game, pressing A prints everything fine and unloads the current scene but then the screen just stays black instead of loading the scene associated with the global variable "drawBackground"
Menu buttons:
update = function()
if global.drawLobby == true then
if keyboard.A then
print("Start game!")
sleep 0.1 seconds
global.drawLobby = false
sleep 1 seconds
global.drawBackground = true
sleep 0.1 seconds
end
if keyboard.B then
print("Load saved game!")
end
if keyboard.C then
print("Load credits!")
end
end
end
Sorry if this is a really stupid error, this is my first time making a game in Microstudio.
update = function()
if keyboard.A then
if drawLobby == true then
print("Play the game")
sleep 0.1 seconds
drawBackground = true
drawLobby = false
end
end
here is a more specific snipped of just the button that is broken
from: Abr00
heute um 03:47 Uhr
sleep 0.1 seconds will pause the entire function for 0.1 seconds. During that time, it will not be able to react to inputs or even do anything. An alternative would be:
update = function()
if keyboard.A then
if drawLobby == true then
print("Play the game")
after 0.1 seconds do // <----<<<
drawLobby = false
drawBackground = true
LobbyMusic.stop()
end
end
end
this will put the code for turning off the draw lobby function in a seperate thread which will run after 0.1 of a second. Meanwhile the update function will continue as normal
the update function is normally called once per frame, but the sleep function will delay that call to when the current iteration of the function finishes
You probably shouldn't use sleep in the update() function.
This function is executed 60 times per second and should last as short as possible and not be stopped for 1 second.
Make the code public in the project settings and provide a link to the code .