Page 1 of 2

Drawing a image from a for loop?

Posted: Fri Jan 17, 2014 11:34 pm
by WolfNinja2
Hey, I'm trying to use love.graphics.draw entirely with table data.

Code: Select all

for k,v in pairs(buttons) do
	g.draw(v.pic, v.x, v.y)
end
And here is the table.

Code: Select all

buttons = {
	
	["Start"] = {x = 250, y = 350, width = 100, height = 50, pic = img_btnStart, picClick = img_btnStart_click, click = false, state = 0}

}
It seems so simple but I can't figure it out haha.

Re: Drawing a image from a for loop?

Posted: Fri Jan 17, 2014 11:56 pm
by veethree
Could you post all your code? would make helping you easier.

First, In the loop you call g.draw, Isn't it supposed to be v.draw? 2nd, Is the draw method even defined?

And third, what exactly is the problem, Are you getting an error?

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 1:41 am
by WolfNinja2
No error, the g.draw is love.graphics.draw, my apologies for not specifying that. And this is the only code other than just love.draw ' ing it that pretains to this code. :P

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 2:55 am
by Helvecta

Code: Select all

g = love.graphics
img_btnStart = gfx.newImage("test.png")

buttons = {
	["Start"] = {x = 250, y = 350, width = 100, height = 50, pic = img_btnStart, picClick = img_btnStart_click, click = false, state = 0}
}

function love.draw()
	for k,v in pairs(buttons) do
		g.draw(v.pic, v.x, v.y)
	end
end
It seems to work for me; just plop a test.png in the folder you're working with (or check out muh .love! :awesome: )

I have a suspicion that you forgot to include the love.draw() function, or put the g.draw call in the wrong place. Drawing stuff is only possible in love.draw! :neko:

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 4:40 am
by WolfNinja2
Whoops crud sorry there is an error.

incorrect parameter type : expected userdata. on the line with g.draw ...

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 4:40 am
by WolfNinja2
Whoops crud sorry there is an error.

incorrect parameter type : expected userdata. on the line with g.draw ...

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 8:03 am
by Robin
Listen, OP, please just upload a .love, because now we're kinda fishing in the dark.

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 4:16 pm
by WolfNinja2
I'm using love 0.8.0. Here is the .love. menu.lua is where the errors are.

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 4:39 pm
by veethree
You're calling "LOAD_BUTTONS()" where the button picture is defined before you load the image in love.load, So buttons["start"].pic is nil.

Re: Drawing a image from a for loop?

Posted: Sat Jan 18, 2014 5:08 pm
by jjmafiae
Why not upgrade to 0.9.0?