Inserting into tables

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.
Jakemason
Prole
Posts: 15
Joined: Sat Aug 18, 2012 1:30 pm

Inserting into tables

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Inserting into tables

Post 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.
Help us help you: attach a .love.
Jakemason
Prole
Posts: 15
Joined: Sat Aug 18, 2012 1:30 pm

Re: Inserting into tables

Post 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?
User avatar
richapple
Citizen
Posts: 65
Joined: Sun Dec 25, 2011 10:25 am

Re: Inserting into tables

Post 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
Jakemason
Prole
Posts: 15
Joined: Sat Aug 18, 2012 1:30 pm

Re: Inserting into tables

Post by Jakemason »

I'm talking about if the ball that has been drawn needs to be deleted, say if it was a game element.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Inserting into tables

Post 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.
Jakemason
Prole
Posts: 15
Joined: Sat Aug 18, 2012 1:30 pm

Re: Inserting into tables

Post 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
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Inserting into tables

Post 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
Jakemason
Prole
Posts: 15
Joined: Sat Aug 18, 2012 1:30 pm

Re: Inserting into tables

Post 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 :/
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Inserting into tables

Post by Nixola »

Do you call objects.load, objects.update and objects.draw in love.load, love.update and love.draw?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

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