0

在我的场景中,我有一个动态加载图像的精灵“imgSprite”。我在这个名为“矩形”的精灵内的左下角附近画了一个小盒子。imageSprite 也可以拖放。当我单击图像时,我将其设置为显示矩形,但我无法弄清楚如何将其隐藏,单击 imgSpirte 之外的某个位置。

var imgSprite:Sprite = new Sprite();
imgSprite.addChild(image);
MovieClip(root).addChild(imgSprite);

/* DELETE BUTTON */
var rectangle:Sprite = new Sprite();
rectangle.graphics.beginFill(0xFF0000);
rectangle.graphics.drawRect(0, 0, 20,20);
rectangle.graphics.endFill();
rectangle.y=imgSprite.y+imgSprite.height;
rectangle.x=imgSprite.x-20;
imgSprite.addChild(rectangle);

rectangle.addEventListener(MouseEvent.CLICK, function() {
    rectangle.parent.parent.removeChild(imgSprite);
});

imgSprite.addEventListener("mouseDown", function() {
    imgSprite.startDrag();
    rectangle.visible = true;
});

stage.addEventListener("mouseUp", function() {
    imgSprite.stopDrag();
});

/*
imgSprite.addEventListener("mouseOut", function() {
    var timer = setInterval(deSelect,3000);
    function deSelect(){
      rectangle.visible = false;
      clearInterval(timer);
    }
});
*/
4

1 回答 1

0

您可以为舞台尝试临时监听器。说,像这样:

if (rectangle) rectangle.visible=false; // or it won't trigger
imgSprite.addEventListener("mouseDown",onMouseDown);
function onMouseDown(e:MouseEvent):void {
    if (rectangle) if (!rectangle.visible) 
        stage.addEventListener("mouseDown",removeRectangle);
    imgSprite.startDrag();
    rectangle.visible=true;
    e.stopPropagation(); // this is needed to not get the rectangle removed as soon as you click on imgSprite
}
function removeRectangle(e:MouseEvent):void {
    if (rectangle) rectangle.visible=false;
    stage.removeEventListener("mouseDown",removeRectangle);
}

它的工作原理是这样的:只要你点击imgSprite,一个监听器就会被分配到舞台并且矩形是可见的。如果imgSprite再次单击,矩形已经可见,因此不会添加额外的侦听器。还e.stopPropagation()可以防止现有侦听器被触发并隐藏矩形。并且只要您单击其他位置但 on imgSprite,侦听器就会被触发,将自己从舞台上移除并隐藏rectangle

于 2013-10-14T13:13:52.800 回答