Game Development Reference
In-Depth Information
}
private function onMouseMove(e:MouseEvent):void {
trace("onMouseMove");
}
private function onMouseOver(e:MouseEvent):void {
trace("onMouseOver");
}
private function onMouseUp(e:MouseEvent):void {
trace("onMouseUp");
}
private function onMouseDown(e:MouseEvent):void {
trace("onMouseDown");
}
private function onMouseOut(e:MouseEvent):void {
trace("onMouseOut");
}
}
}
Handling mouse events in many objects
Consider a screen with many sprite buttons. How do we know what sprite was
clicked on? Do we add one listener callback for all the sprites? You could, but there is
a better way: an object-oriented way to handle it makes things elegant.
Let's start out with our own class that is a subclass of Sprite. Call it MouseTrap ,
which does three things: draws itself, registers for mouse clicks, and prints when it
gets a click. To distinguish among different MouseTrap objects, we also give it an ID,
which is printed when the mouse is clicked.
We will also add code in the main class that creates these MouseTrap objects and put
it on the screen by calling addChild .
The project created for this example has the following compiler settings:
-default-size 300 300 -default-background-color 0xFFFFFF
Here is the listing:
MainClass:
package {
import flash.display.Sprite;
public class SpriteMouseClick extends Sprite
 
Search WWH ::




Custom Search