The _xmouse and _ymouse Properties


Now that we understand the concept of reference variables , we can talk about another important pair of properties: _xmouse and _ymouse . They indicate the distance from the movie clip's registration point to the mouse pointer. These properties cannot be set. (You can try, but nothing will happen.) They are commonly used to make objects move about or react to changes in the user 's mouse position.

The tricky thing about _xmouse and _ymouse is that they indicate a relative distance. In other words, they are relative to the clip you are referencing. Let's have an example to clarify. Attach the following code to the ball instance on the stage:

 onClipEvent(enterFrame){     trace("x-position of mouse:" + _xmouse);     trace("y-position of mouse:" + _ymouse); } 

The _xmouse and _ymouse properties are used without a reference, so the implication is that they are properties of the locally scoped clip (the ball instance). Move your mouse slowly around the screen and watch the values change as in Figure 3.15.

click to expand
Figure 3.15: The _xmouse and _ymouse properties indicate the position of the mouse pointer relative to the movie clip to which they are attached.

Try to get _xmouse and _ymouse as close to (0,0) as you can, and you will notice that your mouse is above the center of your ball instance. That's because you are accessing the ball's _xmouse and _ymouse properties, so they tell you position of the mouse relative to the ball clip.

Sometimes this is what you want, but often it's not. In many cases, you want the position of the mouse pointer relative to the stage. In that case, you would want to use the _root reference to refer to the main movie timeline, as in the following example:

Change the code attached to the ball instance to the following:

 onClipEvent(enterFrame){     trace("x-position of mouse:" + _root._xmouse);     trace("y-position of mouse:" + _root._ymouse); } 

Notice how the output, as shown in Figure 3.16, shows the mouse position relative to the stage coordinates now that we're using the _root reference instead of the this reference.

click to expand
Figure 3.16: The _root._xmouse and _root._ymouse properties indicate the position of the mouse pointer relative to the stage.



Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

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