Drag Objects on Stage - Flash (AS2)

view all ยป


This is a fairly simple tutorial, which will allow the user to drag movie clips around the stage. This is great for use in websites and games for user interaction.

STEP 1

First off, create a movie clip on the stage. To do this simply and for example purposes, select the shape tool, set the colours to your liking and draw a funky shape on the stage. Select this shape, and press f8 to convert to symbol. Select movie clip, registration centre, and name to MC_shape. Click ok, then set an instance name in the properties panel of MC_shape.

STEP 2

Rename layer 1 to ’shape’ and lock it. Create a new layer called ‘actions’. On the first frame of this layer, press f9 to enter the actions window and put the following code:
MC_shape.onPress = function(){
MC_shape.startDrag();
}

MC_shape.onRelease = function(){
MC_shape.stopDrag();
}

STEP 3

Hit CTRL + Enter, and you will notice that if you click and drag on your movie clip, you can move it about the stage! Pretty cool huh for a few lines of code.

Now I will explain what the code does:

The first line of the code takes your movie clip (MC_shape) when clicked (.onPress) starts up the following function ( = function(){ )
The second line takes the movie clip (MC_shape) and starts the Drag effect (startDrag();)
The third line closes the function ( } )

The second block of code is the same, except when the mouse release (.onRelease) is detected the drag ability is stopped (.stopDrag();)

And there you have it! A few simple steps that will allow you to drag movie clips around the Flash stage.

Have a play and enjoy!

More tutorials from The Dude coming soon, so stay tuned!

The Dude

Not a member yet?? Why not! Get invites for game demo’s, important updates and general consideration for cool stuff!

Did this post help you? If so help us continue to grow and Digg this page or Share on Twitter!

If you haven't already, sign up to our RSS feed or register to recieve updates direct to your email!

One Response to “Drag Objects on Stage - Flash (AS2)”

  1. The Arm

    It’s me again!
    This is awesome, one question though. Can I make a drag-around-able movie clip start moving somewhere, but when being dragged it obviously stops movieng, and when released it goes back to moving for the targeted area?
    Kind of like a mix of motion tween and this.

Leave a Reply