动作脚本3:mouse_over& mouse_out导致错误#1009?

时间:2012-06-20 15:34:11

标签: actionscript-3

当我将鼠标悬停在buttonSprite上时,会将displaySprite添加到舞台上,当我将mouseSprite鼠标移出时,将删除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;

}

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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;
}

答案 2 :(得分: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;
    }

}