Saturday, 20 March 2010

Drag and drop puzzle in Actionscript 3

This is an update to drag and drop puzzle tutorial where I will provide the Actionscript 3 code. The steps are exactly the same in the previous Actionscript 2 version, so I will only be providing the code in this tutorial. For more information on dragging and dropping, checkout this tutorial.

//Array to hold all the puzzle movie clip instances.
var burgerArr:Array = new Array (burger_mc, burger2_mc, burger3_mc,
burger4_mc, burger5_mc, burger6_mc, burger7_mc);

//For loop to control the drag and drop of each burger by adding a
//mouse up and down to each movie clip.
for (var i:uint =0; i < burgerArr.length; i++) {
burgerArr[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
burgerArr[i].addEventListener(MouseEvent.MOUSE_UP, drop);

//This function drag the movie clips when the mouse is pressed.
function drag(event:MouseEvent):void {

//This function drop the movie clips when the mouse is released.
function drop(event:MouseEvent):void {

After you have completed this tutorial you should end up with something like below.


Andreas Renberg 21 March 2010 at 05:01  

Just one tip, if you use "event.currentTarget" instead of just "" your code will work better in some cases.

If you have other movie clips inside of each piece of the puzzle, you will end up moving those instead of the actual piece. Using "currentTarget" will fix this.

iliketo 21 March 2010 at 06:29  

@Andreas Renbery

You are correct the event.currentTarget should be used by you have more than one movie clip instance.

*code updated.

mistmedia 7 August 2011 at 15:12  

Is there any way that the final positions of the pieces can be saved so that they can be displayed at a later scene.

Basically I have a game where each piece is a word and they have to be put in a certain order. Later in the game this order can be changed.

Any suggestions?


iliketo 6 September 2011 at 04:47  


You can try storing the positions inside an array.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP