Generating Random Movement in Flash using ActionScript
By Blue_Chi | Flash | BeginnerIn this tutorial you will learn how to create a random movement in Flash using ActionScript, the end result should look like this:
1- Start by creating a new Flash file, set the dimentions to 550 x 100. Make the movie run at 30 frames per second.
2- Draw a vertical line with the same hight of the flash movie. You can put it anywhere horizontally as long as iti s vertically at the centre.
3- Select the line and then press F8 to convert it to a movie clip symbol. The name does not matter.
4- Select the line symbol in the main time line and then access ActionsScript panel. You have to make sure you are setting the Actions of the movie clip and not the frame of the main timeline. In the ActionScript window, paste the following code:
_x = random(551);
_alpha = random(101);
}
- Go to Control > Test Movie and it should work already.
- We need to explain to the code to be able to generate similar effects in other forms as well.
The first line of the code "onClipEven(enterFrame) {}" is necessary to excute the later actions, this tells the movie to excuet the actions every time a new frame is entered.
The second line _x = random (551); changes the value of the horizontal property of the line. The part after the equal marks is the new value set to the horizontal property, in this case it is a random number generated between 550 and 0.
If we want to change the _y property of the line we would have used the code _y = random (ANY NYMBER HERE), the different properties that we can change are vertical location _y, vertical scale _yscale, horizontal location _x, horizontal scale _x, transparency _alpha, and rotation _rotation.
On the same trend you can change any of the properties of objects with a numbered value, for example: _alpha (the transparency), _rotation (the rotation), _xscale (the horizontal scale), _yscale (the vertical scale), _x (the horizontal position), _y (the vertical position).
This is the end of the tutorial, for more help please visit the forum.