Question about using a performWithDelay repeater

So I wrote the following code to target an object with a random delay timer. The code works, however it will only select a single object at a time. What I need is to target an object at a random time (like the code I already wrote) but also potentially target multiple objects at the same time. To clarify, the self.targeting function is a function located within the scene. The self is the scene. This code is located in the enterScene function of my storyboard scene.

 function targetingRepeater() self.targeting({objTable = targetTable,}) timer.performWithDelay( math.random( 1000, 8000 ), function() targetingRepeater() end, 1 ) end timer.performWithDelay( math.random( 1000, 8000 ), function() targetingRepeater() end, 1 )

Does anyone have any idea about how to do that without the targeting function increasing out of control? Ideally I would like 1-5 objects to be targeted at the same time, but I’m not sure how to do that without the targeting repeater function to spiral out of control targeting objects at an exponential rate. Help is MUCH appreciated. 

Hi,

I’m not 100% sure what you want but I will try anyway.

Do you want an endless loop like this?

Maybe a Runtime:addEventListener(“enterFrame”, yourFunction) suits you better?

If you want to target 1-5 objects within the same loop can’t you just add a for loop?

function targetingRepeater() local rnd = math.random(1,5) for i = 1, rnd, 1 do self.targeting({objTable = targetTable,}) timer.performWithDelay( math.random( 1000, 8000 ), function() targetingRepeater() end, 1 ) end end

This way you will get 1-5 objects targeted at the same time.

Once again, I’m not sure what “targeting” does but if you want to set a maximum amount of “targeting” just set an if statement and make sure you remove the unused objects.

I hoped this helped a little because my answer is taken from thin air.

Best regards,

Tomas

Thanks Tomas. I will try the for loop. I may add a counter of how many objects are being targeted so I don’t get more than 5. I’m making a shooting gallery style game, so the targeting function activates targets. So I want there to be multiple targets activated at the same time, on random time intervals, but because I’m using a recursive function call to make the delay timer random I was afraid it would continue to just add active targets in an increasing frequency until there were way too many active at once. I hope that makes sense. I’ll try to add this later today and see if the for loop with a counter works out. Thanks again.

If you have 1 - 5 enemies that should popup but should always be maximum 5 I should do maybe something like this:

local enemies = {} local maxEnemies = 5 local isCreatingEnemies = false function createEnemies() -- Verify that there is not more than maxEnemies on the screen if #enemies \< maxEnemies and isCreatingEnemies == false then isCreatingEnemies = true -- How many enemies should be create? -- The total amount will never be more than maxEnemies local noOfEnemies = math.random(1, maxEnemies - #enemies) for i = 1, noOfEnemies , 1 do local time = math.random(1000, 8000) local function t() -- Do your targeting thingy here!!! -- If the last enemy have been created set back the isCreatingEnemies so -- we can create new enemies again. Might need some tweaking because if 5 -- enemies pop ups fast and you kill them there might be a delay with the -- current logic if i == noOfEnemies then isCreatingEnemies = true end end timer.performWithDelay(time, t) end end end Runtime:addEventListener("eventFrame", createEnemies)&nbsp;

Code was written from head so it might contain errors but I hope you get the idea from it.

Best regards,

Tomas

The thing about the Runtime event “enterFrame” is that is being processesd every FPS (which is 30 times per second by default).

http://www.coronalabs.com/blog/2012/02/21/understanding-coronas-enterframe-event/

It can also be stopped easily:

function endGame() Runtime:removeListener("enterFrame", createEnemies) -- EndGame functions destroyCurrentEnemies() showFinalScoreMessage() end

Best regards,

Tomas

Thanks Tomas,

I think that will do exactly what I was looking for. I really appreciate the help.

Hi,

I’m not 100% sure what you want but I will try anyway.

Do you want an endless loop like this?

Maybe a Runtime:addEventListener(“enterFrame”, yourFunction) suits you better?

If you want to target 1-5 objects within the same loop can’t you just add a for loop?

function targetingRepeater() local rnd = math.random(1,5) for i = 1, rnd, 1 do self.targeting({objTable = targetTable,}) timer.performWithDelay( math.random( 1000, 8000 ), function() targetingRepeater() end, 1 ) end end

This way you will get 1-5 objects targeted at the same time.

Once again, I’m not sure what “targeting” does but if you want to set a maximum amount of “targeting” just set an if statement and make sure you remove the unused objects.

I hoped this helped a little because my answer is taken from thin air.

Best regards,

Tomas

Thanks Tomas. I will try the for loop. I may add a counter of how many objects are being targeted so I don’t get more than 5. I’m making a shooting gallery style game, so the targeting function activates targets. So I want there to be multiple targets activated at the same time, on random time intervals, but because I’m using a recursive function call to make the delay timer random I was afraid it would continue to just add active targets in an increasing frequency until there were way too many active at once. I hope that makes sense. I’ll try to add this later today and see if the for loop with a counter works out. Thanks again.

If you have 1 - 5 enemies that should popup but should always be maximum 5 I should do maybe something like this:

local enemies = {} local maxEnemies = 5 local isCreatingEnemies = false function createEnemies() -- Verify that there is not more than maxEnemies on the screen if #enemies \< maxEnemies and isCreatingEnemies == false then isCreatingEnemies = true -- How many enemies should be create? -- The total amount will never be more than maxEnemies local noOfEnemies = math.random(1, maxEnemies - #enemies) for i = 1, noOfEnemies , 1 do local time = math.random(1000, 8000) local function t() -- Do your targeting thingy here!!! -- If the last enemy have been created set back the isCreatingEnemies so -- we can create new enemies again. Might need some tweaking because if 5 -- enemies pop ups fast and you kill them there might be a delay with the -- current logic if i == noOfEnemies then isCreatingEnemies = true end end timer.performWithDelay(time, t) end end end Runtime:addEventListener("eventFrame", createEnemies)&nbsp;

Code was written from head so it might contain errors but I hope you get the idea from it.

Best regards,

Tomas

The thing about the Runtime event “enterFrame” is that is being processesd every FPS (which is 30 times per second by default).

http://www.coronalabs.com/blog/2012/02/21/understanding-coronas-enterframe-event/

It can also be stopped easily:

function endGame() Runtime:removeListener("enterFrame", createEnemies) -- EndGame functions destroyCurrentEnemies() showFinalScoreMessage() end

Best regards,

Tomas

Thanks Tomas,

I think that will do exactly what I was looking for. I really appreciate the help.