Detecting the Mouse Cursor Position

[ LiB ]

One of the greatest things about Flash is its ability to use much of the operating system's resources and functionality. What I mean by this is that with simple commands, you can get information and perform tasks that programmers in other languages have to work much harder forincluding information about the mouse like the cursor's current on-screen position.

See Figure 9.1 for a visual explanation of a mouse cursor position.

Figure 9.1. Screen cursor positions

graphic/09fig01.gif


Figure 9.2 shows the relationship between the mouse on your desk and the position of the cursor on the screen.

Figure 9.2. Physical and on-screen positions

graphic/09fig02.gif


Why would anyone want to know exactly where the mouse cursor is? Just think of the possibilities. For example, imagine creating a skeet-shooting game in which the mouse cursor becomes a crosshair that the user can position over flying projectiles . The virtual rifle could then be fired by clicking the mouse, and if the cursor and the projectile line up closely enough, the projectile could explode to illustrate the impact. This is just one of the many examples of why you want to know exactly where the mouse cursor isotherwise games like these wouldn't be possible.

Figure 9.3 shows demo GDA_PROG9.1.fla, which I wrote for this chapter. It tracks the current cursor position and displays its location on the X and Y axes in a couple of text boxes.

Figure 9.3. Tracking the cursor's position

graphic/09fig03.gif


A big plus about the way that information is passed on from the operating system is that there is no setup. In other words, you don't have to write any "initialization" code to get mouse support up and runningit's always available for you to use! You can use built-in, property-like variables that the operating system constantly updates, allowing you to straighten your learning curve by skipping all the system specifics and low-level programming.

The mouse position variables are the following:

 _xmouse 

and

 _ymouse 

These properties are part of the Movie Clip class. They are the x and y pair of the current cursor position.

If you take a quick look at Figure 9.4, you'll see how I set up the two dynamic textboxes on the stage that I use to output the information.

Figure 9.4. Setting up dynamic textboxes for output

graphic/09fig04.gif


Let's jump right into the code, which was placed on the Actions layer of the main Timeline. See Listing 9.1.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // This program demonstrates how to // detect the cursor's position on // the _root movie. // Setup a frame handler so we // can check the mouse position // on every frame. onEnterFrame = function () {   // xcoord is the name of   // a dynamic textbox that   // is on the stage.   // _xmouse is being assigned   // on every frame, which in turn   // updates the screen's contents.   xcoord = _xmouse;   // ycoord also is the name of a   // dynamic textbox that is used to   // display the y coordinate of   // the mouse cursor.   ycoord = _ymouse; }; 

The first thing you'll notice is that I set up an onEnterFrame handler. This helps me track every frame. The second thing I did was assign _root._xmouse to xcoord. _xmouse is the built-in property that has the current mouse position and xcoord is the variable that's attached to the textbox on the stage. That being so, it makes sense to say that we are outputting the results by using this line:

 // xcoord is the name of // a dynamic textbox that // is on the stage. // _xmouse is being assigned   // on every frame, which in turn // updates the screen's contents. xcoord = _xmouse; 

NOTE

TIP

_root._xmouse and _root._ymouse will always return the cursor positions relative to the main movie's top-left corner. If you use another object, it will return values relative to the registration point [(0, 0)] of that Movie Clip.

The last thing I did here was assign the y value of the cursor's position to its respective dynamic textbox, ycoord .

 // ycoord also is the name of a // dynamic textbox that is use to // display the y coordinate of // the mouse cursor. ycoord = _ymouse; 

This causes _root._ymouse to be output to the dynamic textbox on stage. Simple, isn't it? It only gets better.

NOTE

NOTE

You might have noticed that sometimes I rewrite the same thing in a couple of different ways. I want you to get used to recognizing the code in all formats. Notice that when writing _ymouse on the main Timeline, it's the same thing as writing _root._ymouse or this._ymouse because you are still referencing the same object.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net