Hey everyone,
I noticed that a lot of the random movement tutorials on this site and others used code that would scare many beginner flashers. At the expense of optimization, I tried to create a more simple, yet working, random movement animation using only simple ActionScript. A demo can be found here:
[size=4][color=red]http://www.kirupa.com/temp/zz2.html[/color][/size]
I avoided a lot of the more complicated AS procedures - partially because I did not want the upcoming tutorial to scare beginners, and partially because I don’t fully understand the complicated AS things
The file size of the SWF is only about 640 bytes, and the 25 circles did not tax the CPU resources. The percentage of CPU power used was about 0% on my comp.
Here is the code I used. The code can be applied to any movieclip:
onClipEvent (load) {
//data you may want to change
width = 300;
height = 200;
speed = Math.round(Math.random()*2)+1;
this._alpha = Math.round(Math.random()*80)+20;
this._xscale = this._yscale=Math.round(Math.random()*80)+20;
//initial positions
x = this._x=Math.random()*width;
y = this._y=Math.random()*height;
x_new = Math.random()*width;
y_new = Math.random()*height;
}
onClipEvent (enterFrame) {
//x movement
if (x_new>this._x) {
dir_x = "positive";
sign_x = 1;
} else {
dir_x = "negative";
sign_x = -1;
}
dx = Math.abs(x_new-this._x);
if ((dx>speed) || (dx<-speed)) {
this._x += sign_x*speed;
} else {
x_new = Math.random()*width;
}
//y movement
if (y_new>this._y) {
dir_y = "positive";
sign_y = 1;
} else {
dir_y = "negative";
sign_y = -1;
}
dy = Math.abs(y_new-this._y);
if ((dy>speed) || (dy<-speed)) {
this._y += sign_y*speed;
} else {
y_new = Math.random()*height;
}
}
The first frame contains a duplicate movie section of code that duplicates the above movie clip x number of times. You can search the site for examples of how that was done.
Hope you all like it A tutorial on this should go up soon. I will probably go back and fix any of the logic errors that occur with some of the if statements and || determiners.
Cheers!
Kirupa :ub: