corona how to make random object fall from the top at random places

So basically what I want is random objects (out of 5 different images) fall from the top of the screen at random places at random times. I am new to app development, so If you post a code, please explain where to put it/how to use it.

Thanx! [import]uid: 104376 topic_id: 21995 reply_id: 321995[/import]

Hope this helps! :slight_smile:

I have gave you the code below for one image to fall, but if you want 5 then you could just duplicate what i have gave you in the same function. Just change the names.

Ask if you have any questions.

Ok so this is what you need:

[code]

local function generateRandomThings()

– First call a this function

local circle = display.newImage “NameOfImage.png”
physics.addBody(circle)

– The two lines of code above is displaying and add physics to your object.

– Below gives the random positions of the object (You can change it)
circle.x = math.random(10, 460)
circle.y = -100

– Also gives a random rotation, thought you might like to see this. For more effect
circle.rotation = math.random(1,360)
circle.alpha = 0.8
end

–[[ The below code calls the function above, you can change
the time it’s 200 at the moment. Then “generateRandomThings”
calls it. 0 is indicating the function to call itself non-stop,
you can change it to any amount you like.]]

timer.performWithDelay(200,generateRandomThings,0)
[import]uid: 23689 topic_id: 21995 reply_id: 87416[/import]

Thank you very much! Works like a charm!
[import]uid: 104376 topic_id: 21995 reply_id: 87446[/import]

Glad it helped ;)! [import]uid: 23689 topic_id: 21995 reply_id: 87447[/import]

Oh, and how do I make the intervals between the falls random?

eg. first object falls, then the second object falls 2 second later, third object falls 1 second later, and so on. [import]uid: 104376 topic_id: 21995 reply_id: 87561[/import]

[lua]math.randomseed(os.time)
local function generateRandomThings()

– First call a this function

local circle = display.newImage “NameOfImage.png”
physics.addBody(circle)

– The two lines of code above is displaying and add physics to your object.

– Below gives the random positions of the object (You can change it)
circle.x = math.random(10, 460)
circle.y = -100

– Also gives a random rotation, thought you might like to see this. For more effect
circle.rotation = math.random(1,360)
circle.alpha = 0.8
end

–[[ The below code calls the function above, you can change
the time it’s 200 at the moment. Then “generateRandomThings”
calls it. 0 is indicating the function to call itself non-stop,
you can change it to any amount you like.]]

timer.performWithDelay(math.random(min value,max value),generateRandomThings,0)[/lua]

[import]uid: 24641 topic_id: 21995 reply_id: 87577[/import]

Thank you!
works like planned.

what is math.randomseed(os.time) for?

It wasn’t in the previous code. [import]uid: 104376 topic_id: 21995 reply_id: 87580[/import]

@stieven.ua

random seed vs random seed os

You can set a “seed” for a random seed generator with a function random see. It’s a number you plug in. So What happens is your game kicks off, and it the generator starts with a fixed seed. However, this is strange to understand because the SAME NUMBER will be generated every time for the duration. So it’s a fixed, random number. Until you relaunch again the another fixed, random number is generated. Yea, it’s a case of “Huhhhhhhh???” so if a number 39.1342 gets generated, then it keeps getting generated, over and over again. Then you exit out, relaunch a new random number is used.
So if you want to figure out how to combat it, use os time instead, it literally uses the time that is seen by the os and uses that as a seed. Since the time always changes in seconds, then voila random time.
Hopefully that makes sense, took me a while to figure out since I was new to coding back in June 2011, and just started getting really comfortable with lua and corona api :slight_smile:

[import]uid: 61600 topic_id: 21995 reply_id: 87776[/import]

That makes perfect sense!

But for some reason my “random seed os” is acting like “random seed”. If I look closely at the object, I notice that the duration between every object is the same. meaning that it uses a same random number every time it generates the object for

timer.performWithDelay(math.random(min value,max value),generateRandomThings,0)

math.random in that line. why is that happening? [import]uid: 104376 topic_id: 21995 reply_id: 87779[/import]