Can't use functions properly

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
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Can't use functions properly

Post 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!
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Can't use functions properly

Post 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.)
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: Can't use functions properly

Post 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().
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Can't use functions properly

Post by DaedalusYoung »

You're overwriting love.load, so the loadButtons function is never called and buttonsImg is never initialized.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests