Randoms

You guessed it, an old one.

FrozenHaddock Tutorials: Randoms

In this tutorial, you will be learning how to randomise certain things in flash.
This can be useful for randomising questions in a quiz, or what sort of powerup
is going to pop up in a game. We are going to look at randomising variables and
randomising which frame to go to.

Randomising Variables-

Ok, so first off lets create our dynamic text box, for more information on
dynamic text boxes see basic variables.

textboxdynamicvar:

Make sure you set your var: to ‘myvariable’, case sensitive. Now you want to open up
the actions panel for your frame. To do this, select the frame in the timeline, and click the
actions bar or hit F9.

frame1

actionspanel

Now, we’re going to put the following code in.

code1

myvariable=random(10);

This small piece of code sets our variable, displayed in the dynamic text box, to a random
number between 1 and 10. Now lets try something differant.

code2

myvariable=random(10)+5;

With this code on the frame, myvariable is set between 5 and 15. This is because, flash
does the random number between 1 and 10, then adds 5. Therefore the smallest
outcome is 0+5=5. The largest is 10+5=15. You can use this to randomise between any 2
numbers, for example here is the code for -5 to 5.

code3

myvariable=random(10)-5;

In this instance, the smallest outcome is 0-5=-5. The largest is 10-5=5. Try this out with
differant numbers, and practice getting randoms between 2 numbers. Now lets move on
to randomising frames.

Randomising Frames-

The best way to show you how to do this is with an example, so open up a new flash
document. Now insert keyframes up to frame 5, and put the frame number on each frame.

frames

5

Now, open up the frame actions for frame one, and enter the code shown below.

frame1
code5

gotoAndStop(random(5));

This code sends the user to a random frame between frame 1 and 5. Go on, try it. Test
your movie (CTRL+Enter).

You can use the same sort of code as in the randomising variables to achieve differant
ranges of frames.

gotoAndStop(random(5)+5);

For example, this code will send the user to a frame between 5 and 10. Play around with
the numbers and see what ranges you can get.

Thats all for this tutorial, remember random frames and variables can be very useful when
combined with ‘if’ statements, among other things. Use them in games to stop your game
getting repetitive and boring.

-->

Leave a Reply