How do I make an object spawn at the top of the screen and fall to the bottom constantly.

Basically I have image of a snowball which I want to constantly fall randomly down to the bottom of the screen and get blocked by an umbrella image; what code could I use to do this?

Thank you.

Well, you would have to run a random number generator by using math.random():

https://docs.coronalabs.com/api/library/math/random.html

And set it to be the snowball’s x coordinate, for example:

function spawnSnowball() local position = math.random(1, 200) snowBall.x = position snowBall.y = -100 end Runtime:addEventListener("enterFrame", spawnSnowball)

Of course, you would also have to add physics to the snowball, so it falls. And a physics body with bounce to the umbrella so the snowball is blocked off:

https://docs.coronalabs.com/api/library/physics/addBody.html

If you need more help, let me know.

SDKTester 15, Hi, before putting in the code you gave me I was able to make the snowball fall from a point I placed with gravity but it was only one but when I added the code it disappeared completely? 

I will copy and paste the code with your code as well.

local knife = display.newImage( “knife.png” )

knife:translate (130,90)

knife:scale (0.5,0.5)

local physics = require( “physics” )

physics.addBody( knife, “dynamic”, {bounce = 0.95} )

knife.gravityScale = 0.25

knife.isSleepingAllowed = false

function spawnKnife()

    local position = math.random(1, 200)

    knife.x = position

    knife.y = -100

end

Runtime:addEventListener(“enterFrame”, spawnKnife)

P.S. I said snowball as an example when it actually is meant to be a knife which is why it is labelled knife.

Thank you, please respond.

Look at this that I made, it might help you:

Under level1.lua, if you run it in the Corona Simulator and press Play Now you should see a crate rain.

You can change the image and remove the random rotation, if you need any help, just ask. 

Thank you.

As for the umbrella, just use a dynamic physics body on it and you should be set. 

Well, you would have to run a random number generator by using math.random():

https://docs.coronalabs.com/api/library/math/random.html

And set it to be the snowball’s x coordinate, for example:

function spawnSnowball() local position = math.random(1, 200) snowBall.x = position snowBall.y = -100 end Runtime:addEventListener("enterFrame", spawnSnowball)

Of course, you would also have to add physics to the snowball, so it falls. And a physics body with bounce to the umbrella so the snowball is blocked off:

https://docs.coronalabs.com/api/library/physics/addBody.html

If you need more help, let me know.

SDKTester 15, Hi, before putting in the code you gave me I was able to make the snowball fall from a point I placed with gravity but it was only one but when I added the code it disappeared completely? 

I will copy and paste the code with your code as well.

local knife = display.newImage( “knife.png” )

knife:translate (130,90)

knife:scale (0.5,0.5)

local physics = require( “physics” )

physics.addBody( knife, “dynamic”, {bounce = 0.95} )

knife.gravityScale = 0.25

knife.isSleepingAllowed = false

function spawnKnife()

    local position = math.random(1, 200)

    knife.x = position

    knife.y = -100

end

Runtime:addEventListener(“enterFrame”, spawnKnife)

P.S. I said snowball as an example when it actually is meant to be a knife which is why it is labelled knife.

Thank you, please respond.

Look at this that I made, it might help you:

Under level1.lua, if you run it in the Corona Simulator and press Play Now you should see a crate rain.

You can change the image and remove the random rotation, if you need any help, just ask. 

Thank you.

As for the umbrella, just use a dynamic physics body on it and you should be set.