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 {
event.currentTarget.startDrag();
}
//This function drop the movie clips when the mouse is released.
function drop(event:MouseEvent):void {
event.currentTarget.stopDrag();
}
After you have completed this tutorial you should end up with something like below.



4 comments:
Just one tip, if you use "event.currentTarget" instead of just "event.target" 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.
@Andreas Renbery
You are correct the event.currentTarget should be used by you have more than one movie clip instance.
*code updated.
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?
Thanks
@mistmedia,
You can try storing the positions inside an array.
Post a Comment