Little help lua table ( one more lol )!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

Little help lua table ( one more lol )!

Post by Neolitik » Wed Jul 29, 2009 10:44 am

:death:
hello all ,

i ' don't understand this : function Me.Draw(bla,bla) just it exemple.

they are some way to doing this ? :

Code: Select all

function Me.new(tx,ty)

table.insert(Me,{Img=love.graphics.newImage("love_ball.png");
				x=tx;	
				y=ty})
end

function load()
Me:new(200,200)
Me:new(70,500)
Me:new(200,400)
end


thank for all !

Arthurio
Prole
Posts: 8
Joined: Sun Jul 12, 2009 7:12 pm

Re: Little help lua table ( one more lol )!

Post by Arthurio » Wed Jul 29, 2009 11:20 am

Perhaps you could try explaining what you are trying to do?

Code: Select all

--declare the variable before trying to use it
images = {}

function loadImages()
	
	--load images only once
	loveball = love.graphics.newImage("images/love-ball.png")

end

function newImage(tx,ty)
	
	--use the image reference
	table.insert(images, {img=loveball; x=tx; y=ty})
end

function load()
	
	loadImages()
	
	newImage(200,200)
	newImage(70,500)
	newImage(200,400)
	
	--print stuff into log (check the stdout.txt)
	for key, value in ipairs(images) do
		print(key .. " " .. value.x .. " " .. value.y)
	end
end

function draw()
	
	--draw images
	for key, value in ipairs(images) do
		love.graphics.draw(value.img, value.x, value.y)
	end
	
end

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Little help lua table ( one more lol )!

Post by Robin » Wed Jul 29, 2009 4:24 pm

Neolitik: the table:func() notation uses an implicit self, and is equivalent to table.func(self). However, you are using it inconsistently: in the function declaration, you use . but every function call you use :

In this case, you're not using the self argument, so you can either change it to

Code: Select all

function Me.new(tx,ty)

table.insert(Me,{Img=love.graphics.newImage("love_ball.png");
				x=tx;	
				y=ty})
end

function load()
Me.new(200,200)
Me.new(70,500)
Me.new(200,400)
end

or

Code: Select all

function Me:new(tx,ty)

table.insert(self,{Img=love.graphics.newImage("love_ball.png");
				x=tx;	
				y=ty})
end

function load()
Me:new(200,200)
Me:new(70,500)
Me:new(200,400)
end
Help us help you: attach a .love.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests