Ask a Question related to Macromedia Flash Actionscript, Design and Development.
-
JackFoley3215 #1
mouse avoidance help
I'm trying to make a movieclip avoid my mouse, like on this
site:[url]http://exopolis.com[/url]. 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.
JackFoley3215 Guest
-
Help with Mouse Within and Mouse Leave problem...
I have a shockwave 3d file of a building with individual floors split up into seperate objects. I want a pop-up information box (set at a MARKER)... -
Move mouse cursor to X,Y location and simulate mouse click
Hi, Just something that slip my mind.. but how do you move the mouse cursor to a sprite member and simulate a mouse click? Any help is... -
Mouse wheel down scrolls down normal, mouse wheel up goes to the top!?
I have this same problem with a Microsoft Intellimouse optical, even on XP. I installed all the new drivers for it, and it still happens. This must... -
ps/2 mouse is not loaded by the kernel (neither is usb mouse)
I've been looking all over the net trying trying to solve this problem, I found a few posts where people had the same problem, but no solution. ... -
Fading alpha on mouse over/mouse off
Hi, Wondering if someone could take a minute to help a flash newbie: I'm trying to create a set of images which fade to 1% alpha when the mouse is... -
stwingy #2
Re: mouse avoidance help
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]
stwingy Guest
-
JackFoley3215 #3
Re: mouse avoidance help
Thanks for your help that worked, but I tried applying the code so I could have four movieclips moving around independent of one another and I couldn't get it to work. Any ideas? Thanks!
JackFoley3215 Guest
-
stwingy #4
Re: mouse avoidance help
Is it something like this you are wanting? [L=http://www.gifsrus.com/testfile/mouserepel2.swf]http://www.gifsrus.com/testfile/mouserepel2.swf[/L]
stwingy Guest
-
JackFoley3215 #5
Re: mouse avoidance help
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!
JackFoley3215 Guest
-
stwingy #6
Re: mouse avoidance help
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 Guest
-
stwingy #7
Re: mouse avoidance help
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;
}
}
}
stwingy Guest
-
JackFoley3215 #8
Re: mouse avoidance help
It works! Except I don't want the stopping point to be random everytime. I want to specifiy where each spider will stop, and on which box. Where do I do this in the code? Thanks!
JackFoley3215 Guest
-
stwingy #9
Re: mouse avoidance help
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{}"
stwingy Guest



Reply With Quote

