我正在尝试制作一个简单的项目,当您单击按钮时,将可拖动的 MovieClip 添加到 stag 中,当您单击它时,将 MovieClip 释放到您单击的 X/Y 位置,然后您可以拾取 MovieClip 并将其拖入 bin (MovieClip),它会破坏自己。代码运行良好,我可以使用按钮制作多个电影剪辑,当我将它们拖到垃圾箱中时它们都被破坏了,但是我不喜欢有“错误代码”。
import flash.events.MouseEvent;
var rubbish:my_mc = new my_mc();
btntest.addEventListener(MouseEvent.CLICK, makeRubbish);
function makeRubbish (event:MouseEvent):void {
addChild(rubbish);
rubbish.x = mouseX - 10;
rubbish.y = mouseY - 10;
rubbish.width = 50;
this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
rubbish.buttonMode = true;
}
function stopDragging (event:MouseEvent):void {
rubbish.stopDrag()
event.target.addEventListener(MouseEvent.CLICK, startDragging);
rubbish.buttonMode = true;
if (event.target.hitTestObject(bin))
{
trace("hit");
event.target.name = "rubbish";
removeChild(getChildByName("rubbish"));
}
}
function startDragging (event:MouseEvent):void {
event.target.startDrag();
this.addEventListener(MouseEvent.CLICK, stopDragging);
}