Skip navigation
mano_gold
Currently Being Moderated

if ... else mouse follow help.

May 14, 2012 1:20 AM

Tags: #mouse #3.0 #action #3 #action_script_3 #ac3 #actionscript3 #if #actionscript_3.0 #eventlistener #else #follow

if ... else mouse follow help.

 

Hi, I'm using this code for a movie to follow mouse and nest when it leaves the stage. The movie is a hand and I need it to be a left hand on the left of the stage then nest while the right hand becomes active on the other side. Any help would be gratefully appreciated.

 

var targetX:Number;

var targetY:Number;

 

var sp:Sprite = new Sprite();

with(sp.graphics){

    beginFill(0x000000);

    drawRect(0,0,stage.stageWidth,stage.stageHeight);

    endFill();

}

addChild(sp);

sp.alpha = 0;

sp.addEventListener(MouseEvent.MOUSE_MOVE,updateTargetF)

 

stage.addEventListener(Event.MOUSE_LEAVE,nestF);

function updateTargetF(e:MouseEvent) {

    targetX=mouseX;

    targetY=mouseY;

    trace(targetX);

}

 

function nestF(e:Event) {

    targetX=150;

    targetY=700;

}

 

stage.addEventListener(Event.ENTER_FRAME,followBall);

 

function followBall(event:Event):void {

    var dx:int = hand_mc.x - targetX;

    var dy:int = hand_mc.y - targetY;

    hand_mc.x -= dx / 5;

    hand_mc.y -= dy /5;

}

 
Replies
  • Currently Being Moderated
    May 14, 2012 4:28 AM   in reply to mano_gold

    Have you tried to do this yourself or did you just acquire the code you have so far and don't know how it works?  If you have tried this yourself, you should show what isn't working that you tried.

     
    |
    Mark as:
  • Currently Being Moderated
    May 14, 2012 4:54 AM   in reply to mano_gold

    Consider this your opportunity to learn some Actionscript and take a shot at solving it. 

     

    For starters, realize that you need to be in control of both hands at the same time, so both of them need to have their own "targetX" and "targetY" properties that you continuously direct them to - One gets directed to a fixed location (its nesting position) and the other follows the mouse.  So start off by having sets of code for each.

     

    Then think about what role mouseX/mouseY play in determining whether to have the left hand or the right hand following the mouse and create the code for it... at what values should the shift of control occur... add the code in and try things until it works.

     

    The MOUSE_LEAVE listener/handler role will need to be changed to apply nesting to both.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points