Home >> Production >> Flash and ActionScript >> Drag and Drop

ActionScript: Drag and Drop

It is not difficult to create "draggable objects" in Flash. But it does involve communicating between timelines. When we press down on one mc instance, we want to be able to drag that instance until we release the mouse.

[You need to FIRST understand how to control different timelines using Tell Target to do this]

Look at this example:

[Download FLA]

 

How to make a draggable movieclip symbol:

First, to be draggable, we need this symbol to be both a button, so we can work with mouse events like on press an don release of mouse, and we need it to be a movieclip symbol so we can target it. How do we do this? We make movieclip symbol, and then put a button inside of it.

1. Draw or Type something on the stage in Flash, and then choose Insert-->Convert to symbol and make it a BUTTON.

2. Select your new button symbol, and again go to Insert-->Convert to symbol and this time make it a MOVIE CLIP. Now you have a movieclip symbol with a button inside.

3. Apply the different states to your button: Select the movieclip symbol that you just made and right-click and choose Edit in Place. Now you will be inside the mc timeline, where the button is. Select the button and right-click and choose Edit in Place again. This time you will be inside the button symbol. Rememb to add a solid area to the hit state.

(Notice how far this button symbol "iam" is inside the mc "mc iam" which is on the main timeline, Scene 1.)

4. Now go back to the main timeline (click Scene 1)

5. Now we have to give this mc an instance name. Select the mc symbol on the main timeline, and bring up the Instance panel, by going to Windows--> Panels--> Instance.

In the panel box, give this instance a unique name. In this example, this instance is named "samiam"

6. Now we just have to apply the button actions, in order to tell it to drag when pressed and stop dragging when released. In order to apply the actions to the button (and not the mc that contains the button) right-click on the mc symbol and choose Edit in Place. Now you are inside the mc symbol where the button is.

7. Select the button symbol and right-click to bring up the Actions Editor. This time we need to double click the Actions (Not Basic Actions) in the menu on the left side to access more commands. Double click "startDrag" which is listed under the Actions section.

You will notice that the editor knows you're applying these actions to a button, so it automatically adds the On Mouse over part to the code:

8. Highlite the code: startDrag (""); and then click inside the target box at the bottom of the actions editor. Then click the Path editor icon, which turns green once you click inside the Path field.

 

9. In the path editor, choose Absolute Mode this time, and select the name of the instance you are targetting. Click OK.

 

10. In the Actions Editor, now we want to change On Release to On Press in the first line.

11. Now we have: on press, start dragging that symbol named samiam, but we also have to tell it to stop dragging the symbol when the mouse is released.

To add another mouse event first, select the third line of the code on the right hand side so only the symbol } is highlited.


Then, on the left hand side, find the OnMouseEvent command under Basic Actions (Not under Actions where we found the startDrag command). Double-click OnMouse Event.

The code on the right should now read:

12. Under Actions (not Basic Actions) on the left hand menu double click stopDrag.

 

That's it! Close down the Actions Editor, return to the main _root timeline (Scene 1) and test your movie.

 

Troubleshooting:

1. Did you remember to name the instance of the mc? (Go to step 5)

2. Did you remember to define the hit state of the button with a solid shape? (Step 3)

3. Did you apply the actions to the button symbol inside the MC and not the MC? (Step 6)

 

See some examples:

Dress Up Barbie

Magnetic Poetry