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:

1
2
3
4
5
6
7
8
9
10
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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

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:)

Oh, just curious, but what does the line:

1
math.randomseed( os.time() )

You're welcome! I love the logo too :-)

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.

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

Happy to help. It's a nice escape from "real work" :-)

views:1385 update:2011/10/18 8:54:01
corona forums © 2003-2011