Please help me to understand one line of code

I just met with a sample of corona sdk named as Many Crates…though the code is in difficult way so i changed it to easy sone that i can understand but with everything understood there is one thing left which is cannot understand and that is

 local function newCrate( event ) local random = math.random( 100 ) if (random \<= 60) then crate = display.newImage("crate.png"); crate.x = 60 + math.random( 160 ) crate.y = -100 physics.addBody( crate, { density=0.9, friction=0.3, bounce=0.3} ) elseif (random \< 80) then crateB = display.newImage("crateB.png"); crateB.x = 60 + math.random( 160 ) crateB.y = -100 physics.addBody( crateB, { density=1.4, friction=0.3, bounce=0.2} ) else crateC = display.newImage("crateC.png"); crateC.x = 60 + math.random( 160 ) crateC.y = -100 physics.addBody( crateC, { density=0.3, friction=0.2, bounce=0.5} ) end end local dropCrates = timer.performWithDelay( 500, newCrate, 1000 )

local random =math.random(100)

so what is it…is it like saying to corona that screen height is 100 and when there is value less or equal to 60 then u will launch one crate(small one) or what its really confusing and sorry corona for the last time but please answer for the real question this time and dont say to self search as i am done with a lot and is unable to find anything… :slight_smile:

The timer function will fire ‘newCrate’ function every 1/2 second for 1000 times.  Each time the function ‘newCrate’ is called, a random value from 1-100 will be picked.  It has NOTHING to do with the height of the screen.  It is like rolling a dice with 100 numbers on it (if there really was such a dice).

Once the random number is picked, when that random value is 60 or less basic crate is dropped, between 61-79 the crateB is dropped, and anytime the random value is 80-100 it will drop crateC.

When the crates (no matter which crate), it will be created ‘above the screen’ (that is -100 on the y axis), and it will always be aligned at least 60 pixels PLUS the random amount of pixels of (1-160) from the left edge of the screen.  As the screen width is likely going to be more then 220 pixels wide the crates will always be within the frame of the screen.

So the main reason for the random = math.random(100) is to mix up ‘randomly’ which of the 3 crate types will drop each time.  The 100 is just a value the developer picked.  Developer could use 50 for that random range value.  But would have to change the if-elseif-else block to read more like this :

if (random < 25) then

 …  drop crate.png

elseif (random < 40) then

 …  drop crateB.png

else

 …  drop crateC

end

hope this helps.

Bob

THANK YOU VERY MUCH BRO!..IF YOU DONT HAVE ANY PROBLEM THEN PLEASE MESSAGE ME YOUR FACEBOOK ID SO THAT I CAN CLEAR MY DOUBTS(IF ANY)…

Just want to add that you can find documentation (including examples) on all Corona’s api’s here: https://docs.coronalabs.com/daily. The documentation for math.random for instance:  https://docs.coronalabs.com/daily/api/library/math/random.html

It also includes examples of using it, along with showing you the output of the api call.

The documentation should be your first target, when attempting to understand how an api works.

Thanks

Ya thanx but there english level are higher so its bit difficult for me to go forward reading them!!! but yeah once again thanx

The timer function will fire ‘newCrate’ function every 1/2 second for 1000 times.  Each time the function ‘newCrate’ is called, a random value from 1-100 will be picked.  It has NOTHING to do with the height of the screen.  It is like rolling a dice with 100 numbers on it (if there really was such a dice).

Once the random number is picked, when that random value is 60 or less basic crate is dropped, between 61-79 the crateB is dropped, and anytime the random value is 80-100 it will drop crateC.

When the crates (no matter which crate), it will be created ‘above the screen’ (that is -100 on the y axis), and it will always be aligned at least 60 pixels PLUS the random amount of pixels of (1-160) from the left edge of the screen.  As the screen width is likely going to be more then 220 pixels wide the crates will always be within the frame of the screen.

So the main reason for the random = math.random(100) is to mix up ‘randomly’ which of the 3 crate types will drop each time.  The 100 is just a value the developer picked.  Developer could use 50 for that random range value.  But would have to change the if-elseif-else block to read more like this :

if (random < 25) then

 …  drop crate.png

elseif (random < 40) then

 …  drop crateB.png

else

 …  drop crateC

end

hope this helps.

Bob

THANK YOU VERY MUCH BRO!..IF YOU DONT HAVE ANY PROBLEM THEN PLEASE MESSAGE ME YOUR FACEBOOK ID SO THAT I CAN CLEAR MY DOUBTS(IF ANY)…

Just want to add that you can find documentation (including examples) on all Corona’s api’s here: https://docs.coronalabs.com/daily. The documentation for math.random for instance:  https://docs.coronalabs.com/daily/api/library/math/random.html

It also includes examples of using it, along with showing you the output of the api call.

The documentation should be your first target, when attempting to understand how an api works.

Thanks

Ya thanx but there english level are higher so its bit difficult for me to go forward reading them!!! but yeah once again thanx