Spawning objects, random timer.

–Is there a better way? Basically what happens is if I do
–local randomNumber1 = math.random(8000)
–local myTimer1 = timer.performWithDelay (randomNumber1, makeBox, 10)
–What will happen is 10 boxes will be made with a set random number
–between 1 and 8000 for each of the 10 boxes. So basically a random number
–is chosen but the boxes will come out evenly spaced and not random.
–SOOOOOOO, I had to be creative and create a bunch of variables
–1 through 10 so the code could run through each line and execute…
–The problem is I plan on ramping up to 1000 boxes!!!


–I am very new to programming so I have basically gone through every
–sample, tutorial, etc etc and commenting out each line, seeing what does
–what etc.
–I just want to randomly spawn a box at different intervals based on
–a range of 1 to 8000 for example but have EACH box be at a different
–interval…code below (I know this is not efficient, but works for —me!..for now. I know I want to be clean and I haven’t tested on hardware
–im doing the unlimited trial until I have something to publish :slight_smile:

–Thanks in advance for help! - Nick
local randomNumber1 = math.random(8000
local myTimer1 = timer.performWithDelay (randomNumber1, makeBox, 1)
local randomNumber2 = math.random(8000)
local myTimer2 = timer.performWithDelay (randomNumber2, makeBox, 1)
local randomNumber3 = math.random(8000)
local myTimer3 = timer.performWithDelay (randomNumber3, makeBox, 1)
local randomNumber4 = math.random(8000)
local myTimer4 = timer.performWithDelay (randomNumber4, makeBox, 1)
local randomNumber5 = math.random(8000)
local myTimer5 = timer.performWithDelay (randomNumber5, makeBox, 1)
local randomNumber6 = math.random(8000)
local myTimer6 = timer.performWithDelay (randomNumber6, makeBox, 1)
local randomNumber7 = math.random(8000)
local myTimer7 = timer.performWithDelay (randomNumber7, makeBox, 1)
local randomNumber8 = math.random(8000)
local myTimer8 = timer.performWithDelay (randomNumber8, makeBox, 1)
local randomNumber9 = math.random(8000)
local myTimer9 = timer.performWithDelay (randomNumber9, makeBox, 1)
local randomNumber10 = math.random(8000)
local myTimer10 = timer.performWithDelay (randomNumber10, makeBox, 1) [import]uid: 61600 topic_id: 11149 reply_id: 311149[/import]

I’ll preface this with saying you will get a crash if you spawn 1000 boxes at once on a mobile device.

To do this you could just use a timer to create a “parent” loop yimer that would then go through and spawn all of the child timers.

Also, take some time to look at other posts on the forum to see how they are formatted.

Something like:

[code]

local num

local function maketimers()

num = math.random(1,8000)

timer.performWithDelay(num, makebox, 1)

end

timer.performWithDelay(1, maketimers, 1000)

[/code] [import]uid: 10903 topic_id: 11149 reply_id: 40467[/import]

I’ll give that a shot. The 1000 boxes would dissapear when they reached one side of the screen

I am using a clean up function

local function killBox(Box)
Box:removeSelf()
end

transition.to(Box, {time=(box.x-50)*8, x=50, onComplete=killBox})
So I could see 1000 all on screen crashing, but they are to be removed when they hit a certain coordinate. In the sim it works so far, but I got a long way to go (completely new to programming, my 3rd week!)!
Also I’ll watch my post formatting so it includes line #'s :slight_smile:
-Nick [import]uid: 61600 topic_id: 11149 reply_id: 40525[/import]

Just use the code tags when you’re posting code.

You may want to look at using a “for” loop to add and remove your objects.

http://www.planetlua.com/journal/tag/corona [import]uid: 10903 topic_id: 11149 reply_id: 40587[/import]