Random motion can be easily produced via actionscript. You can a sample of this effect on kirupa.com. This tutorial shows you how to reproduce this effect.
1. First of all, open flash. Make a new doc using the default dimensions, this won't matter since we wont be making anything entirely too complicated. The main thing we will be focusing on is actionscript. I used the dimensions : 300 X 200 You may use these if you prefer to do this exactly the way I did. Or if you want to use a different size, you will have to edit the script, which is explained at the bottom of this page.
2. The next thing we want to do is make an object in which we will be using for the "random motion" It can be anything you want. Preferably something small and simple. I used a square. After you create your object, convert it to a symbol.
3. Taking your newly made movieclip, Put this script in it:
| onClipEvent (enterFrame) {
move(); } |
(this script invokes the current clip and applies it to the script we are going to be making for the random motion)
4. Now we will be making the script that will be used to create the randomized motion. Pay attention so you understand how the script works. Place the script in frame 1. Get the script below :
| function getdistance(x, y, x1, y1) {
var run, rise; run = x1-x; rise = y1-y; return (_root.hyp(run, rise)); } function hyp(a, b) { return (Math.sqrt(a*a+b*b)); } MovieClip.prototype.reset = function() { width = 300; height = 200; var dist, norm; this.x = this._x; this.y = this._y; this.speed = Math.random()*4+2; this.targx = Math.random()*width; this.targy = Math.random()*height; dist = _root.getdistance(this.x, this.y, this.targx, this.targy); norm = this.speed/dist; this.diffx = (this.targx-this.x)*norm; this.diffy = (this.targy-this.y)*norm; }; MovieClip.prototype.move = function() { if (_root.getdistance(this.x, this.y, this.targx, this.targy)>this.speed) { this.x += this.diffx; this.y += this.diffy; } else { this.x = this.targx; this.y = this.targy; if (!this.t) { this.t = getTimer(); } if (getTimer()-this.t>1000) { this.reset(); this.t = 0; } } this._x = this.x; this._y = this.y; |
5. After you have finished with the application of the script, Duplicate the movieclip you made and place each item on different areas of the stage. This will show the randomized motion in action, making each instance move in a different direction and elapsement. After you have done this, You're done. Test the movie!
Get a copy of the finished fla here






