Chapter 8. Mouse Interactivity

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:

  • The mouse events supported by Away3D
  • The difference between the ROLL_OVER/ROLL_OUT and MOUSE_OVER/MOUSE_OUT events
  • Projecting the mouse position into the scene

Away3D mouse events

Away3D 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:

Mouse3DEvent String Constant

Description

MOUSE_MOVE

Dispatched when the mouse cursor is moved across the surface of a 3D object.

MOUSE_OVER

Dispatched when the mouse cursor moves over a new 3D object or face with a new material.

MOUSE_OUT

Dispatched when the mouse is moved off a 3D object or face.

MOUSE_DOWN

Dispatched when a mouse button is pressed while the cursor is over a 3D object.

MOUSE_UP

Dispatched when a mouse button is released while the cursor is over a 3D object.

ROLL_OVER

Dispatched when the mouse cursor moves over a 3D object belonging to a group that was not already under the mouse cursor.

ROLL_OUT

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

screenX

The horizontal coordinate at which the event occurred in view coordinates.

screenY

The vertical coordinate at which the event occurred in view coordinates.

screenZ

The depth coordinate at which the event occurred in view coordinates.

sceneX

The x coordinate at which the event occurred in global scene coordinates.

sceneY

The y coordinate at which the event occurred in global scene coordinates.

sceneZ

The z coordinate at which the event occurred in global scene coordinates.

view

The view object inside which the event took place.

object

The 3D object inside which the event took place.

elementVO

The 3D element inside which the event took place.

material

The material of the 3D element inside which the event took place.

uv

The UV coordinates inside the 3D element where the event took place.

ctrlKey

Indicates whether the Control key is active (true) or inactive (false).

shiftKey

Indicates whether theShift key is active (true) or inactive (false).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.29.201