I am trying to help myself understand the general concept of coding game menus and I have come to a few roadblocks. What I want to do is go into the options and change the volume of the music that is currently playing. I sort of have an idea as to how I am suppose to solve my problem, and I am guessing of multiple ways that this can be accomplished, such as.....
A- I should have a function called changeVolume which will return a value to set the current volume of the music that is playing.
B- Just do a regular userInput to change the value of the volume to up or down.
But if option b is the case, do I just make a variable called chnge_volume so I can use the up and down arrow key to only affect the value of the volume by placing this in its own gamestate? Or are my options A and B completely wrong alltogether?
And one more thing, I am having the feeling that I can accomplish what I have so far with much less code,mainly using a for loop somewhere.
Any suggestions on these issues I am having would be much appreciated.
Code: Select all
function load()
font = love.graphics.newFont(love.default_font, 20)
love.graphics.setFont(font)
img_menu = love.graphics.newImage("game menu.jpg")
music_menu = love.audio.newMusic("memory alloys.ogg")
love.audio.play(music_menu, 0)
sound_ping = love.audio.newSound("beep ping.ogg")
menu = {}
menu[1] = "Start"
menu[2] = "Options"
menu[3] = "High Scores"
menu[4] = "Cheats"
menu[5] = "Exit"
state = "menu"
current = 1
previous = current - 1
end
function update(dt)
end
function draw()
if state == "menu" then
showMenu()
end
if state == "start" then
start()
end
if state == "options" then
options()
end
if state == "high scores" then
highScores()
end
if state == "cheats" then
cheats()
end
if state == "exit" then
exit()
end
end
---------------------------------User Functions---------------------
-- These make it so when the variable "current" changes
-- so does the current menu selection
function keypressed(key)
if key == love.key_up then
current = current - 1
love.audio.play(sound_ping)
end
if key == love.key_down then
current = current + 1
love.audio.play(sound_ping)
end
if key == love.key_q and state == "options" or "high scores" or "cheats" or "exit" then
state = "menu"
end
--This takes us to our given location in the games menu
if key == love.key_return and current == 1 then
state = "start"
end
if key == love.key_return and current == 2 then
state = "options"
end
if key == love.key_return and current == 3 then
state = "high scores"
end
if key == love.key_return and current == 4 then
state = "cheats"
end
if key == love.key_return and current == 5 then
state = "exit"
end
end
function showMenu()
if current == 1 then
love.graphics.draw(menu[1], 340,100)
else love.graphics.draw(menu[1], 240,100)
end
if current == 2 then
love.graphics.draw(menu[2], 340,150)
else love.graphics.draw(menu[2], 240,150)
end
if current == 3 then
love.graphics.draw(menu[3], 340,200)
else love.graphics.draw(menu[3], 240,200)
end
if current == 4 then
love.graphics.draw(menu[4], 340,250)
else love.graphics.draw(menu[4], 240,250)
end
if current == 5 then
love.graphics.draw(menu[5], 340,300)
else love.graphics.draw(menu[5], 240,300)
end
--Makes it so the selection goes from the bottom to top
if current == 6 then
current = 1
end
if current == 0 then
current = 5
end
end
function start()
love.graphics.draw(menu[1], 300,300)
end
function options()
love.graphics.draw(menu[2], 300,300)
love.graphics.draw("setvolume: ", 300,350)
love.graphics.draw("set difficulty: ", 300,400)
end
function highScores()
love.graphics.draw(menu[3], 300,300)
end
function cheats()
love.graphics.draw(menu[4], 300,300)
end
function exit()
love.graphics.draw(menu[5], 300,300)
end