[SOLVED] Help How to have a objects spawn more frequent after certain?

Hi

I have been struggling how to have an object spawn frequent after a certain time. I had a code written for that but deleted it because it was not working. I want to know how can I make an object spawn more frequent after 60 seconds than spawn more frequent after 90 seconds etc…

Thanks [import]uid: 17058 topic_id: 30610 reply_id: 330610[/import]

There are quite a few ways you could do it. The main thing would be keeping track of how much total time has passed and how much time has passed since your last spawning. Below code probably won’t work due to syntax errors but at least the logic should be sound.

[lua]local iTotalTimer = 0
local iTimeSinceSpawn = 0

– timer goes off every second just for easy maths
spawnTimer = timer.performWithDelay(1000, spawnTimerListener, 0)

function spawnTimerListener()
–increment timers. god I miss incrementing by int++
iTotalTimer = iTotalTimer + 1;
iTimeSinceSpawn = iTimeSinceSpawn + 1

–check the total timer and spawntimer. going to go with a 10 second, 5second then 1 second spawn timer. spawnEnemy would be whatever function creates your new object.
if iTotalTimer < 60 and iTimeSinceSpawn >= 10 then
spawnEnemy()
–really, iTimeSinceSpawn would be better in your spawnEnemy() function but i don’t have one of those so there it is.
iTimeSinceSpawn = 0
elseif iTotalTimer >=60 and iTotalTimer < 90 and iTimeSinceSpawn >=5 then
spawnEnemy()
itimeSinceSpawn = 0
elseif iTotalTimer >= 90 and iTimeSinceSpawn >= 1 then
spawnEnemy()
itimeSinceSpawn = 0

end[/lua] [import]uid: 147305 topic_id: 30610 reply_id: 122646[/import]

just one more “end” and it’s perfect? haha [import]uid: 116842 topic_id: 30610 reply_id: 122653[/import]

@budershank Thanks. Your right there is many ways to do it. I tried to do it your way it was kinda confusing. So I did my way below for anyone that want to see.

local physics = require("physics")  
physics.start()  
  
local timers = 0  
  
local function spawnEnemy ()  
local function yyy ()  
if timers \<= 3 then  
timer.pause(go1)  
end  
if timers \>= 3 and timers \< 10 then  
timer.cancel (go)  
timer.resume(go1)  
end  
if timers \>= 10 then  
timer.cancel(go1)  
local function change ()  
director:changeScene("menu2", "fade")  
end  
timer.performWithDelay(2000, change, 1)  
end  
end  
yyy()  
timers = timers + 1  
print(timers)  
local enemy = display.newRect(0,0,50, 50)  
enemy.x = math.random(0, 320)  
enemy.y = -10  
physics.addBody(enemy)  
end  
go = timer.performWithDelay(2000, spawnEnemy, 0)  
go1 = timer.performWithDelay(1000, spawnEnemy, 0)  

Thanks :slight_smile: [import]uid: 17058 topic_id: 30610 reply_id: 122896[/import]

There are quite a few ways you could do it. The main thing would be keeping track of how much total time has passed and how much time has passed since your last spawning. Below code probably won’t work due to syntax errors but at least the logic should be sound.

[lua]local iTotalTimer = 0
local iTimeSinceSpawn = 0

– timer goes off every second just for easy maths
spawnTimer = timer.performWithDelay(1000, spawnTimerListener, 0)

function spawnTimerListener()
–increment timers. god I miss incrementing by int++
iTotalTimer = iTotalTimer + 1;
iTimeSinceSpawn = iTimeSinceSpawn + 1

–check the total timer and spawntimer. going to go with a 10 second, 5second then 1 second spawn timer. spawnEnemy would be whatever function creates your new object.
if iTotalTimer < 60 and iTimeSinceSpawn >= 10 then
spawnEnemy()
–really, iTimeSinceSpawn would be better in your spawnEnemy() function but i don’t have one of those so there it is.
iTimeSinceSpawn = 0
elseif iTotalTimer >=60 and iTotalTimer < 90 and iTimeSinceSpawn >=5 then
spawnEnemy()
itimeSinceSpawn = 0
elseif iTotalTimer >= 90 and iTimeSinceSpawn >= 1 then
spawnEnemy()
itimeSinceSpawn = 0

end[/lua] [import]uid: 147305 topic_id: 30610 reply_id: 122646[/import]

just one more “end” and it’s perfect? haha [import]uid: 116842 topic_id: 30610 reply_id: 122653[/import]

@budershank Thanks. Your right there is many ways to do it. I tried to do it your way it was kinda confusing. So I did my way below for anyone that want to see.

local physics = require("physics")  
physics.start()  
  
local timers = 0  
  
local function spawnEnemy ()  
local function yyy ()  
if timers \<= 3 then  
timer.pause(go1)  
end  
if timers \>= 3 and timers \< 10 then  
timer.cancel (go)  
timer.resume(go1)  
end  
if timers \>= 10 then  
timer.cancel(go1)  
local function change ()  
director:changeScene("menu2", "fade")  
end  
timer.performWithDelay(2000, change, 1)  
end  
end  
yyy()  
timers = timers + 1  
print(timers)  
local enemy = display.newRect(0,0,50, 50)  
enemy.x = math.random(0, 320)  
enemy.y = -10  
physics.addBody(enemy)  
end  
go = timer.performWithDelay(2000, spawnEnemy, 0)  
go1 = timer.performWithDelay(1000, spawnEnemy, 0)  

Thanks :slight_smile: [import]uid: 17058 topic_id: 30610 reply_id: 122896[/import]