sign up!

Are you interested in learning, then sign up to be notified about new tutorials.

Email:


helpfull links


website solutions

Do you need a website?
We can have you online in 24 hrs for less then $100


master flash

Are you looking to learn flash? If so you need to check out Flash, the Information you need. You will find all of these tutorials, source files and more...

Get your copy today!

Flash, This Information you need

a few friends


follow Us On

FrenchSquared on Linked in

FrenchSquared on Twitter

Frenchsquared on Facebook



Flash Tutorial, Random Numbers

Flash has built in methods that can generate random numbers. Math.random() will generate a random number similar to 0.3048873310908675. There isn’t much you can do with such a number so if you add Math.floor() to the random method flash will generate the number 0. The Math.floor method rounds the number inside the parentheses to the lowest whole number, and since math random only generates a number less then zero, the number produced will always be zero. Flash is prepared for this. Yet, there is one more thing that needs to be done. Multiply the random number by a whole number. So, the new equation should resemble Math.floor(Math.random() * 5). This equation will generate a random number between 0 and 4, if you want 1 -5 you simple add 1 to the end of the equation. The multiplier is the key, if you want to generate a random number between 1 and 99 you would simple change the 5 to 99 and add 1 to the end of the equation.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Using Math.random()

open the Start01 file and select the first key frame on the actions layer and press F9 to open the Actions Panel and add the example code. This code is creating 4 variables r1 - r4, then Flash is setting each one of the four variables to be equal to a whole number ranging from 0 - 9.

var r1 = Math.floor(Math.random() * 9)
var r2 = Math.floor(Math.random() * 9)
var r3 = Math.floor(Math.random() * 9)
var r4 = Math.floor(Math.random() * 9)

n the source file you will see that several text field have been created for you. If you need information on how to create a dynamic text field, please see the Flash CS3 Tutorials in the text section of this web site. Four of the text field have been named r1_txt - r4_txt. The four lines of code in the example simply place the random number created above into the four text fields.

r1_txt.text=r1
r2_txt.text=r2
r3_txt.text=r3
r4_txt.text=r4

Something needs to be done with the random numbers. This next section of example code simply creates two more variables a1 and a2. Then a1 is set to equal the sum of random number 1 and random number 2. Flash can work simple math problems the same as any calculator. Typing 1 + 2 will result in three. So, that means the equation to add two of the numbers together and the equation to subtract two of the numbers has been created. The only thing left to do is to display the answers of the two equations.

var a1 = (r1 + r2)
var a2 = (r3 - r4)

The last example code places the a1 and a2 inside the text fields a1_txt and a2_txt.

a1_txt.text=a1
a2_txt.text=a2

Press Control-Enter and test your movie.

Download Tutorials