Copy link to clipboard
Copied
Hello,
I have created a puzzle (ActionScript 3.0) with drag and drop pieces, assigned targets, and everything is working except for one thing which I can't figure out. If I move one puzzle piece, all of the pieces at the top suddenly snap to the top of the puzzle frame, and can't be moved above it freely. They keep snapping to that Y coordinate.
I have 8 pieces, but here is a sample of the code I am using:
piece1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_17);
function fl_ClickToDrag_17(event:MouseEvent):void
{
piece1.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_17);
function fl_ReleaseToDrop_17(event:MouseEvent):void
{
piece1.stopDrag();
if (target1.hitTestObject(piece1.tar1))
piece1.x = 218.5;
piece1.y = 309.4;
}
The code I am using is the same for each piece. The target1 is a square in Scene 1. tar1 is a square that it matches up to inside the piece1 movie clip.
Any suggestions would be greatly appreciated.
Copy link to clipboard
Copied
use:
piece1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_17);
function fl_ClickToDrag_17(event:MouseEvent):void
{
piece1.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_17);
}
function fl_ReleaseToDrop_17(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_17);
piece1.stopDrag();
if (target1.hitTestObject(piece1.tar1))
piece1.x = 218.5;
piece1.y = 309.4;
}
or, even better, use:
var piece_num:int = 8;
var piece_dragged:MovieClip
for(var i=0;i<piece_num;i++){
this['piece'+(i+1)].addEventListener(MouseEvent.MOUSE_DOWN,piece_startdragF);
this['piece'+(i+1)].origX = this['piece'+(i+1)].x;
this['piece'+(i+1)].origY = this['piece'+(i+1)].y;
this['piece'+(i+1)].ivar = i;
}
function piece_startdragF(e:MouseEvent);void{
piece_dragged=MovieClip(e.currentTarget);
MovieClip(e.currentTarget).startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP,piece_stopdragF);
}
function piece_stopdragF(e:MouseEvent):void{
stage.removeEventListener(MouseEvent.MOUSE_UP,piece_stopdragF);
if(this['target'+(piece_dragged.ivar+1))].hitTestObject(piece_dragged.tar)){ // <- note all pieces have the same named 'tar' child movieclip
piece_dragged.x = this['target'+(piece_dragged.ivar+1)).x
piece_dragged.y = this['target'+(piece_dragged.ivar+1)).y
} else {
piece_dragged.x=piece_dragged.origX;
piece_dragged.y=piece-dragged.origY;
}
}
Copy link to clipboard
Copied
Thank you for your reply.
I changed it to the first option, and the piece now snaps to the bottom if it is not in the right spot.
The second option gave me a number of Syntax errors.
I need to keep the scripting simple so that I can teach it to others - what I used original was the ActionScript snippet "drag and drop" which worked fine until I started adding the coordinates for the targets.
Is there another way to script the targets?
if (target1.hitTestObject(piece1.tar1))
piece1.x = 128;
piece1.y = 159;
Copy link to clipboard
Copied
if (target1.hitTestObject(piece1.tar1))
piece1.x = target1.x
piece1.y = target1.y;
Copy link to clipboard
Copied
With this code it is still effecting the other pieces. The one I am dragging snaps into place, but the others also snap to the top y coordinate and you cannot move them from there afterwards. Only side to side.
Copy link to clipboard
Copied
what code are you using?
Copy link to clipboard
Copied
piece2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);
function fl_ClickToDrag_2(event:MouseEvent):void
{
piece2.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
function fl_ReleaseToDrop_2(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
piece2.stopDrag();
if (target2.hitTestObject(piece2.tar2))
piece2.x = target2.x
piece2.y = target2.y;
}
Copy link to clipboard
Copied
nothing will change it's y property, from that code, except piece2.
if you see anything else, there's other code causing the problem.
Copy link to clipboard
Copied
I just started again with a new file. The other pieces are still moving. This is the entire code:
/* Piece1
*/
piece1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
piece1.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
piece1.stopDrag();
if (target1.hitTestObject(piece1.tar1))
piece1.x = target1.x
piece1.y = target1.y;
}
/* Piece2
*/
piece2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);
function fl_ClickToDrag_2(event:MouseEvent):void
{
piece2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
function fl_ReleaseToDrop_2(event:MouseEvent):void
{
piece2.stopDrag()
if (target2.hitTestObject(piece2.tar2))
piece2.x = target2.x
piece2.y = target2.y;;
}
/* Piece3
*/
piece3.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_3);
function fl_ClickToDrag_3(event:MouseEvent):void
{
piece3.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_3);
function fl_ReleaseToDrop_3(event:MouseEvent):void
{
piece3.stopDrag();
if (target3.hitTestObject(piece3.tar3))
piece3.x = target3.x
piece3.y = target3.y;
}
Copy link to clipboard
Copied
from that code all the pieces will move when there's a mouseup on the stage. you should use the code i suggested or, at least, check the code differences and see why your code causes all the pieces to move on mouseup and my code does not.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now