0

我正在尝试制作一个简单的项目,当您单击按钮时,将可拖动的 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);
}
4

1 回答 1

0

一些指针

  • an的target属性Event并不总是看起来的那样。它实际上是指事件冒泡过程中的当前阶段。尝试使用该currentTarget属性。
  • 我还建议将该stopDragging方法与舞台联系起来,因为有时您的鼠标不会在您单击时悬停在拖动上。
  • 我会使用MOUSE_UP事件而不是CLICK标准拖动行为。
  • 拖动时,保持对拖动的全局引用,以便stopDrag在正确的对象上调用方法。

试试这个

import flash.events.MouseEvent;

var rubbish:my_mc = new my_mc();
var dragging: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;
  rubbish.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
  rubbish.buttonMode = true;

}

function stopDragging (event:MouseEvent):void {

  this.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);

  if(dragging !== null){

    dragging.stopDrag();

    if (event.currentTarget.hitTestObject(bin)){

      removeChild(dragging);

    }

    dragging = null;

  }
}

function startDragging (event:MouseEvent):void {

  dragging = event.currentTarget as my_mc;

  dragging.startDrag();

  this.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

}
于 2013-09-22T23:34:06.813 回答