My Flash Resource – New Ideas and News for Applying Flash to Complex Gaming Apps

Latest News and Developments

May 25, 2008

For Beginners: Flash Random Number Creation

by @ 5:55 pm. Filed under Flash Tutorials

WebWasp.co.uk has a great “click by click” tutorial for beginners regarding how to generate random numbers. In the example provided on the website, a Flash button programmed by Action Script displays a random number between 1 and 10 in a predefined window space.

If you click the number button repeatedly, another random number appears.

Beginners will love this easy tutorial, which shows all the steps necessary to recreate the example .swf file offered by the authors.

Here are some code snippets from the tutorial:

Step …

5. If Script Assist is on, Switch it off:
6. Type (or copy and paste) the following code in the Actions Panel;
on (release) {
myDisplay = random (10) +1;
}

7. Test your Movie: Control > Test Movie (Ctrl Enter)

The ActionScript Explained:

on (release) {
When the user releases the Mouse button what is between the curly braces …

myDisplay = random (10) +1;
In this case the random function creates a random number that starts at 0 and ends on the 10th digit. Which is the number specified in the parentheses: (10). If you start your count from zero (instead of 1) then the 10th digit is 9 (not 10)! So to make it count form 1 to 10 I simply add one to the result. Hence the +1.

}
The curly brace ends the on (release) statement.

The next code snippet creates the non-repeating random number effect. This is slightly more complicated, but does come with a working example file:

The ActionScript attached to the button is as follows:

on (release) {

// Sets the number of digits in the random sequence:
var mySetting = 3;

// Creates a random number:
myNumber = random(mySetting)+1;

// Checks to see if it is repeating itself:
if (myNumber == myTemp) {
// If it is repeating it generates a new random number:
myNumber = random(mySetting)+1;
// Continues as above:
if (myNumber == myTemp) {
myNumber = random(mySetting)+1;
if (myNumber == myTemp) {
myNumber = random(mySetting)+1;
if (myNumber == myTemp) {
myNumber = random(mySetting)+1;
}
}
}
}
// Displays the random number:
myDisplay = myNumber;
// Resets the variable to the new random number:
myTemp = myNumber;
}

Finally, after you’ve come this far, the tutorial concludes with a practical application of Flash Randon Number creation: Subbing the random numbers for 10 different instances of “Hello” in different languages.

Very Cool – It’s all here: Click by Click Flash Random Numbers

Comments are closed.

My Flash Resource - A continually growing collection of tutorials, thoughts and interests of a Flash Animator and/or Developer.

categories:

other:

New Feedburner Feed!


Feel free to syndicate.

Learning Material

Flash Resources:


Listed on BlogShares
  • Resources