TUTORIALS
FORUMS
Flash
Basics / Animation
ActionScript
Special Effects
HTML5
CSS /
HTML Basics
Animation
JavaScript
Common Tasks
Windows Phone
Get Help!) Just as with the Flash 5 Random
Movement code, suprabeener was more than kind enough
to share his expertise in making the code work in Flash MX.
So, thank you supra!
The following animation is a result of
suprabeener's code and my infatuation with the post icons
found on kirupaforum.com.
[ randomly moving cones! ]
You will create something similar to the
above animation in about 5 minutes by following the
instructions in this tutorial.
Creating Random Motion in Flash MX:
[ the
convert to symbol dialog box ]
//special thanks to Suprabeener for the code
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() {
//specify the width and height of the movie
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;
};
[ copy and paste the following
code as an action on Frame 1 ]
onClipEvent(enterFrame){
move();
}
[ add the
above lines of code on your movie clip ]
[ multiple
copies of cone movie clip with the three lines of code ]
In the width field, change the value after width to
reflect your animation's width. Likewise, in the
height field, change the value after height to
reflect your animation's height:
The above note concluded this tutorial. If
you are interested in seeing how my animation in this
tutorial was created, click the download fla link