Game Development Reference
In-Depth Information
As you can see, all the drag-and-drop functionality has been pushed into this class. The [eo@n]ccejc
variable is accessed with a getter so it can be used by the I]ej class to find out whether the object is
being dragged.
The class also uses an AJPAN[BN=IA event that prevents the object from moving past the edges of the
stage. I used it in this simple example just to show you how it can be done, but there's no reason for
you to stop here. You can use any other code you like here (including code for friction, acceleration,
and gravity) and also build in a collision detection system if you need to.
One very important thing to understand is that when you drag objects using op]np@n]c, the object's
position on the stage is not updated by an AJPAN[BN=IA event. In fact, it even updates at a different
rate from the movie's frame rate. This means that any physics simulations you might want to imple-
ment won't work while the object is being dragged. This might not be a problem for you, but if it is,
you'll be looking at some techniques in the following pages that will help you build your own custom
drag-and-drop engine if you need to.
A drag-and-drop engine? Engines are what programmers like to call self-contained
systems that perform a specialized job. Phrases such as “collision detection engine” or
“drag-and-drop engine” have nothing to do with exhaust pipes and fan belts. It's just
another way of saying “integrated system.”
Releasing the mouse outside the stage area
There is an extremely important detail that you need to make note of in this code. The kjIkqoa@ksj
listener is added directly to the object, but the kjIkqoaQl listener is added to the op]ca object. If you
don't add the kjIkqoaQl listener to the op]ca, your drag-and-drop programs will encounter a pecu-
liar glitch that is due to the way the Flash Player interacts with the rest of your computer's operating
For “security reasons” (which means to prevent someone using Flash and AS3.0 to write a destructive
virus or worm that could damage your computer), the Flash Player does not allow anything that goes
on within the confines of the SWF to interact with the rest of the computer's operating system, includ-
ing the position of the mouse on the screen. If the computer mouse is interacting with objects inside
the Flash Player or web browser window, everything works the way you expect it to. However, as soon
as the mouse moves away from the player window, the player can no longer receive instructions from
the mouse.
This is a problem for drag-and-drop games. What happens if the player clicks an object, drags it to the
edge of the screen, and then releases the mouse button outside the Flash Player window? The Flash
Player isn't informed that the button was released and still thinks that the object is being dragged. This
Search Nedrilad ::

Custom Search