Enemy Spawning
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 15
- Joined: Wed Oct 03, 2012 3:37 pm
Enemy Spawning
Hello
I have one enemy at the moment which I can kill.
I would like to have this enemy spawn multiple time every x seconds at different position.
I have no idea how to do this
Thanks
I have one enemy at the moment which I can kill.
I would like to have this enemy spawn multiple time every x seconds at different position.
I have no idea how to do this
Thanks
Re: Enemy Spawning
Hi,
One simple solution is to use the hump's timer library which has a function quite adapted :
http://vrld.github.com/hump/#hump.timer
For example, if you want to spawn enemies every 5 seconds, you would have to call (only once) this :
Where spawnTonsOfEnnemies() picks some random location and add the ennemies.
Have fun!
One simple solution is to use the hump's timer library which has a function quite adapted :
http://vrld.github.com/hump/#hump.timer
For example, if you want to spawn enemies every 5 seconds, you would have to call (only once) this :
Code: Select all
Timer.addPeriodic(5, function()
spawnTonsOfEnnemies()
end)
Have fun!
Current project : 3D fractal explorer DEFract ( viewtopic.php?t=9193)
Github : https://github.com/plule/ | Twitter : http://twitter.com/plule_
Github : https://github.com/plule/ | Twitter : http://twitter.com/plule_
-
- Prole
- Posts: 15
- Joined: Wed Oct 03, 2012 3:37 pm
Re: Enemy Spawning
Sounds good but any help on the spawnTonsOfEnnemies()
Re: Enemy Spawning
From what I can tell the one enemy you have is a premade table, instead you should create tables for enemies that go in an enemies table that stores all of them with that in mind spawnTonsOfEnemies() would be something like:
Code: Select all
function spawnTonsOfEnemies()
for i=1,10 do --Replace 10 with number of enemies you want
enemies[#enemies+1]={
--Put data for enemy here (such as health, position, velocity etc)
}
end
end
Your screen is very zoomed in...
Re: Enemy Spawning
I could tell you how I do it . It should be readable but I'm not sure if this is efficient spawning code.
hopefully you know what self means.
First you have to create a global enemy table to save the functions and values.
Then load everything with a init / load function( or declare this anywhere else [ i.e in the love.load() ] ):
Then I create a function to generate a new enemy. This create a table into the self.table( self = enemy / the name of the global table ) for the enemy.
Now you have to update the spawn and the enemy itself( here is just the spawner ).
yeah, and now there is only one thing left to do. Draw all enemys.
the ipairs goes through the full self.table ( enemy.table = stores the single enemys ). v is the value of a single enemy, i is the index ( the position of the enemy in the self.table )
Hopefully I could help.
hopefully you know what self means.
First you have to create a global enemy table to save the functions and values.
Then load everything with a init / load function( or declare this anywhere else [ i.e in the love.load() ] ):
Code: Select all
enemy = {}
function enemy:init()
self.table = {} -- stores the single enemys later
self.timer = 0 -- use with spawntimer
self.spawnTime = 3 -- time when the next enemy should spawn
end
Code: Select all
function enemy:new( x, y, xvel ) -- to use self you have to type a " : " to say that you want to use the self.
local temp = {
x = x,
y = y,
w = 32,
h = 64,
xvel = xvel,
}
table.insert( self.table, temp )
end
Code: Select all
function enemy:update( dt )
self.timer = self.timer + dt
if self.timer > self.spawnTime then
enemy:new( math.random( -200, width + 200 ) , height - 64 , 150 ) -- math.random for a different position everytime you spawn an enemy
self.timer = 0
end
-- update anything else enemy related here
end
the ipairs goes through the full self.table ( enemy.table = stores the single enemys ). v is the value of a single enemy, i is the index ( the position of the enemy in the self.table )
Code: Select all
function enemy:draw()
for i, v in ipairs( self.table ) do
lg.setColor( 0, 0, 255 )
lg.rectangle( "line", v.x, v.y, v.w, v.h )
end
end
-
- Prole
- Posts: 15
- Joined: Wed Oct 03, 2012 3:37 pm
Re: Enemy Spawning
Getting this problem
Heres my table
and
and this error
Atempt to index global zombie a nil value?
Heres my table
Code: Select all
zombie = {
x = math.random(0,728),
y = math.random(0,428)
}
Code: Select all
function zombie:init()
self.table = {}
self.timer = 0
self.spawnTime = 3
end
Atempt to index global zombie a nil value?
Re: Enemy Spawning
May you should begin with the code from mickeyjm. It's much easier to understand.
The zombie table didn't get x, y values. They have to be in the self.table create like I wrote or you combine the code from
mickeyjm with mine. just remove function enemy:new and insert his code in the update.
The zombie table didn't get x, y values. They have to be in the self.table create like I wrote or you combine the code from
mickeyjm with mine. just remove function enemy:new and insert his code in the update.
Code: Select all
zombie = {}
function spawnZombies()
for i=1,10 do --Replace 10 with number of enemies you want
zombies[#zombies+1]={
x = math.random(0,728),
y = math.random(0,428),
}
end
end
function zombie:init()
self.timer = 0
self.spawnTime = 3
end
function zombie:update( dt )
self.timer = self.timer + dt
if self.timer > self.spawnTime then
spawnZombies() -- his code
self.timer = 0
end
-- update anything else enemy related here
end
Re: Enemy Spawning
You have Deathmatch AI activated, so that's why they shoot eachother.
You should set your spawncount to a global variable that would fix your problem I think.
Can't see that much on these screens though.
You should set your spawncount to a global variable that would fix your problem I think.
Can't see that much on these screens though.
-
- Prole
- Posts: 15
- Joined: Wed Oct 03, 2012 3:37 pm
Re: Enemy Spawning
I know this thread is old but how do I draw a image on these enemies. For my old single enemy I was doing
Code: Select all
love.graphics.draw(zombieimg, zombie.x, zombie.y)
Re: Enemy Spawning
Add an extra variable to your 'zombie' table, which corresponses to the correct picture of that zombie.SuperMeijin wrote:I know this thread is old but how do I draw a image on these enemies. For my old single enemy I was doingCode: Select all
love.graphics.draw(zombieimg, zombie.x, zombie.y)
Something like this :
zombie = {
x = 120,
y = 310,
texture = <TEXTURE>
}
love.graphics.draw(zombie.texture, zombie.x, zombie.y)
Who is online
Users browsing this forum: Google [Bot] and 3 guests