0

当我将鼠标悬停在 buttonSprite 上时,它会将 displaySprite 添加到舞台,当我将鼠标移出 buttonSprite 时,会删除 displaySprite。

我的问题是,当我将鼠标快速移出 buttonSprite 几次时,不会删除 displaySprite 并显示错误消息(错误#1009)。即使我输入“displaySprite = null”,它仍然无法正常工作。有什么建议么?谢谢

var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);

var displaySprite:Sprite;

function overSprite(e:MouseEvent):void{

    displaySprite = new Sprite();
    addChild(displaySprite);
    buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);

}
function outSprite(e:MouseEvent):void{

    removeChild(displaySprite);
    displaySprite = null;

}
4

3 回答 3

1

不能保证事件会按顺序触发。
在您的情况下,您不必多次实例化 displaySprite。
只是不要将其清空,对象就会在那里。

var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);

//you only need to create it once.
var displaySprite:Sprite = new Sprite();

function overSprite(e:MouseEvent):void{
    addChild(displaySprite);
    buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);
}
function outSprite(e:MouseEvent):void{
    removeChild(displaySprite);
}
于 2012-06-20T19:34:45.083 回答
0

这个怎么样?

var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);

var displaySprite:Sprite;
addChild(displaySprite);
displaySprite.visible=false;

function overSprite(e:MouseEvent):void
{
    displaySprite.visible = true;
}

function outSprite(e:MouseEvent):void
{
    displaySprite.visible = false;
}
于 2012-06-20T15:49:28.147 回答
0

问题是当您将鼠标悬停在显示对象上时,MouseEvent.MOUSE_OVER 会被多次调度。您应该尝试的是 MouseEvent.ROLL_OVER 和 MouseEvent.ROLL_OUT 这两个事件被调度一次。

类似的东西:

var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.ROLL_OVER, overSprite);
buttonSprite.addEventListener(MouseEvent.ROLL_OUT, outSprite);
var displaySprite:Sprite;

function overSprite(e:MouseEvent):void
{
    if(!displaySprite)
    {
       displaySprite = new Sprite();
       addChild(displaySprite);
    }

}

function outSprite(e:MouseEvent):void{
    if(displaySprite)
    {
        removeChild(displaySprite);
        displaySprite = null;
    }

}
于 2012-06-20T15:54:45.950 回答