Groups | Blog | Home
all groups > flash actionscript > march 2004 >

flash actionscript : mouse avoidance help


JackFoley3215
3/3/2004 8:17:43 PM
I'm trying to make a movieclip avoid my mouse, like on this
site:http://exopolis.com. I want the movieclip to never leave a defined area
(700 x 525). I'm having a little trouble getting it to work. Here's my code:

onClipEvent (enterFrame) {
if (_root._xmouse<700 && _root._xmouse>0) {
//x movement
mx = _root._xmouse;
if (mx<_x) {
dx = _x-mx;
} else {
dx = mx-_x;
}
moveSpeedx = dx/30;
if (mx>_x) {
_x = _x-moveSpeedx;
} else {
_x = _x+moveSpeedx;
}
}
if (_root._ymouse<525 && _root._ymouse>0) {
//y movement
my = _root._ymouse;
if (my>_y) {
dy = _y+my;
} else {
dy = my+_y;
}
moveSpeedy = dy/30;
if (my<_y) {
_y = _y+moveSpeedy;
} else {
_y = _y-moveSpeedy;
}
}
}

Right now the mouse is still leaving the defined area, and it slows down when
my mouse gets closer rather. Any help would be appreciated.
stwingy
3/3/2004 8:33:30 PM
I`ve not visited the site which you have mentioned, but see if this is of any
use to you

[L=http://www.gifsrus.com/testfile/mouserepel.swf]http://www.gifsrus.com/testfil
e/mouserepel.swf[/L]
source

[L=http://www.gifsrus.com/testfile/mouserepel.fla]http://www.gifsrus.com/testfil
e/mouserepel.fla[/L]
JackFoley3215
3/4/2004 7:57:25 PM
stwingy
3/4/2004 8:43:53 PM
JackFoley3215
3/4/2004 9:23:09 PM
Hi.

Yeah that's what I was trying to do. You wouldn't also know how to have each
spider stop once it reaches a certain position? For example once you push
spider 1 to x position 200 and y position 200 it would stop moving, spider 2
would stop moving once it reached a different position, etc.

Thanks!
stwingy
3/4/2004 10:17:11 PM
with the file you already have, delete the instance of the spider(b1) and in
the library give it a linkage identifier of b1(poorly chosen by me). Create
another movieclip and draw a square or something just so you can see it for the
time being. Give this a linkage identifier of mc and make sure there are no
instances of it on the stage. Then change the code to this.

for (i=0; i<5; i++) {
b1 = attachMovie("b1", "b1"+i, i);
m1 = attachMovie("mc", "mc"+i, i+10);
m1._x = 300;
m1._y = random(400);
b1._x = random(600);
b1._y = random(400);
b1.ivar = i;
b1.onEnterFrame = move;
}
_root.onEnterFrame = function() {
for (i=0; i<5; i++) {
if
(_root["b1"+i].hitTest(_root["mc"+_root["b1"+i].ivar]._x,_root["mc"+_root["b1"+i
].ivar]._y,true)) {
_root["b1"+i].vx = 0;
_root["b1"+i].vy = 0;
} else {
minD = 100+(25*i);
k = .04+.02*i;
dx = _root["b1"+i]._x-_xmouse;
dy = _root["b1"+i]._y-_ymouse;
dist = Math.sqrt(dx*dx+dy*dy);
if (dist<minD) {
angle = Math.atan2(dy, dx);
ty = _ymouse-Math.sin(angle)*minD;
tx = _xmouse-Math.cos(angle)*minD;
ax = (tx-_root["b1"+i]._x)*k;
ay = (ty-_root["b1"+i]._y)*k;
vx -= ax;
vy -= ay;
_root["b1"+i]._x += vx;
_root["b1"+i]._y += vy;
}
}
}
};
function move() {
trace(this.ivar);
if (this.hitTest(_root["mc"+this.ivar])) {
delete (this.onEnterFrame);
} else {
var damp = .1;
vx *= damp;
vy *= damp;
this._x += this.vx;
this._y += this.vy;
if (this._x>844) {
this._x = 844;
this.vx *= -.2;
} else if (this._x<2) {
this._x = 2;
this.vx *= -.2;
}
if (this._y<55) {
this._y = 55;
this.vy *= -.2;
} else if (this._y>576) {
this._y = 576;
this.vy *= -.2;
}
}
}

//once you have seen how it works, you can delete mc`s pixels.

stwingy
3/4/2004 10:36:08 PM
slight change of the code to
for (i=0; i<5; i++) {
b1 = attachMovie("b1", "b1"+i, i);
m1 = attachMovie("mc", "mc"+i, i+10);
m1._x = random(800);
m1._y = random(400);
b1._x = random(600);
b1._y = random(400);
b1.ivar = i;
b1.onEnterFrame = move;
}
_root.onEnterFrame = function() {
for (i=0; i<5; i++) {
if (_root["b1"+i].hitTest(_root["mc"+_root["b1"+i].ivar])) {
_root["b1"+i].vx = 0;
_root["b1"+i].vy = 0;
} else {
minD = 100+(25*i);
k = .04+.02*i;
dx = _root["b1"+i]._x-_xmouse;
dy = _root["b1"+i]._y-_ymouse;
dist = Math.sqrt(dx*dx+dy*dy);
if (dist<minD) {
angle = Math.atan2(dy, dx);
ty = _ymouse-Math.sin(angle)*minD;
tx = _xmouse-Math.cos(angle)*minD;
ax = (tx-_root["b1"+i]._x)*k;
ay = (ty-_root["b1"+i]._y)*k;
vx -= ax;
vy -= ay;
_root["b1"+i]._x += vx;
_root["b1"+i]._y += vy;
}
}
}
};
function move() {
trace(this.ivar)
if (this.hitTest(_root["mc"+this.ivar])) {

delete (this.onEnterFrame);
} else {
var damp = .1;
vx *= damp;
vy *= damp;
this._x += this.vx;
this._y += this.vy;
if (this._x>844) {
this._x = 844;
this.vx *= -.2;
} else if (this._x<2) {
this._x = 2;
this.vx *= -.2;
}
if (this._y<55) {
this._y = 55;
this.vy *= -.2;
} else if (this._y>576) {
this._y = 576;
this.vy *= -.2;
}
}
}

JackFoley3215
3/5/2004 12:26:21 AM
stwingy
3/5/2004 6:01:00 AM
delete the lines
m1._x = random(800);
m1._y = random(400);
then you can replace with what you want
eg/
m1._x = 50+(100*i);
m1._y = 300;
or just type in seperately for each mc
eg/
_root["mc"+0]._x = 500
_root["mc"+0]._y = 300
//maybe make use of "with{}"
AddThis Social Bookmark Button