Random Numbers in Flash

In this tutorial, I'm going to say one or two things about random( ) function. This is a very useful function that you will most likely use in your games or some other applications. Before we examine this function in-depth, let me show you the basic code:

random(value:Number) : Number

So, what exactly does this function do? If you have a little bit of experience in programming, you probably already know. If, however, you're new to the exciting World of variables, functions and classes, you might have doubts. 

This function returns a random number from 0 to "value"−1. It means that if I were to type "random(3)", the function would return 0, 1, or 2.

Now, this is theory. Let's move on to what we might call an interesting exercise.


The game

Why not make a simple "game" using the function? As mentioned, one of this function's main applications are games-related.

So, what will the game be about? Oh, it's going to be a very simple game: If you roll your mouse over an object, it jumps to a randomly picked new spot.

 

 


How this is done

i. Create a new Flash document, 400x600, 30fps.

ii. Let's draw a circle (with Oval Tool), select it and press F8. A window should pop up that will enable you to convert the circle to a Button. You can then name it "button". It is necessary that you make it a Button, and not a Movieclip, because this is the only way this method will work.



iii. Press Ctrl + F3 to make the Properties window appear, and then click once on the button you have created to select it.

If you have done that, there should be an empty white field visible in the bottom-left corner of the screen, just under "Button". 

Let's type "circle" there. This is going to be the instance name of our button. We need it to address the Button through Actionscript.

iv. Now, select the circle, press F9 and copy (Ctrl + C) and paste (Ctrl + V) the following code into the Actionscript window that you should now see:

on(rollOver){
circle._x = random(250)
circle._y = random(250)
}


That's it. You've just created a simple a game!


How it works

on(rollOver){

We say what will evoke the actions that are between curly brackets ( { } ). In our case, it is:

circle._x = random(250)
circle._y = random(250)

"circle" is, as we know, the instance name of our button. It is some kind of a name that we have given to this object, and that we can use to refer to it in Actionscript.

The dot ( . ) and underscore ( _ ) is necassary to refer to an object's property.

Note that there are many object properties. For example:

  • Alpha—in other words "transparancy". For example, circle._alpha = 30
  • Rotation—for example,  circle._rotation = 40 
  • X scale—for example, circle._xscale = 25


These are just a few of many.


So! This is it! You should now be able to use random( ) function.

Hope you've enjoyed this tutorial and managed to understand the topic well. If you feel like, why not have a glance and other tutorials?

Cheers!


Average: 2.3 (61 votes)