Only one of many objects is drawn

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
Comonut
Prole
Posts: 2
Joined: Sat Mar 02, 2013 11:01 am

Only one of many objects is drawn

Post by Comonut »

Hi guys,
I'm trying to program enemies, but i've run into a little problem. Right now, the object for the enemies is very basic - a body with some functions to get coordinates and sprite. Here is the code for the object

Code: Select all

function spawnEnemy(enemyx,enemyy)
	enemy = {}
	enemy.body = love.physics.newBody(world, enemyx, enemyy, "dynamic")
  enemy.shape = love.physics.newCircleShape(15) 
  enemy.fixture = love.physics.newFixture(enemy.body, enemy.shape, 1) 
  enemy.fixture:setRestitution(0.3)
  enemy.graphic = enemyr
	function enemy.setImage(Image)
	enemy.graphic = Image
	end
	function enemy.getImage()
	return enemy.graphic
	end
	function enemy.getX()
	return enemy.body:getX()
	end
	function enemy.getY()
	return enemy.body:getY()
	end
	function enemy.getXY()
	return enemy.body:getX() .. enemy.body:getY()
	
	end
return enemy
end
Then I spawn some of this objects with

Code: Select all

enemy1 = spawnEnemy(150,500)
enemy2 = spawnEnemy(500,500)
I will probably use arrays later on but for now i use this for testing.
Here is where the problem appears, no matter how many of these objects i have spawned, only one thing will be drawn on the screen by using

Code: Select all

love.graphics.draw(enemy1:getImage(),enemy1:getX(),enemy1:getY())
love.graphics.draw(enemy2:getImage(),enemy2:getX(),enemy2:getY())
Always, one of the objects is drawn, the other one behaves correctly with collision and other physics stuff, but it appears invisible.
Thanks and I hope that im not being annoying with my struggles.
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Only one of many objects is drawn

Post by iPoisonxL »

Try

Code: Select all

love.graphics.setColor(255,255,255)
to set the sprites to a blank color, that might be the problem.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Only one of many objects is drawn

Post by Jasoco »

I have a tip.

Don't create all your objects like that. You have enemy1, enemy2, enemy3, etc. Instead create an enemy table:

Code: Select all

enemy = {}
Then add objects to it like so:

Code: Select all

enemy[id] = spawnEnemy(x,y)
You can also insert it into the enemy table with table.insert but I prefer to just create them this way. My prefered method is where id will = #enemy+1. #enemy returns how many items are in the enemy table.

Then when you need to update or draw or otherwise refer to all your enemies at once, you'd loop through them thusly:

Code: Select all

for i, e in pairs(enemy) do
  -- You'd use e as a reference to the enemy.
  -- So for instance if you want to draw at the enemy's X and Y it would just be e.x or e.y or as in your code
  -- e.getX() and e.getY() or e.graphic or e.whatever.
  -- That's what the e means in the for line. The i will be returned as the current number, basically the id in the code above.
end
For pairs will loop through all the objects in the enemy table for you returning i for the ID of the table row and e for a reference to the object as explained above.

It's much much MUCH better than having separate enemy objects since you can't maintain that very well at all when it gets much more complicated.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests