Replacing the Mouse Cursor

[ LiB ]

In the Windows operating system, the mouse cursor is normally an arrow. This is all fine and good for a spreadsheet, but game cursors need to be a lot more interesting! Wouldn't it be nice to replace that drab arrow with a crosshair, or perhaps a rusty blade ? Flash makes this easy. All you have to do is hide the real cursor and program a Movie Clip to follow your mouse cursor position properties. Doesn't sound too bad. Let's see what it takes to do it.

Let's talk about the setup of the mouse cursor: it must be specific. What do I mean by that? The registration point has to be in a certain place. Open the demo file GDA_PROG9.2.fla to see what I mean, then check out Figure 9.5.

Figure 9.5. Replacing the cursor

graphic/09fig05.gif


NOTE

TIP

When you create a Movie Clip, you have the option of selecting where its (0, 0) coordinates should be placed; this is referred to as its registration point . In previ ous demos, we've kept the registration point to the upper-left of the Movie Clip because of its ease in programming. But in this demo I placed the cursor's graph ics right under the registration point in the Movie Clip so that I can use this point as the hot spot. A cursor's hot spot is the location on the cursor graphic that the user uses to click with.

That mouse silhouette in the middle of Figure 9.5 is my cursor. Double-click it when you get the chance and observe where I placed all of the graphicsbelow the registration point, so the hot spot, the spot on the cursor that you click with, becomes the mouse's nose.

I have highlighted the Movie Clip in Figure 9.6 so that you can see where the registration point is.

Figure 9.6. Setting up your cursor's registration point

graphic/09fig06.gif


Okay, enough playcheck out the following listing and see what it really takes to replace the mouse cursor.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // This program demonstrates how // to hide your current mouse // cursor and replace it with one // in your Flash movie. // Initialize onLoad = function () {   // Hide the mouse cursor   Mouse.hide();   // Make sure the cursor is on top   // of all other objects   cursor.swapDepths(1000); }; // Follow the mouse position // on every frame. onEnterFrame = function () {   cursor._x = _xmouse;   cursor._y = _ymouse; }; // Once done, do a little cleaning. onUnload = function () {   Mouse.show(); }; 

If you look carefully you can see that I whipped out a handler that I haven't used in a while, the onLoad handler. This handler is the very first thing that executes when the project file is loaded. I also used the onUnload handlerand that's the very last thing that executes. I used the onLoad function to hide the cursor and to bring it all the way to the front. I used the onUnload function to reveal the cursor. Let's see how:

 onLoad = function () {   // Hide the mouse cursor   Mouse.hide();   // Make sure the cursor is on top   // of all other objects   cursor.swapDepths(1000); } 

The Mouse.hide method is very simple; all it does is hide the mouse cursor. Now what's this other method? cursor.swapDepths? When I set up the file, I gave the instance name of cursor to my mouse silhouette. As every object is organized in internal layers , our cursor was also placed in a layer. So what does cursor.swapDepths do? It swaps the layer position with whatever object is in the layer you request in the parameter. Requesting a ridiculously high number will almost guarantee that your cursor will be on top of everything elseand that's what you want.

Now that you performed all this setup, all you have to do is make the Movie Clip follow your cursor position. Take a look:

 // Follow the mouse position // on every frame. onEnterFrame = function () {   cursor._x = _xmouse;   cursor._y = _ymouse; }; 

It's a simple matter of passing on the current cursor position to the current Movie Clip position.

Now what happens if you want the cursor back? There's a command for that, of course Mouse.show . This is exactly what I used in the onUnload function.

NOTE

TIP

Use swapDepths on any Movie Clip you need to appear in front of another Movie Clip. For example, let's say you had a set of explosions programmed into your shooter, then you programmed the main player after the explosions.When you play the game, the explosions seem to burst behind the main player because of the order in which you created them. A nice way to fix this is by swapping the position of the first explosion with the player's posi tion and the game will look like it should. Next time you play the game, the explosions will explode over the player's layer. By using this method, you swap the internal layer positions , of course.

 // Once done, do a little cleaning. onUnload = function () {   Mouse.show(); }; 

As Mouse.show is the last thing that executes within the whole Flash program, the cursor will be revealed.

[ 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