Professional Web Applications Themes

mouse avoidance help - Macromedia Flash Actionscript

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 { ...

  1. #1

    Default 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

  2. #2

    Default 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

  3. #3

    Default 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

  4. #4

    Default 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

  5. #5

    Default 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

  6. #6

    Default 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

  7. #7

    Default 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

  8. #8

    Default 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

  9. #9

    Default 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

Similar Threads

  1. Help with Mouse Within and Mouse Leave problem...
    By Khanh75 webforumsuser@macromedia.com in forum Macromedia Director 3D
    Replies: 2
    Last Post: December 3rd, 08:22 PM
  2. Move mouse cursor to X,Y location and simulate mouse click
    By WZSun in forum Macromedia Director Lingo
    Replies: 1
    Last Post: August 6th, 10:17 AM
  3. Mouse wheel down scrolls down normal, mouse wheel up goes to the top!?
    By kmatrix webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 9th, 04:36 PM
  4. Replies: 0
    Last Post: June 29th, 02:30 PM
  5. Fading alpha on mouse over/mouse off
    By Fry in forum Macromedia Flash
    Replies: 2
    Last Post: June 29th, 10:46 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139