Problem dealing with changing a variable

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
fiskero94
Prole
Posts: 4
Joined: Sat Jan 18, 2014 8:17 pm
Location: Denmark

Problem dealing with changing a variable

Post 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 :)
Attachments
main.lua
(2.96 KiB) Downloaded 69 times
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Problem dealing with changing a variable

Post 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.
User avatar
fiskero94
Prole
Posts: 4
Joined: Sat Jan 18, 2014 8:17 pm
Location: Denmark

Re: Problem dealing with changing a variable

Post by fiskero94 »

Thank you for the response, the menu works now.

Here is the full source
Attachments
snake.zip
(668.58 KiB) Downloaded 59 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests