Copy link to clipboard
Copied
Hi.
I have a simple drag and drop app for learning English. You hear for eg: banana and have to drag that object to a certain area.
When my little kid plays it she finds bugs - she's my researcher.
It's hard to explain the bug as its hard to get it but kids can easily. You click on the target object and drag it a little, then quickly reclick it but not drag ie: release the mouse as it goes back to its initial position. You move the mouse away and and the object follows mouse even though you are now not dragging and you can't drop it.
I know its hard to imagine this but perhaps this is a know bug for drag and drops.
private function dragHandler(e:MouseEvent)
{
e.currentTarget.startDrag();
xIni = e.currentTarget.x;
yIni = e.currentTarget.y;
}
The you have the following which has an event listener for the mouse up event.
private function checkDrag(e:MouseEvent)
{
e.currentTarget.stopDrag();
if (this.currentBubble.hitTestObject(this.dragTarget))
{
if (currentBubble && currentBubble == e.currentTarget)
{
currentBubble.visible = false;
blnCorrect = true;
points = this.points + 10;
score.score_txt.text = String(points);
correct++;
vehiclePosition+=100;
TweenLite.to(animation,1,{x:vehiclePosition})
trace("CORRECT="+correct);
if (correct == 10)
{
endGame();
return;
}
bubbles.splice(currentIndexArray,1);//you must specify the parameter 1 ie: remove 1
sndChannel=soundCorrect.play();
sndChannel.addEventListener(Event.SOUND_COMPLETE, soundCorrectComplete)
}
}
else
{
incorrect++;
sndChannel=soundIncorrect.play();
sndChannel.addEventListener(Event.SOUND_COMPLETE, soundIncorrectComplete);
TweenLite.to(e.currentTarget, 1, {x:xIni, y:yIni, ease:Strong.easeOut, onComplete:onFinishTween});
}
}
So, the code should be like this:
private function dragHandler(e:MouseEvent):void
{
e.currentTarget.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, checkDrag);
xIni = e.currentTarget.x;
yIni = e.currentTarget.y;
}
private function checkDrag(e:MouseEvent):void
{
stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, checkDrag);
}
Also, i suggest you get into habit to ALWAYS declare datatypes, including what functions return - it is good for memory and performance. In
...Copy link to clipboard
Copied
add MOUSE_UP event listener to stage - not object itself.
Copy link to clipboard
Copied
Hi - cheers Andrei - how u doing?
I got it sorted with a boolean. ie: If the object is still being tweened back to its original position then you can't drag it unless the boolean is false.
Cheers
private function mouseDownDragHandler(e:MouseEvent)
{
if (blnTweenedtoInitialPosition == false)
{
e.currentTarget.startDrag();
trace("You should drag");
}
else
{
//do nothing
}
xIni = e.currentTarget.x;
yIni = e.currentTarget.y;
/*var myTargetName:String = e.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
trace("TARGET NAME="+myTargetName);*/
}
Copy link to clipboard
Copied
Still, the most reliable way to manage MOUSE_UP on objects in AS3 is to add listener to stage, otherwise mouse interaction behavior is totally unpredictable.
Copy link to clipboard
Copied
So, the code should be like this:
private function dragHandler(e:MouseEvent):void
{
e.currentTarget.startDrag();
stage.addEventListener(MouseEvent.MOUSE_UP, checkDrag);
xIni = e.currentTarget.x;
yIni = e.currentTarget.y;
}
private function checkDrag(e:MouseEvent):void
{
stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, checkDrag);
}
Also, i suggest you get into habit to ALWAYS declare datatypes, including what functions return - it is good for memory and performance. In your case, :void should be function return datatype.
In addition, you don't need to stopDrag() on an object - since only a single object can be dragged at a time - just calling stopDrag() is sufficient.
Copy link to clipboard
Copied
Thanks a lot. I'm going to put that code in now. That's useful as I'm going to make at least 20 versions of this and some will end up on a mobile so all optimizations are great.
Cheers again
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more