Game Development Reference
The one must-read section, however, is the “Dispatching events” section at the end of the chapter. Learning
to dispatch events is such a useful skill for a game designer that it could become the primary technique
that you use for structuring your games from now on, no matter what kind of game you're designing.
So, without further ado, on with the game!
Dragging and dropping objects
To create any kind of game that involves matching objects, such as puzzle or board games, you need
to create objects that can be dragged and dropped with the mouse. Lucky for you, any objects that
inherit AS3.0's Olnepa class, such as Movie Clip objects, have some built-in properties and methods
that are specialized for drag-and-drop interfaces:
op]np@n]c: Makes an object dragable
opkl@n]c: Stops dragging
`nklP]ncap: Tells you which object is under the mouse when an object is being dragged
You'll take a look at two contrasting examples of how to program a basic drag-and-drop game:
a procedural approach and an object-oriented approach. Highlighting the differences between these
approaches will illustrate some important aspects about drag-and-drop objects and be an interesting
look at the way procedural programs differ from object-oriented ones.
Dragging and dropping the procedural way
Let's have a look at a simple example of a drag-and-drop interface.
1. In this chapter's source files, you'll find a folder called @n]c]j`@nkl-. Open this folder as
a project in the Project panel.
2. Test the project. You can drag the red and blue squares around the stage. When you release
them over the corresponding empty red or blue squares, they snap into place. If you release
the left mouse button over an object, the name of the object is displayed in the Output panel.
The `n]c=j`@nkl-*bh] file contains four objects: ^hqaOmq]na, na`Omq]na, ^hqaP]ncap, and na`P]ncap
(see Figure 10-1).
Figure 10-1. Drag-and-drop objects