Page 1 of 1
Spawning Enemies
Posted: Fri Apr 01, 2016 6:28 pm
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
Re: Spawning Enemies
Posted: Fri Apr 01, 2016 7:21 pm
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
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
Re: Spawning Enemies
Posted: Fri Apr 01, 2016 7:43 pm
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.
Re: Spawning Enemies
Posted: Fri Apr 01, 2016 7:54 pm
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.
Re: Spawning Enemies
Posted: Fri Apr 01, 2016 8:23 pm
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
Re: Spawning Enemies
Posted: Fri Apr 01, 2016 9:27 pm
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
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.
Re: Spawning Enemies
Posted: Fri Apr 01, 2016 11:20 pm
by Ref
Could just check Modog's code over on 'Programs and Demos' or my code farther down the post.