Spawning Enemies

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
J4awesome
Prole
Posts: 3
Joined: Fri Apr 01, 2016 6:24 pm

Spawning Enemies

Post by J4awesome »

Hi,
I new to love2d and I was wondering if there is a very simple way to spawn an enemy.The enemy need to be on the top of the screen and slowly coming down.I would also like for there to be about 2-3 enemies at a time.The size of the window for my game is 400x400.
Any help is great :)
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Spawning Enemies

Post by MadByte »

Welcome J4awesome!

Something to read about "Tables"
Something about "numeric for loops"

There are tons of ways to use tables to hold multiple objects. Here is one:

Code: Select all

local enemies = {}

function newEnemy(x,y)
  local enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.width = 64
  enemy.height = 64
  enemy.removed = false
  table.insert(enemies, enemy)
end


function love.load()
  newEnemy(0, 0)
  newEnemy(100, 100)
  newEnemy(200, 200)
end


function love.update(dt)
  for i=#enemies, 1, -1 do
    local enemy = enemies[i]
    if not enemy.removed then
      -- update enemy
    else table.remove(enemies, i) end
  end
end


function love.draw()
  for i=#enemies, 1, -1 do
    local enemy = enemies[i]
    -- draw stuff
    love.graphics.rectangle("line", enemy.x, enemy.y, enemy.width, enemy.height)
  end
end


function love.keypressed(key)
  if key == "escape" then love.event.quit() end
  if key == "space" and #enemies > 0 then enemies[#enemies].removed = true end
end
spawnEnemies.love
(490 Bytes) Downloaded 238 times
I didn't added the movement because I'm sure you'll easily find out how to do it, anyway if you really want some help with it - go here.
Please read the Lua pil article before asking what exactly happens here. If you afterwards still got questions feel free to ask :)
Last edited by MadByte on Fri Apr 01, 2016 8:07 pm, edited 1 time in total.
J4awesome
Prole
Posts: 3
Joined: Fri Apr 01, 2016 6:24 pm

Re: Spawning Enemies

Post by J4awesome »

Hi,
I used your script in my game and I worked good.But the only thing is I couldn't figure out how to make the enemies move down to the bottom of the screen and hide itself.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Spawning Enemies

Post by Doctory »

J4awesome wrote:Hi,
I used your script in my game and I worked good.But the only thing is I couldn't figure out how to make the enemies move down to the bottom of the screen and hide itself.
You could do this:

Code: Select all

  for k, v in ipairs(enemies)  do
  	v.height = v.height + 100 * dt
  end
Put this in love.update.
J4awesome
Prole
Posts: 3
Joined: Fri Apr 01, 2016 6:24 pm

Re: Spawning Enemies

Post by J4awesome »

I tried everything you told me to do but the 'enemies' are just staying there doing nothing.I sent to my main lua file to see if you know what the problem is.

P.S-If the game looks rushed it's because i'm doing a 24 hour game challenge with my friend :nyu:
Attachments
main.lua
My main.lua file
(1.64 KiB) Downloaded 177 times
monkeyman
Prole
Posts: 6
Joined: Fri Feb 19, 2016 6:29 am

Re: Spawning Enemies

Post by monkeyman »

Doctory wrote:You could do this:

Code: Select all

  for k, v in ipairs(enemies)  do
  	v.height = v.height + 100 * dt
  end
Put this in love.update.
Small typo, v.y not v.height.

Code: Select all

  for k, v in ipairs(enemies)  do
    v.y = v.y + 100 * dt
  end
J4awesome wrote:I tried everything you told me to do but the 'enemies' are just staying there doing nothing.I sent to my main lua file to see if you know what the problem is.

P.S-If the game looks rushed it's because i'm doing a 24 hour game challenge with my friend :nyu:
Here are two .love files, the first is Madbyte's code with Doctory's update suggestion, the second is a small game I worked on a while ago to test out a similar problem. The enemies spawn at the top of the screen at a random x coordinate within the screen bounds, and you have to shoot them before they hit the ground. Arrow keys to move and spacebar to shoot. Hopefully going over the code will help with your problem.

Edit: By the way that's some of the first code I ever wrote, and it's not particularly elegant. The enemy.update and enemy.spawn functions are what you should look at, ignore the other stuff.
Attachments
main.love
(482 Bytes) Downloaded 190 times
SquareBlaster.love
(1.57 KiB) Downloaded 205 times
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Spawning Enemies

Post by Ref »

Could just check Modog's code over on 'Programs and Demos' or my code farther down the post.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests