Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com�s entire library of video tutorials.
You'll often want to introduce randomness to your Flash games and design effects. Accomplish this with a random number generator script. This script will no doubt utilize the math class's math.random function. I have an example of that in this file, the Random underscore Integer.fla file. Open that up. Click on the first keyframe there in the Actions Layer, open up the Actions Panel. You'll see my math.random function right here. This random function creates random numbers between zero and one including zero but not interestingly enough including one. You can then multiply or modify this number to get any span of numbers needed. In this case I'm multiplying the number generated by five and then adding one to generate an integer. You'll use the math.round function right here. We've seen that before. That will round off any decimal places. I'm going to go ahead and trace this variable; my number in the output panel. Go ahead and click on Control Test Movie and you'll get a random number here. That's four. We do it again, it'll be a different number. It'll be an integer since I'm rounding it. There's three. And continuing to do this will give me different values. There's six. So this construct here will give me a random number between one and six. If I change the five to a nine, that'll give me a random number between one and ten. If I choose the Control Enter Key, I can output this and test it multiple times. There's the number ten, seven, five. So that gives you an idea of how you would then multiply your random number by any series of numbers to then get a span of random numbers. To establish a random number between a given range, set up a variable representing the high value and a variable setting up the low value and then manipulate the random numbers to fall between those variables. If you open up the Random underscore Number underscore Range.fla file, open up the script in Frame 1 of the Action Layer, you'll see a script that does just this. So you'll generate a random number and then you'll return a random number between a specified range. Also this will throw an error if the low or high value is not provided so here we'll create a function, a random number function. We'll set up some arguments here; low value, high value needs to be a number. Then we'll set up our variables; low number variable, high number variable. If there's no low number variable defined, you'll throw an error. Low must be defined. Likewise, if there's no high number specified, it'll throw an error. High number must be defined. Then we'll go ahead and return our math random function. That will create a random number between 0 and 1 but not including 1. This math floor function here, that will round down to the lowest nearest integer. So you're going to round down whatever number you get here. Then you're going to multiply that by 1 plus the high minus the low plus the low number that'll give you that range. This is probably the most important part of it right here in terms of using this script. I'm going to generate a random number between 1 and 100. Notice that there is my low and high values. Let's go ahead and forget to post a low value. We'll only post one variable here in this argument and if we do a Control Test Movie, it'll throw this error. High must be defined. Let's go back and put in 90. So we're going to do a random number between 90 and 100 and test the movie and the value comes up 91, which is between 90 and 100. Let's try between 95 and 100. And gives me 95. Do it one more time just to make sure this is working correctly; 96. So it is working, giving me a random number in between the range specified in this random number parameter right there. So you can set up this part of the script. This is a function right here that will generate the random number and then calling the function is very simple. Just call it here with the function name, random number, making sure that you provide the two parameters that are required in the structure here. Now, be aware when to use decimals and when to use integers when generating your random numbers. Many properties, such as the X and Y locations of movie clips and other objects take decimal values or integers. But many others, such as the go to and play method require integers only. In other words, it's not possible to go to and play Frame 1.5. In this case you need to convert your numbers to an integer before using them in your scripts. We're now ready to investigate one of the most important scripts for the type of games that I've been showing and that is controlling objects, the directional movements of objects using the Arrow Keys, which will be the topic and the scripts reviewed in the next movie.
| Course: | Adobe Flash ActionScript 3.0 for Designers |
| Author: | James Gonzalez |
| SKU: | 34060 |
| ISBN: | 1-935320-82-3 |
| Release Date: | 2009-11-09 |
| Duration: | 9.5 hrs / 101 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |