I want to alter some code within this tutorial from kirupa:
http://www.kirupa.com/developer/actionscript/random_motion.htm
I’ll only have one movieclip flying around the stage, but I need to restrict it from entering a certain area.
My stage is 600x450 and in the middle of the stage I want to put a text area that is approx 450x250 and I’ll populate this with text from xml doc. But basically I don’t want my flying object to go into the text area… so it either bounces off of or swerves away from it when it gets near it.
I’ll probably make the boundaries larger than the stage so my object flys out of view every now and then.
Here is the code from the above tutorial:
onClipEvent (load) {
//data you may want to change
width = 700;
height = 550;
speed = Math.round(Math.random()*2)+1;
//initial positions
x = Math.random()*width;
y = Math.random()*height;
this._x = x;
this._y = y;
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;
}
}
I’d really appreciate any help with this.
Thanks
Mark