Almost every Flash application uses the mouse as the primary means of receiving input from the user. In Flash you respond to mouse events by registering functions against specified mouse events. Away3D follows this same principle, and indeed even uses the same names for mouse events performed on 3D objects as Flash uses for 2D objects. This means that any developer who has used the mouse in a traditional 2D Flash application will have no trouble doing the same in an Away3D application.
We will also see how the position of the mouse on the screen can be projected into a 3D scene, which gives us the ability to create the kind of 3D drag-and-drop interface that is present in many games.
This chapter will cover the following topics:
ROLL_OVER
/ROLL_OUT
and MOUSE_OVER
/MOUSE_OUT
eventsAway3D has support for a number of mouse events relating to 3D objects. All of the events are defined as constant strings in the Mouse3DEvent
class. These constants are listed in the following table:
|
Description |
---|---|
Dispatched when the mouse cursor is moved across the surface of a 3D object. | |
Dispatched when the mouse cursor moves over a new 3D object or face with a new material. | |
Dispatched when the mouse is moved off a 3D object or face. | |
Dispatched when a mouse button is pressed while the cursor is over a 3D object. | |
Dispatched when a mouse button is released while the cursor is over a 3D object. | |
Dispatched when the mouse cursor moves over a 3D object belonging to a group that was not already under the mouse cursor. | |
Dispatched when the mouse cursor moves off a 3D object. |
These events can be dispatched by any object that extends the Object3D
class, which includes the scene. The view can also dispatch all of these events except for Mouse3DEvent.ROLL_OVER
and Mouse3DEvent.ROLL_OUT
.
If you have ever used the Flash mouse events, the Away3D mouse events should look familiar as they mirror those that are available in Flash. Responding to them is also very similar to the way traditional Flash mouse events are handled.
You register an event handler using the addEventHandler()
function like so:
myObject3D.addEventHanlder( MouseEvent3D.MOUSE_MOVE, onMouseMove );
The mouse event handler functions look like the following:
function onMouseMove(event:MouseEvent3D):void { // do something here }
The MouseEvent3D
class (from the away3d.events
package) that is passed to the mouse event handler functions includes some unique properties that allow you to work with mouse events in 3D. These properties are listed in the following table:
Property |
Description |
---|---|
|
The horizontal coordinate at which the event occurred in view coordinates. |
|
The vertical coordinate at which the event occurred in view coordinates. |
|
The depth coordinate at which the event occurred in view coordinates. |
|
The x coordinate at which the event occurred in global scene coordinates. |
|
The y coordinate at which the event occurred in global scene coordinates. |
|
The z coordinate at which the event occurred in global scene coordinates. |
|
The view object inside which the event took place. |
|
The 3D object inside which the event took place. |
|
The 3D element inside which the event took place. |
|
The material of the 3D element inside which the event took place. |
|
The UV coordinates inside the 3D element where the event took place. |
|
Indicates whether the Control key is active (true) or inactive (false). |
|
Indicates whether theShift key is active (true) or inactive (false). |