4. Other useful MouseEvent properties

I have referred throughout this article to the event object in a generic sense. But actually, the event object we have been using is an instance of the MouseEvent class. The MouseEvent class is a subclass of the Event class. Two properties of the Event class, target and currentTarget, are inherited by the MouseEvent class. However, the MouseEvent class defines a few properties of its own that apply specifically to Mouse events, and these are quite useful, too.

For example, the properties altKey, ctrlKey, and shiftKey are Boolean properties that can tell you if those keyboard keys are currently being held down when the mouse event occurs. To use them, just remember that they are properties of the MouseEvent object that your handler function receives as a parameter. So you use them the same way you used target and currentTarget. Example:

button_mc.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
    if(event.shiftKey == true) {
        event.currentTarget.x += 5;
    }
}

In this scenario, button_mc will move five pixels to the right when it is clicked, but only if the Shift key is also being pressed when the click happens.

Also useful are the localX, localY, stageX, and stageY properties. These properties are Number properties that can tell you the screen coordinates of the mouse pointer's location when the click happened. localX and localY give you the mouse coordinates in relation to the target (in the case of our examples so far, whatever was clicked on). stageX and stageY, meanwhile, give you the mouse coordinates in relation to the stage.

Another useful property of the Event class is type. This property is a String value that can tell you what type of event triggered the listener. This might be useful for combining two or more event handler functions into one. For example, let's consider our sample file as we last left it, and let's combine the two event handler functions into one big one:

menu.addEventListener(MouseEvent.CLICK, clickHandler);
menu.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
function clickHandler(event:MouseEvent):void {
	if (event.type == MouseEvent.MOUSE_DOWN) {
		if (event.target == menu) {
			event.target.startDrag();
		}
	} else if (event.type == MouseEvent.CLICK) {
		if (event.target == menu) {
			event.target.stopDrag();
		}
		if (event.target == button1) {
			navigateToURL(new URLRequest("http://www.google.com"), "_blank");
		}
		if (event.target == button2) {
			navigateToURL(new URLRequest("http://www.amazon.com"), "_blank");
		}
	}
}

It's not that this is necessarily any more desirable than the previous version, but it makes a good example of one possible use of the type property. The main thing is that the type property can tell you what kind of event triggered the listener. What you do with that information is up to you.

There are several other properties of the MouseEvent class (some inherited from the Event class) that you can use in the body of your event handler functions. You should consult the Adobe documentation (help files) for the complete list.

Also available are several methods of the MouseEvent class. One particularly useful method is updateAfterEvent, which can cause the screen to be updated when the event happens, when it would normally be updated only at the framerate of the flash movie. You would use methods of the MouseEvent class the same way you use properties. Just use the name of the event parameter, followed by a dot, then the name of the method, followed by the method call operator ():

event.updateAfterEvent();

Two other methods worth mentioning here, inherited from the Event class, are stopPropagation() and stopImmediatePropagation(). stopPropagation() will prevent the event flow from continuing beyond the current "place" in the event flow (remember the demo from several pages back?) after the listeners there are executed. stopImmediatePropagation() also prevents the event flow from continuing beyond the current "place" in the event flow, but also doesn't allow the listeners there to be executed (I know my use of the term "place" in the event flow is unofficial; Adobe in the documentation calls it a "node").

The main point is that all of these properties and methods of the Event and MouseEvent classes can be used by you in the body of your event handler functions. Once again, see the documentation for a complete list.

While we have been primarily focused on the CLICK event, the event flow through the display list also applies to most of the other mouse events, like MOUSE_DOWN, MOUSE_UP, MOUSE_OVER, MOUSE_OUT, MOUSE_MOVE, DOUBLE_CLICK, and MOUSE_WHEEL. All of these events will propagate to display object children when the event listener is added to a display object container.

The one pair of mouse events that don't have a bubbling phase is ROLL_OVER and ROLL_OUT. This is on purpose, to give you an alternative to MOUSE_OVER and MOUSE_OUT. You would use ROLL_OVER and ROLL_OUT instead when you want to program a display object container for mouse rollovers, but don't want the rollout to be triggered by the children. Rather, you want to detect only when a rollout happens for the whole container.

I'm sure I haven't covered everything to do with the MouseEvent class, and you can check out whatever I've missed on your own. My main purpose with this article has been to help you understand the Actionscript 3.0 event flow. The MouseEvent.CLICK event served a good purpose as an example of one of the events that flows through the display list. By all means, you should explore and experiment with the other events on your own. As always, I hope I've sparked your imagination and insipired you to create your own flash applications. Send me a link to yours, drop me a line, leave me a comment. Thanks!

Happy coding!

Jody Hall

Comments

spiralvista
User offline. Last seen 1 year 13 weeks ago. Offline
Joined: 06/09/2009
thorough dissection

all us, beginners need to be spoon fed. thank you for this tutorial, I will not be flummoxed by target and currentTarget anymore. Also bubbling, target and capture phases are much more clearer. Yes you deserve my Bookmark. spiralvista *****

rsolis_20
User offline. Last seen 1 year 13 weeks ago. Offline
Joined: 06/09/2009
MouseEvent

Hi Sir Jody, Thank you for sharing your idea for making the event flow on how it works. While I am reading your article, is it possible to use it in separate Actionscript (flash+as file). Do you have any sample of that. Always admire your work. Once again, thank you for sharing your talent! Sincerely yours, Roger :)

Jody Hall
Jody Hall's picture
User offline. Last seen 3 hours 15 min ago. Offline
Joined: 04/26/2009
Hey, Roger, Thanks for the

Hey, Roger,

Thanks for the comment! The event flow (and the event model) are built into the language, and so using it inside of classes works exactly the same. I didn't really have any examples hanging around, so I adapted the "event_flow_finished.fla" file to use a document class instead. I removed the code from frame 1 of the fla file and put it in a document class called Main.as. The code is essentially the same. The only rewriting that was necessary was to give it the structure of a class, of course, and then to add access modifiers like "private" to the functions. Also, in a class you have to make sure you import any other classes that your class makes use of, but that aren't in the current package. Anyway, I put these files in a zip archive for you: event_flow_using_classes.zip . Hope it helps!

Jody

raglan
User offline. Last seen 1 year 9 weeks ago. Offline
Joined: 07/06/2009
Great Introduction to ActionScript 3

Hi Jody, your tutorial was extremely helpful in building a foundation for writing ActionScript. I've spent the last week reading through tutorial sites, and yours is the first to really take the time to explain everything that is happening in a basic function. Keep it up!

jonasio
User offline. Last seen 1 year 7 weeks ago. Offline
Joined: 07/15/2009
Thanks!

Finally someone who can explain these things!!, I'm stuck with books that cover everything but can't lay it out using logical examples, thanks so much!

tzachi
User offline. Last seen 25 weeks 6 days ago. Offline
Joined: 02/15/2010
Thanks!

Jody, you are a rising star in that field of tutorials. Following your tutorial together with Colin Moock's book by my side, makes me feel the sky is the limit :)