Page 1 of 1

Problem dealing with changing a variable

Posted: Mon Jan 20, 2014 5:35 pm
by fiskero94
Hello, I'm creating a menu and I have a difficulty button that is supposed to change from easy to medium, medium to hard and hard to easy when pressed. However nothing seems to happen, and I don't get an error either.

Here is the code related to the difficulty option.

Code: Select all

state = mainmenu
mode = "Easy"

function love.update()

	if state == mainmenu then
		if love.mouse.isDown("l") then
			local x, y = love.mouse.getPosition()
			if x >= 50 and x <= 175 and y >= 151 and y <= 175 and mode == "Easy" then -- Difficulty Easy Button Position
				mode = "Medium"
			end
			if x >= 50 and x <= 175 and y >= 151 and y <= 175 and mode == "Medium" then -- Difficulty Medium Button Position
				mode = "Hard"
			end
			if x >= 50 and x <= 175 and y >= 151 and y <= 175 and mode == "Hard" then -- Difficulty Hard Button Position
				mode = "Easy"
			end
		end
	end
	
end

function love.draw()

	if state == mainmenu then
		love.graphics.print("Difficulty:", 50, 150)
		if mode == "Easy" then
			love.graphics.print("Easy", 135, 150)
		end
		if mode == "Medium" then
			love.graphics.print("Medium", 135, 150)
		end
		if mode == "Hard" then
			love.graphics.print("Hard", 135, 150)
		end
	end

end
I've attached the full code of the menu to this post.

Thanks :)

Re: Problem dealing with changing a variable

Posted: Mon Jan 20, 2014 5:52 pm
by veethree
For the future, If you could include the whole source it'd be helpful. I had to go through your code and comment out any lines that have to do with images and fonts.

First, You want to move the button press related code out of love.update, The code on each button press will be executed each frame as long as the mouse is down and on the button, usually you don't want that. It'd be best to move the code to love.mousepressed()

2nd, Instead checking of the mouse is inside the button 3 times, You can just do it once, And then figure out what mode should be next
like so:

Code: Select all

if x >= 50 and x <= 175 and y >= 151 and y <= 175 then -- Difficulty Easy Button Position
				if mode == "Easy" then
					mode = "Medium"
				elseif mode == "Medium" then
					mode = "Hard"
				elseif mode == "Hard" then
					mode = "Easy"
				end
			end
and third, I recommend you read up on tables, Your code could be greatly simplified, And cleaned if you stored the buttons in a table.

Re: Problem dealing with changing a variable

Posted: Mon Jan 20, 2014 6:29 pm
by fiskero94
Thank you for the response, the menu works now.

Here is the full source