A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Avoid Mouse in Flash CS4/AS3

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    8

    Avoid Mouse in Flash CS4/AS3

    Hey.
    I've serched the forums over and over to find helpful threads on how to make my movieclip avoid the mouse. I have a movieclip, a square, which I would like to have moving around on stage, avoiding the mouse if you "chase" the movieclip.

    Unfortunately, all the threads I have found have been with Actionscript 1.0 or 2.0. I tried converting it to AS3, but failed.

    I dont have much code so far, just the basics:

    stage.addEventListener(Event.ENTER_FRAME, avoidTouch);

    function avoidTouch(event:Event):void {
    var distx: int = noTouch_mc.x - mouseX;
    var disty: int = noTouch_mc.y - mouseY;
    var afstand: int = ????;
    if(afstand < distx && afstand < disty){

    }
    }

    Im stuck. I want to make it calculate the distance from the mouse to the NoTouch_mc and if it gets below a certain number, make the noTouch_mc move away from the mouse.
    If it could be made so it never leaves stage, that I'd like to incorporate too, but wouoldnt I have to consider sinus and cosinus to an angle to make it turn?
    Too much math in this

    Anyone have any idea how to do this? Im not a flash/AS3 expert or anything close to that. Any help is much appreciated.

    (Just please dont link/refer to Actionscript 1.0 or 2.0. I simply just dont know how to convert it)

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The distance formula is
    Code:
    var distance:Number = Math.sqrt(distx*distx + disty*disty*);
    That's a simple application of the pythagorean theorem.

    You can make your threshold anything you like. Compare the calculated distance to your threshold and if it is less than the threshold, you'll want to impart some velocity to your clip, or you could simply move it threshold distance away from the mouse.

    Code:
    var thresh:Number = 10;
    addEventListener(Event.ENTER_FRAME, avoidTouch);
    
    function avoidTouch(event:Event):void {
      var distx: int = noTouch_mc.x - mouseX;
      var disty: int = noTouch_mc.y - mouseY;
      var dist:Number = Math.sqrt(distx*distx + disty*disty);
      if(dist < thresh){
        noTouch_mc.x = mouseX + thresh*distx/dist;
        noTouch_mc.y = mouseY + threst*disty/dist;
      }
    }

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Thank you. It makes much more sense than what I was thinking I had to do.

    But now I run into another problem. The movieclip does nothing now. It doesnt respond, nor does the mc move in any way.

    Code:

    Actionscript Code:
    stage.addEventListener(Event.ENTER_FRAME, avoidTouch);

    function avoidTouch(event:Event):void {
      var thresh:Number = 10;
      var distx: int = noTouch_mc.x - mouseX;
      var disty: int = noTouch_mc.y - mouseY;
      var dist:Number = Math.sqrt(distx*distx + disty*disty);
      if(dist < thresh){
        noTouch_mc.x = mouseX + thresh*distx/dist;
        noTouch_mc.y = mouseY + thresh*disty/dist;
      }
    }

    My movie clip is called "noTouch_mc".

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Although I did not test before posting that solution, I have just created a project for it and tested it. It works.

    Make sure your threshold is big enough. The clip will not move unless the mouse is within threshold of its registration point.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Strange. I did try to increase the threshold to 200, and nothing worked. Seems to work just fine now.

    Thank you very much!

    Is there any way to prevent the square from leaving the stage?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yeah, just check that after it's moved that it's still between 0 and stagewidth, and 0 and stageHeight. If not, put it back to the closest edge.

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    8
    Thank you

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center