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
Spawning Enemies
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Spawning Enemies
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:
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
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
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.
Re: Spawning Enemies
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.
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
You could do this: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.
Code: Select all
for k, v in ipairs(enemies) do
v.height = v.height + 100 * dt
end
Re: Spawning Enemies
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
P.S-If the game looks rushed it's because i'm doing a 24 hour game challenge with my friend
- Attachments
-
- main.lua
- My main.lua file
- (1.64 KiB) Downloaded 177 times
Re: Spawning Enemies
Small typo, v.y not v.height.Doctory wrote:You could do this:Put this in love.update.Code: Select all
for k, v in ipairs(enemies) do v.height = v.height + 100 * dt end
Code: Select all
for k, v in ipairs(enemies) do
v.y = v.y + 100 * dt
end
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.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
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
Re: Spawning Enemies
Could just check Modog's code over on 'Programs and Demos' or my code farther down the post.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests