Page 1 of 1

Need help with multiple objects

Posted: Tue Jul 29, 2014 4:13 pm
by smijjy
I've just started this coding games business so sorry for my noobness :P

I'm working on a tower deference game, I'm trying to code the enemy's and I've run into the problem of I have no idea how to add multiples of the same enemy, what ever I try doesn't seem to work and I cant find on the internet what I need (mainly because I don't have much idea on what I'm supposed to look at), so I need help from the lovely people on the love2d forums

Here's the .love file of what I've done so far

https://drive.google.com/file/d/0Bwn9_x ... sp=sharing

if you need some other info just ask (as I've probably not included it because I'm really quite crap at this so far)

:P

Re: Need help with multiple objects

Posted: Tue Jul 29, 2014 7:43 pm
by Cucurbitacée
Hi,

I've just looked at your code and here is what I would do:
  • Create a table to hold the enemies, like this:

    Code: Select all

    enemies = {}
  • In the function enemy.new, replace this instruction return enemyStat by this:

    Code: Select all

    table.insert(enemies, enemyStat)
  • Replace the functions to update and draw the enemy by an iteration of the enemies table, for example:

    Code: Select all

    function enemy.draw()
    	for i, v in ipairs(enemies) do
    		love.graphics.draw(enemyImage, v.x, v.y)
    	end
    end
Hope that helps.

Re: Need help with multiple objects

Posted: Wed Jul 30, 2014 11:01 am
by smijjy
Thanks, now I can finally get on with coding another part of the game! :)