How do you get a spawn function to spawn a different image everytime?

I have a spawn function that spawns scooters that travel across the screen left to right, at various heights. Up until now, every scooter that spawns uses the same display image (a red scooter), but I have since created art assets for several more scooters, and I want my spawn function to spawn these new scooters (chosen at random) as well. How would I go about doing this? Any help would be much appreciated. Here is my spawn function:

local function scooterSpawn (event) local scooter = display.newImage("red scooter.png") scooter:addPhysics(physics,'dynamic',{}) scooter.x = -100 scooter.y = math.random(120,924) physics.addBody(scooter,"dynamic",{isSensor = true}) scooter.isSensor = true transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end}) spawnedGroup:insert(scooter)end[/code]Thank you,Steven [import]uid: 79394 topic_id: 16054 reply_id: 316054[/import]

Lot’s of different ways for that, simplest is probably this:

[code]

math.randomseed( os.time() )

local images = {}

images[#images + 1] = “red scooter.png”
images[#images + 1] = “blue scooter.png”
images[#images + 1] = “green scooter.png”
images[#images + 1] = “yellow scooter.png”

local function scooterSpawn (event)

local index = math.random( 1, #images )

local scooter = display.newImage( images[index] )
scooter:addPhysics(physics,‘dynamic’,{})
scooter.x = -100
scooter.y = math.random(120,924)
physics.addBody(scooter,“dynamic”,{isSensor = true})
scooter.isSensor = true
transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
spawnedGroup:insert(scooter)
end

[/code] [import]uid: 5833 topic_id: 16054 reply_id: 59657[/import]

Worked great! Thanks very much for the advice, GrahamRanson, it’s nice getting help from a pro. Btw, love your monkey and cross-bananas logo:) [import]uid: 79394 topic_id: 16054 reply_id: 59668[/import]

Oh, just curious, but what does the line:
math.randomseed( os.time() )[/code].. do? I understand the workings of all the rest of your code, just not that one line. Thanks! [import]uid: 79394 topic_id: 16054 reply_id: 59669[/import]

You’re welcome! I love the logo too :slight_smile:

When using a random number generator all the numbers are based off a “seed” value, by default imagine this number is 1 ( I’m not actually sure what the default value is ). With that seed if you call the function 5 times you will get 5 random numbers, if you then close the app and open it again then call it 5 more times you will get the same 5 random numbers as you are sill working off the same seed. This can be useful in certain circumstances where you want predictable random numbers ( a bit of an oxymoron ), for instance in testing. However in real production code you will most likely want actual random numbers so you need to give it a seed.

You could give it any seed but you want it to be unique everytime, and the easiest way to do that is to simply use the current time. [import]uid: 5833 topic_id: 16054 reply_id: 59706[/import]

Incredible, I never would have figured that out on my own, glad I asked, and glad there’s people like you on the forums!

Once again, thanks so much for your help and explanation Graham.

Cheers,
Steven [import]uid: 79394 topic_id: 16054 reply_id: 59710[/import]

Happy to help. It’s a nice escape from “real work” :slight_smile: [import]uid: 5833 topic_id: 16054 reply_id: 59723[/import]

Graham,

This is exactly what I am looking for, but I cant get it to function?
I pasted into a new project with my own images, and required physics.
I get a blank screen, I fiddled with it but alas no luck.
I get no errors either.
What am I missing?

Anybody help me please? [import]uid: 127675 topic_id: 16054 reply_id: 122129[/import]

Are you calling the function after declaring it?

If not try calling it with a delay and removing the (event) from the function to test.

[lua]math.randomseed( os.time() )

local images = {}

images[#images + 1] = “red scooter.png”
images[#images + 1] = “blue scooter.png”
images[#images + 1] = “green scooter.png”
images[#images + 1] = “yellow scooter.png”

local function scooterSpawn()

local index = math.random( 1, #images )

local scooter = display.newImage( images[index] )
scooter:addPhysics(physics,‘dynamic’,{})
scooter.x = -100
scooter.y = math.random(120,924)
physics.addBody(scooter,“dynamic”,{isSensor = true})
scooter.isSensor = true
transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
spawnedGroup:insert(scooter)
end

timer.performWithDelay(2000, scooterSpawn, 15)[/lua] [import]uid: 62706 topic_id: 16054 reply_id: 122132[/import]

Hey Deano,
firstly I forgot to add the eventlistener…doh!

Ok, having looked at this a little more I realize there is more to it, so if you could answer me a couple questions.
I want the exact random movement described here albeit right to left, also able to randomise the transition.
I notice

transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
the [#transitions +1], is this referring to another set of table objects? I presume it does as my images are static. What would this table look like?
When I ran this I get errors on line 15 also.

thankyou

[import]uid: 127675 topic_id: 16054 reply_id: 122136[/import]

Graham,

This is exactly what I am looking for, but I cant get it to function?
I pasted into a new project with my own images, and required physics.
I get a blank screen, I fiddled with it but alas no luck.
I get no errors either.
What am I missing?

Anybody help me please? [import]uid: 127675 topic_id: 16054 reply_id: 122129[/import]

Are you calling the function after declaring it?

If not try calling it with a delay and removing the (event) from the function to test.

[lua]math.randomseed( os.time() )

local images = {}

images[#images + 1] = “red scooter.png”
images[#images + 1] = “blue scooter.png”
images[#images + 1] = “green scooter.png”
images[#images + 1] = “yellow scooter.png”

local function scooterSpawn()

local index = math.random( 1, #images )

local scooter = display.newImage( images[index] )
scooter:addPhysics(physics,‘dynamic’,{})
scooter.x = -100
scooter.y = math.random(120,924)
physics.addBody(scooter,“dynamic”,{isSensor = true})
scooter.isSensor = true
transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
spawnedGroup:insert(scooter)
end

timer.performWithDelay(2000, scooterSpawn, 15)[/lua] [import]uid: 62706 topic_id: 16054 reply_id: 122132[/import]

Hey Deano,
firstly I forgot to add the eventlistener…doh!

Ok, having looked at this a little more I realize there is more to it, so if you could answer me a couple questions.
I want the exact random movement described here albeit right to left, also able to randomise the transition.
I notice

transitions[#transitions + 1] = transition.to(scooter, {time = math.random(1400,1500), delay = 0, x = scooter.x + 968, onComplete=function() scooter :removeSelf() end})
the [#transitions +1], is this referring to another set of table objects? I presume it does as my images are static. What would this table look like?
When I ran this I get errors on line 15 also.

thankyou

[import]uid: 127675 topic_id: 16054 reply_id: 122136[/import]