Thomas D.Garrod
07-19-2009, 07:20 PM
I've created drag and drop widgets that have a separate target for each object draged, and in those cases, I've directed the dragged object to position over the target.
Now I need to have one common target, and send each dragged object to a different location.
I have a hidden object that I want each dragged piece to rest over after a successful hitTest. I set the _targetPiece.x and _targetPiece.y for each drag object in the function that loads them to the stage. All this makes sense when the target piece and the hidden target object are the same, but since the target and the destination are different, I'm confused over how to change this to work.
I though that if I changed _targetPiece (bold red) in the hitTest to "hitTarget", the instance name of the common target, that would work, but the object just rests where the dragging stops. Can someone suggest how I can change the following code to fit?
private function checkTarget(event:MouseEvent):void
//The listener tells flash to go here for instructions when the object is released after a drag
{
if(event.currentTarget.hitTestObject(event.current Target._targetPiece))
{
//tells the piece to position itself exactly over the target if the drage touches the target piece
event.currentTarget.x = event.currentTarget._targetPiece.x;
event.currentTarget.y = event.currentTarget._targetPiece.y;
trace(event.currentTarget._targetPiece.X);
trace(event.currentTarget._targetPiece.y);
//since each drag object has its own listener, we want to remove these when done
//(otherwise they might move them to the wrong position)
event.currentTarget.removeEventListener(MouseEvent .MOUSE_UP, checkTarget);
event.currentTarget.disable();
//if all pieces are in place, the game is over._
_currentPieces ++;
if(_currentPieces >= _totalPieces)
{
celebrateSuccess();
}
}
else
{
//else put the piece back to its starting point
event.currentTarget.x = event.currentTarget._origX;
event.currentTarget.y = event.currentTarget._origY;
}
}
Now I need to have one common target, and send each dragged object to a different location.
I have a hidden object that I want each dragged piece to rest over after a successful hitTest. I set the _targetPiece.x and _targetPiece.y for each drag object in the function that loads them to the stage. All this makes sense when the target piece and the hidden target object are the same, but since the target and the destination are different, I'm confused over how to change this to work.
I though that if I changed _targetPiece (bold red) in the hitTest to "hitTarget", the instance name of the common target, that would work, but the object just rests where the dragging stops. Can someone suggest how I can change the following code to fit?
private function checkTarget(event:MouseEvent):void
//The listener tells flash to go here for instructions when the object is released after a drag
{
if(event.currentTarget.hitTestObject(event.current Target._targetPiece))
{
//tells the piece to position itself exactly over the target if the drage touches the target piece
event.currentTarget.x = event.currentTarget._targetPiece.x;
event.currentTarget.y = event.currentTarget._targetPiece.y;
trace(event.currentTarget._targetPiece.X);
trace(event.currentTarget._targetPiece.y);
//since each drag object has its own listener, we want to remove these when done
//(otherwise they might move them to the wrong position)
event.currentTarget.removeEventListener(MouseEvent .MOUSE_UP, checkTarget);
event.currentTarget.disable();
//if all pieces are in place, the game is over._
_currentPieces ++;
if(_currentPieces >= _totalPieces)
{
celebrateSuccess();
}
}
else
{
//else put the piece back to its starting point
event.currentTarget.x = event.currentTarget._origX;
event.currentTarget.y = event.currentTarget._origY;
}
}