Help: Spawning multiple enemies at set locations

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
reynne
Prole
Posts: 3
Joined: Mon Mar 13, 2017 8:21 pm

Help: Spawning multiple enemies at set locations

Post by reynne »

Hello.

I am trying to learn love2d, and decided to follow a youtube tutorial (https://www.youtube.com/watch?v=FeLljv5clnw&t=689s). However, I cannot get multiple enemies to spawn, and I am pretty sure I am following the instructions correctly. Please take a look at my code and let me know what I am doing wrong. Thank you.

Code: Select all

enemy {}
enemies_controller = {}
enemies_controller.enemies = {}

function enemies_controller:spawnEnemy(x, y)
  enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.bullets = {}
  enemy.cooldown = 0
  enemy.speed = 6
  table.insert(self.enemies, enemy)
end

enemy = {}
enemies_controller:spawnEnemy(0, 0)
enemies_controller:spawnEnemy(100, 100)

function love.update(dt)
  for _,e in pairs (enemies_controller.enemies) do
    e.y = e.y + 1
  end
end

function love.draw()
  --draw enemy
  love.graphics.setColor(255, 0 , 0)
  for _,e in pairs(enemies_controller.enemies) do
    love.graphics.rectangle("fill", e.x, e.y, 80, 20)
  end
end 
User avatar
mtdev
Prole
Posts: 20
Joined: Mon Feb 20, 2017 5:10 am
Location: Midwest US

Re: Help: Spawning multiple enemies at set locations

Post by mtdev »

Line 1 looks like it is missing the =

Code: Select all

enemy = {}
then it worked for me.
reynne
Prole
Posts: 3
Joined: Mon Mar 13, 2017 8:21 pm

Re: Help: Spawning multiple enemies at set locations

Post by reynne »

That was a typo from retyping the code a few times. However, I put in two calls to the spawnEnemy, but it is still not spawning more than one. I am thinking that it is just drawing it with the draw function, but somehow the logic for the spawnEnemy function is not working. I can get one to spawn, but I can't get the second one to appear.
reynne
Prole
Posts: 3
Joined: Mon Mar 13, 2017 8:21 pm

Re: Help: Spawning multiple enemies at set locations

Post by reynne »

Oh, sorry. I figured it out I think. It had something to do with my directories in my ide. It was running a different version of my main.lua which did not have any of the changes to spawn the enemies. Thank you for the help.
User avatar
DanielPower
Citizen
Posts: 50
Joined: Wed Apr 29, 2015 5:28 pm

Re: Help: Spawning multiple enemies at set locations

Post by DanielPower »

I'm glad you got it working, but I would like to add that you should not be using a global variable for enemy.

Code: Select all

-- enemy = {} -- Not necessary
enemies_controller = {}
enemies_controller.enemies = {}

function enemies_controller:spawnEnemy(x, y)
  local enemy = {} -- This variable can be local since we can access it from `enemies_controller.enemies` later
  enemy.x = x
  enemy.y = y
  enemy.bullets = {}
  enemy.cooldown = 0
  enemy.speed = 6
  table.insert(self.enemies, enemy)
end

-- enemy = {} -- I guess you're using this to clear the enemy variable after you added things to it in spawnEnemy.
-- This is no longer necessary since enemy is now local, and disappears as soon as you exit the spawnEnemy function.

enemies_controller:spawnEnemy(0, 0)
enemies_controller:spawnEnemy(100, 100)

function love.update(dt)
  for _,e in pairs (enemies_controller.enemies) do
    e.y = e.y + 1
  end
end

function love.draw()
  --draw enemy
  love.graphics.setColor(255, 0 , 0)
  for _,e in pairs(enemies_controller.enemies) do
    love.graphics.rectangle("fill", e.x, e.y, 80, 20)
  end
end 
By using a local variable for enemy, the variable will only exist when you actually need it to, and won't get in the way of other places where you might want to use that name. For example, later on you may want to have more than one type of enemy. It's just a lot cleaner to use local variables when appropriate.

There is also a performance boost when using local variables, but it is rather small, and not worth worrying about unless your project is quite large.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests