• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Mouse Detection

The Mouse object, like the Key object, is one of the few ActionScript classes that do not need to be instantiated before you can use it. Its properties are available immediately, and its methods are called with the object name (Mouse). You will learn about its methods and properties in Chapter 7 when you learn more about the movie clip. Here, you’ll learn about the mouse events and how to detect them.

You can detect three mouse events: onMouseMove, onMouseDown, and onMouseUp. The onMouseMove event happens whenever the user moves the mouse pointer, onMouseDown happens when the mouse button is pressed, and onMouseUp happens when the mouse button is released. To detect a mouse event, you can assign an anonymous function for it on the main Timeline, like this:

_root.onMouseMove = function(){

(_root is an ActionScript word that refers to the main Timeline.)

Another way is to create a listener, just as you do to detect key events. Create a listener from the generic Object class. Assign a mouse-event handler to the listener and then register your listener with the Mouse object, using the method


To detect mouse movement:

Select the first frame of the main Timeline, and open the Actions panel.

Choose Objects > Movie > Mouse > Listeners > onMouseMove.

In the Object field of the Parameters pane, enter _root to refer to the main Timeline.

The onMouseMove event handler is assigned to the main Timeline (Figure 4.88).

Figure 4.88. An onMouseMove event handler.

Choose an action as a response to this event.

Whenever the mouse moves, Flash performs the actions listed within the onMouseMove event handler (Figure 4.89).

Figure 4.89. This movie contains a frame-by-frame animation of a three-wheeler that rotates. Any time the mouse pointer moves, Flash advances to the next frame.

Choose Actions > Movie Clip Control > updateAfterEvent.

The updateAfterEvent action forces Flash to refresh the display. For certain events, such as onMouseMove, updateAfterEvent makes certain that the graphics are updated according to the event, not the frame rate.

To create a listener to detect a mouse press:

Select the first frame of the main Timeline, and open the Actions panel.

Choose Actions > Variables > set variable.

In the Variable field, enter a name for your listener object (Figure 4.90).

Figure 4.90. The object called myListener will be your listener to detect events of the Mouse object.

Put your pointer in the Value field, choose Objects > Core > Object > new Object, and check the Expression checkbox.

The constructor function new Object() appears in the Value field (Figure 4.91). Flash instantiates a new generic object, using the name in the Variable field.

Figure 4.91. The object called myListener is created from the Object class.

Choose Objects > Movie > Mouse > Listeners > onMouseDown.

In the Object field, enter the name of the listener that you created in the preceding statement (Figure 4.92).

Figure 4.92. The onMouseDown event is assigned to your listener.

The onMouseDown event handler for your listener is created.

Now choose an action as the response to the onMouseDown event (Figure 4.93).

Figure 4.93. When the listener detects the mouse button being pressed, Flash goes to frame 5 and stops there.

Select the closing curly brace of the function.

Choose Objects > Movie > Mouse > Methods > addListener.

For the parameter, enter the name of your listener (Figure 4.94).

Figure 4.94. The final step in using this listener is registering it to the Mouse object. The stop action has been added here to keep the movie at frame 1 until the event happens.

Your listener is registered and will now listen and respond to the onMouseDown event.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint