Is my approach for keypressed menu suitable.
Posted: Tue Dec 03, 2024 6:17 am
I'm new to programming and English is not my first language, please don't mind my words. The code is not exact like the code i used.
I decided is to use number (1, 2, 3 ... 9) to access the menu. For example, 1 for start game, 2 for settings, 3 for credits, 4 for quit game and escape to back up one level.
Using the modified example from love wiki.
However, the problem happen when i have sub menu like in 'settings' that use the number to access item the same way as the main menu. When i press "2" from main menu to access 'settings', and in the 'settings' also use "2" to access 'audio' menu. The scene instantly go to 'audio' menu instead of stop at the level of 'settings' sub menu.
So, i think that the keypressed is still active when the scene change from 'mainMenu' to 'settings', the scene 'settings' then change to 'audio' because the key "2" is also still pressed in 'settings' scene.
At first i use delay counter so the scene not change instantly but if the delay is too low, it's not working. If the delay is too long it made the menu lagging and not responsive. So i decide to use a flag to check if the scene is ready to change (the number key need to be released once before the scene can change again). To prevent my confusing i use global variable that can access anywhere.
The code is for this post and not the same as in my project (there are a lot of iteration in my project). It is working but i'm not sure if it is suitable for the game programming. Everyting is new to me. Thank you in advance from amateur developer.
I decided is to use number (1, 2, 3 ... 9) to access the menu. For example, 1 for start game, 2 for settings, 3 for credits, 4 for quit game and escape to back up one level.
Using the modified example from love wiki.
Code: Select all
scene = {}
scene.current = "mainMenu"
function love.keypressed( key, scancode, isrepeat )
-- pressed key on "mainMenu" scene to change scene
if scene.current == "mainMenu" then
if scancode == "1" then
scene.current = "newGame"
elseif scancode == "2" then
scene.current = "settings"
end
end
end
function love.draw()
-- draw current scene
scene.current.draw()
end
So, i think that the keypressed is still active when the scene change from 'mainMenu' to 'settings', the scene 'settings' then change to 'audio' because the key "2" is also still pressed in 'settings' scene.
At first i use delay counter so the scene not change instantly but if the delay is too low, it's not working. If the delay is too long it made the menu lagging and not responsive. So i decide to use a flag to check if the scene is ready to change (the number key need to be released once before the scene can change again). To prevent my confusing i use global variable that can access anywhere.
Code: Select all
scene = {}
scene.current = "mainMenu"
scene.keypressed = nil
scene.readyToChange = true
function love.keypressed( key, scancode, isrepeat )
-- pressed key on "mainMenu" scene to change scene
if scene.current == "mainMenu" and scene.readyToChange then
if scancode == "1" then
scene.readyToChange = false -- flag to prevent the scene to change immediately.
scene.keypressed = "1"
scene.current = "newGame"
elseif scancode == "2" then
scene.readyToChange = false
scene.keypressed = "2"
scene.current = "settings"
end
elseif scene.current == "settings" and scene.readyToChange then
if scancode == "1" then
scene.readyToChange = false -- flag to prevent the scene to change immediately.
scene.current = "graphicsSettings"
elseif scancode == "2" then
scene.readyToChange = false
scene.keypressed = "2"
scene.current = "audioSettings"
end
end
end
function love.keyreleased( key, scancode )
if scene.current == "newGame" and scancode == scene.keypressed then -- released key == pressed key
scene.readyToChange = true
scene.keypressed = nil
elseif scene.current == "settings" and scancode == scene.keypressed then
scene.readyToChange = true
scene.keypressed = nil
end
end
function love.draw()
-- draw current scene
scene.current.draw()
end