Page 1 of 1

Can't use functions properly

Posted: Fri Aug 22, 2014 4:13 pm
by Raxe88
I am trying to make a basic menu using functions and I sure I am using them wrong, mostly because it does not work. I have no idea how to google this so I need your help!

This is the code:

Code: Select all

function love.load()
	carregarButons();
end

function love.draw()
	imprimirButons()
end

function love.update()
	comprobarRatoliPos();
end

function carregarButons()
	butonsImg = { 
		love.graphics.newImage("/Images/mainMenu/botoJugar/V2/botoJugar.png"), love.graphics.newImage("/Images/mainMenu/botoJugar/V2/botoJugar2.png"), 100, 200 -- buto Jugar
	};
	
	estatButoJugarActual = 1;
end

function imprimirButons()
	if estatButoJugarActual == 1 then
		love.graphics.newImage("Images/mainMenu/botoJugar/V2/botoJugar2.png");
		love.graphics.draw( butonsImg[2], 100, 200 );
	else
		b = love.graphics.newImage("Images/mainMenu/botoJugar/V2/botoJugar.png");
		love.graphics.draw( b, 100, 200 );
	end
end

function comprobarRatoliPos()
	mouseX, mouseY = love.mouse.getPosition();
	if mouseX > 100 and mouseX < 350 and mouseY > 200 and mouseY < 270 then
		estatButoJugarActual = 2;
	else
		estatButoJugarActual = 1;
	end
end
As you can see, I tried various things in the imprimirButons() function. The first love.graphics.draw() does not work while the second does but I want the first one to work.

Image

Any help will be appreciated!

Re: Can't use functions properly

Posted: Fri Aug 22, 2014 4:24 pm
by Plu
The "love.graphics.newImage("Images/mainMenu/botoJugar/V2/botoJugar2.png");" on line 23 doesn't do anything, you should remove it. It constantly loads a file from disk and then discards it, which is bad for your overal performance.

Other than that I'm not really sure what is going wrong. If I load this code, then it works just fine? At least; it's not showing any errors.

(Also, if you want help from the international community it helps to put your variables and functions in english, although I don't know if you english is good enough for it. While I can figure out what this is supposed to do, if it gets more complex it'll be pretty much impossible for me and other non Spanish? speakers to understand.)

Re: Can't use functions properly

Posted: Fri Aug 22, 2014 4:58 pm
by Raxe88
I actually didnt even wrote in a very known lenguage, it's Catalan. I'll edit this post and see if I can give you more information.

Ok, so I am changing the functions to english.

Code: Select all

function love.load()
	loadButtons();
end

function love.update()
	checkMousePos();
end

function loadButtons()
	buttonsImg = { 
		love.graphics.newImage("/Images/mainMenu/playButton/V2/playButton.png"), love.graphics.newImage("/Images/mainMenu/playButton/V2/playButton2.png"), 100, 200 -- buto Jugar
	};
	
	playButtonActualState= 1;
end

function drawButtons()
	if playButtonActualState== 1 then
		love.graphics.draw( butonsImg[2], 100, 200 );
	else
		b = love.graphics.newImage("Images/mainMenu/playButton/V2/playButton.png");
		love.graphics.draw( b, 100, 200 );
	end
end

function checkMousePos()
	mouseX, mouseY = love.mouse.getPosition();
	if mouseX > 100 and mouseX < 350 and mouseY > 200 and mouseY < 270 then
		playButtonActualState = 2;
	else
		playButtonActualState = 1;
	end
end
The error I get is the same. Something that I find revelant is that I am using main.lua and menu.lua. The main.lua is this:

Code: Select all

require("menu")

function love.load()

end

function love.draw()
	drawButtons()
end
The error is the same as the post above.

Edit: I just fixed it by removing love.load() from the menu.lua and using loadButtons() in the main.lua's love.load().

Re: Can't use functions properly

Posted: Fri Aug 22, 2014 9:32 pm
by DaedalusYoung
You're overwriting love.load, so the loadButtons function is never called and buttonsImg is never initialized.