11.13. Constraining Drag-and-Drop Areas

Problem

You want to allow a movie clip to be draggable only within a certain region.

Solution

Use the optional parameters for the startDrag( ) method to specify a region over which the movie clip can be dragged.

Discussion

By default, draggable movie clips can be moved anywhere on the stage. However, there are many situations in which you may want to define an area in which a movie clip can be dragged. Two examples of this include:

  • If your movie contains various sections, and you don’t want the user to be able to drag movie clips over other sections. For example, if you have a puzzle movie with instructions or other text on the right side, and the puzzle pieces on the left, then you could define a draggable area for the movie clips that includes only the left side of the movie.

  • When you create sliders, you want to make sure that the slider can be dragged only along a line.

Fortunately, ActionScript makes it relatively simple to define a draggable area for your movie clips. In Recipe 11.12 you can read about how to use the startDrag( ) method in a basic way to create draggable movie clips. However, in addition to the optional Boolean parameter that determines whether the movie clip snaps to the pointer, the startDrag( ) method also accepts four more optional parameters. These four additional parameters determine the rectangular area over which the movie clip can be dragged by defining the left, top, right, and bottom coordinate values. The coordinates ...

Get Flash 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.