rmedek
08-10-2008, 09:42 PM
I am using a script I found in a tutorial online to move an object around randomly inside the boundaries of a container. The script is applied to a movie clip (the object) in the only frame of the .fla file:
onClipEvent (load) {
//data you may want to change
width = 60;
height = 20;
speed = 1;
//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) {
sign_x = 1;
} else {
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) {
sign_y = 1;
} else {
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;
}
}
So far so good, but the trick is I'd like the object to move slower. The speed is controlled via the variable "speed," but anything lower than "1" makes the animation very jerky. I've tried adjusting the numbers along with the frame rate and I can't figure out a way to smooth out the animation. Any ideas?
onClipEvent (load) {
//data you may want to change
width = 60;
height = 20;
speed = 1;
//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) {
sign_x = 1;
} else {
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) {
sign_y = 1;
} else {
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;
}
}
So far so good, but the trick is I'd like the object to move slower. The speed is controlled via the variable "speed," but anything lower than "1" makes the animation very jerky. I've tried adjusting the numbers along with the frame rate and I can't figure out a way to smooth out the animation. Any ideas?