A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Follow Mouse

  1. #1
    Junior Member
    Join Date
    Oct 2003
    Location
    Raleigh, NC USA
    Posts
    7

    Follow Mouse

    I need an example of code for CS3 AS3. I want to have a mc follow my mouse around the stage. Thanks!
    Jay Diegar
    http://www.Diegar.com - Animated Chaos!

  2. #2
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    One way:

    PHP Code:
    package {
        
    import flash.display.MovieClip
        import flash
    .events.MouseEvent
        
        
    public class FollowDoc extends MovieClip {
                              
    //MyMC linked in the library:
            
    private var theMC:MyMC;

            public function 
    FollowDoc():void {
                
    theMC = new MyMC();
                
    addChild(theMC)
                
    stage.addEventListener(MouseEvent.MOUSE_MOVEonMove);
            }
            private function 
    onMove(me:MouseEvent):void {
                
    theMC.this.mouseX;
                
    theMC.this.mouseY;
            }
        }

    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

  3. #3
    Senior Member
    Join Date
    Jul 2009
    Posts
    113
    Here is another approach. It assumes that the cursor symbol is already on the stage with the instance name "cursorInstanceName".

    Following code goes into a frame.

    Code:
    stage.addEventListener(MouseEvent.MOUSE_MOVE, followMouse);
    
    function followMouse(evt:MouseEvent):void {
    	cursorInstanceName.x = mouseX;
    	cursorInstanceName.y = mouseY;
    }

  4. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    If you doesn't want your mouse cursor to show during this, you can use:

    Mouse.hide();

    But then you have to import flash.ui.Mouse;

  5. #5
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    With these sort of mouse-dependant events, I also like using evt.updateAfterEvent(). Keeps things nice and close.
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help 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