|
||||||
|
||||||
Flash Tutorial - Generating Random Numbers

Free
Flash Tutorial
Step Five: Converting the Random Number to Text
In general you do not want to create a random number for the sake of creating a random number. You create a random numbers to create some other random event. Once you have created a random number it is actually quite easy to convert it to something useful. In this example I convert the number into text. Each number is associated with a particular word (or words). In this case the word hello in 10 different languages:
My Example: Download the Flash file Int 039c
Click to say hello in 10 different languages.Note: If you can guess all 10 languages I will eat my computer (just joking) !!
The ActionScript in first section is identical to the previous example except that I have changed mySetting to 10 as I want to generate a random number between 1 and 10. The second converts the random number to text. I hope the gray comments make the code self explanatory:
Note for AS2: The code below will work in AS1 or AS2 but if you are using AS2 there is a shorter way to achieve the same thing. You can use an Array: See sample code
The ActionScript attached to the button is as follows:
on (release) {
// Sets the number of digits in the random sequence:
var mySetting = 10;
// Creates a random number:
myNumber = random(mySetting)+1;
//Checks the repeat function
while (myNumber == myTemp) {
myNumber = random(mySetting)+1;
}
// Resets the variable to the new random number:
myTemp = myNumber;
// This section converts the number to text.
// Each number is associated with a particular sentence.
// On this occasion my random number is from 1 to 10.
if (myNumber == 1) {
myDisplay = "Hello";
} else if (myNumber == 2) {
myDisplay = "Salut";
} else if (myNumber == 3) {
myDisplay = "Guten Tag";
} else if (myNumber == 4) {
myDisplay = "Hola";
} else if (myNumber == 5) {
myDisplay = "Dobrý Deň";
} else if (myNumber == 6) {
myDisplay = "Nei Ho";
} else if (myNumber == 7) {
myDisplay = "Ciao";
} else if (myNumber == 8) {
myDisplay = "God Dag";
} else if (myNumber == 9) {
myDisplay = "Namaste";
// I don't have to specify the last digit:
} else {
myDisplay = "Kia Ora";
}
}
That's it. Enjoy being random!
I hope you have found this useful. If so perhaps you could recommend this site to others and link to webwasp!
|
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
7466 visitors to this page since
2 July 07 •
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|