Page 1 of 3

Inserting into tables

Posted: Sat Aug 18, 2012 1:38 pm
by Jakemason
Hi, I have been learning how to use tables. I understand the basic concepts but I don't know how I would do something like being able to spawn multiple objects that all have the same image, etc. At the moment I have this in an objects.lua:
objects = {}

function objects.load()
objects.ball.image = love.graphics.newImage("images/ball.png")
end

function objects.ball.spawn()

end

function objects.ball.draw()
love.graphics.draw(objects.ball.image,x,y)
end

Re: Inserting into tables

Posted: Sat Aug 18, 2012 4:09 pm
by Robin
You need to do this at least:

Code: Select all

function objects.load()
   objects.ball = {} -- this
   objects.ball.image = love.graphics.newImage("images/ball.png")
end
If you have a more specific question, I could help you more.

Re: Inserting into tables

Posted: Sat Aug 18, 2012 4:32 pm
by Jakemason
Ok, so how would I then draw the object in different places. Let's say, get the position of the mouse cursor and draw the multiple instances of the ball at different places and then delete them?

Re: Inserting into tables

Posted: Sat Aug 18, 2012 5:25 pm
by richapple
Jakemason wrote:Ok, so how would I then draw the object in different places. Let's say, get the position of the mouse cursor and draw the multiple instances of the ball at different places and then delete them?
You can use this image every time you want to draw it:

Code: Select all

function love.draw()
    love.graphics.draw(objects.ball.image, posx, posy, ..
    love.graphics.draw(objects.ball.image, otherposx..
end
I don't see the reason to delete it though

Re: Inserting into tables

Posted: Sat Aug 18, 2012 5:40 pm
by Jakemason
I'm talking about if the ball that has been drawn needs to be deleted, say if it was a game element.

Re: Inserting into tables

Posted: Sat Aug 18, 2012 5:57 pm
by Roland_Yonaba
Jakemason wrote:I'm talking about if the ball that has been drawn needs to be deleted, say if it was a game element.
richapple wrote:I don't see the reason to delete it though
Well, I tend to think the same.
Anyway, there are many ways to achieve this.You can set an axtra boolean to control whether or not the ball should be drawn.

Code: Select all

function objects.load()
   objects.ball = {} -- this
   object.ball.shouldDraw = true
   objects.ball.image = love.graphics.newImage("images/ball.png")
end

function objects.ball.draw()
   if objects.ball.shouldDraw then love.graphics.draw(objects.ball.image,x,y) end
end
When you won't need it to be drawn, under some specific conditions, you, can set this boolean to false.
So you don't need to "delete" it.
Hope this helps.

Re: Inserting into tables

Posted: Sat Aug 18, 2012 6:13 pm
by Jakemason
Ok, so here is what I have but nothing happens when I press space.

Code: Select all

objects = {}

function objects.load()
	objects.ball = {} 
	objects.ball.shouldDraw = false
	objects.ball.image = love.graphics.newImage("images/ball.png")
end

function objects.draw()
	love.graphics.draw(objects.ball.image, x, y)
end

function objects.update()
	x, y = love.mouse.getPosition()
	if objects.ball.shouldDraw then love.graphics.draw(objects.ball.image,x,y) end
	if love.keyboard.isDown("space") then objects.ball.shouldDraw = true else objects.ball.shouldDraw = false end
end

Re: Inserting into tables

Posted: Sat Aug 18, 2012 6:18 pm
by ivan
Hi. Like Roland said, you have to check the "shouldDraw" boolean from the "draw" function:

Code: Select all

function objects.ball.draw()
  if objects.ball.shouldDraw then
    love.graphics.draw(objects.ball.image,x,y)
  end
end
Also, you don't need to call "draw" from the "update" function:

Code: Select all

function objects.update()
   if love.keyboard.isDown("space") then
       objects.ball.shouldDraw = true
   else
       objects.ball.shouldDraw = false
   end
end

Re: Inserting into tables

Posted: Sat Aug 18, 2012 6:35 pm
by Jakemason
Ok, so now I have:

Code: Select all

objects = {}

function objects.load()
	objects.ball = {} 
	objects.ball.shouldDraw = false
	objects.ball.image = love.graphics.newImage("images/ball.png")
end

function objects.draw()
	if objects.ball.shouldDraw then love.graphics.draw(objects.ball.image,x,y) end
end

function objects.update()
	x, y = love.mouse.getPosition()
	if love.keyboard.isDown("space") then objects.ball.shouldDraw = true else objects.ball.shouldDraw = false end
end
and still nothing appears :/

Re: Inserting into tables

Posted: Sat Aug 18, 2012 6:51 pm
by Nixola
Do you call objects.load, objects.update and objects.draw in love.load, love.update and love.draw?