TRACKING THE CURSOR POSITION


Tracking the cursor position in Flash 4 can be accomplished with DragMovieClip function.  

General technique:
 

The cursor position can be obtained by dragging an invisible object and getting the position of the object.  It's simple but it's rather tricky to implement.  

(You might ask, "Why on earth ... there's no built in function to get the cursor position in Flash 4?"  Oops, I guess not.  If you think this makes things unnecessarily complicated, I agree.)   

Step by step guide:

  1. Create a new FLA (for this illustration purpose, use 300x200 size).
  2. Insert/Create a new Symbol, and call the new Symbol: CursorTracker.  Set behavior to MovieClip. (This will be a blank symbol so no need to worry that the symbol has nothing on it.)
  3. On the first frame of the CursorTracker, insert the following Action: 
    DragMovieClip("", lockcenter)
    .  This will cause the CursorTracker to be dragged when the movie starts.
  4. The second and third frame of CursorTracker will be a loop.  The loop will continuously get the mouse position.  To accomplish that, do the following: 
    On the second frame insert the following action 
    Set Variable: "cusorX" = GetProperty ("",_x) 
    Set Variable: "cusorY" = GetProperty ("",_y)
    On the third frame, insert the following action:  
    Go to and Play(2)
  5. That's it, you can then drag an instance of CursorTracker into your movie and whenever you need to get the cursor position, the (x,y) value is available at  "CursorTracker:cursorX" and "CursorTracker:cursorY" variables.

Example:

Below is an example to show how it works.  You can also download the FLA file.  The CursorTracker symbol can be re-used independently in other movie by dropping it into the main timeline.

<IMG SRC="cursorTracker.gif" WIDTH=300 HEIGHT=200 usemap="#cursorTracker" BORDER=0>
FLA

Additional Notes:

There are some limitations that you should be aware of when using this method.  First, the object that tracks the cursor position must always be dragged.  That means that you cannot drag other object while tracking the cursor position.  It is possible to crate a workaround  by using any dragged object to track the mouse position.  For example, when you drag Object1, you should get the cursor position from Object1, and when you switch to Object2, you should get the cursor position from Object2, etc.  But frankly, that will be a pretty horrible task; and it is also very impractical.  Hope Flash 5 will include a built in function to obtain the cursor position.   


December 1999
(C) F. Permadi 

<<INDEX>>