Skip to main content
stech2017
Participant
March 4, 2018
Question

Setting Targets for Puzzle Pieces in Animate CC

  • March 4, 2018
  • 1 reply
  • 1140 views

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.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
March 5, 2018

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;

}

}

stech2017
stech2017Author
Participant
March 5, 2018

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;

kglad
Community Expert
Community Expert
March 5, 2018

if (target1.hitTestObject(piece1.tar1))

piece1.x = target1.x

piece1.y = target1.y;