2012-06-20 91 views
0

當我將鼠標放在buttonSprite上時,它將displaySprite添加到舞臺上,當我將buttonSprite鼠標移出時,displaySprite被移除。Actionscript 3:mouse_over&mouse_out導致錯誤#1009?

我的問題是,當我快速地將鼠標移出並移出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; 

} 

回答

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; 
} 
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; 
    } 

} 
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); 
}