快樂前萬聖節前夕大家:)使用removeChild方法時得到錯誤
我今天的問題是我得到的時候刪除一個子對象的DisplayObject錯誤。我有代碼將啓動(addChild)視頻容器和視頻控件以及添加關閉按鈕。 Now the close button works fine and everything,刪除視頻和控制,我能夠再次選擇其他的視頻,但是當您單擊關閉第二次我得到這個錯誤:
引發ArgumentError:錯誤#2025:提供的DisplayObject必須的孩子呼叫者,召集者。在flash.display一::級DisplayObjectContainer/removeChild之()
所以我已經縮小的問題,我刪除videoContainer(持有該視頻對象)
我的代碼,以播放視頻:
public function videoSwitch(videoName):void
{
nv.closeOut();
nv.resetNav = false;
if (!videoPlaying)
{
vc = new VideoClass(videoName, videoHolder);
vc.addEventListener("KillMovie", removePlayer);
container.addChild(videoContainer);
container.addChild(vc);
//container.addChildAt(videoContainer, 1);
//container.addChildAt(vc, 2);
videoPlaying = true;
closeVideo();
}
else if (videoPlaying)
{
vc.clearSource();
container.removeChild(videoContainer);
container.removeChild(vc);
vc = new VideoClass(videoName, videoHolder);
vc.addEventListener("KillMovie", removePlayer);
container.addChild(videoContainer);
container.addChild(vc);
//container.addChildAt(videoContainer, 1);
//container.addChildAt(vc, 2);
closeVideo();
}
trace("videoPlaying = "+videoPlaying+"\r");
}
收出視頻播放器代碼: 你可以在我的意見看其他代碼我試過了,但仍然得到錯誤。
function closeVideo():void
{
closeBtn.visible = true;
closeBtn.x = 770;
closeBtn.y = 20;
closeBtn.buttonMode = true;
container.addChild(closeBtn);
closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);
function closeButtonClicked(event:MouseEvent):void
{
vc.clearSource();
container.removeChild(videoContainer);
//container.removeChildAt(videoContainer, 1);
container.removeChild(vc);
videoPlaying = false;
closeBtn.visible = false;
}
}
現在my movie工作正常,但我擔心,這個錯誤在後臺發生的事情(和顯示在我的輸出窗口上)最終會導致一個問題,別的地方:(
預先感謝在這一個任何眼睛:)
UPDATE: 修復!問題是我刪除了殺VC聽衆,卻忘了刪除愚蠢關閉Mouse_Event聽衆:(
function addCloseButton():void
{
container.addChild(closeBtn);
closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);
function closeButtonClicked(event:MouseEvent):void
{
videoPlaying=false;
vc.clearSource();
removeContainerChildren(); // <- thx Joel!
closeBtn.removeEventListener(MouseEvent.MOUSE_UP, closeButtonClicked);
//^ Forgot this line - thx Jotham!
container.removeChild(closeBtn);
}
}
不知道這個圖形幫助,但:
你不需要多次調用closeVideo()。它應該只被調用一次。初始化您的容器時,因爲它對每個視頻都很常見。只需調用closeButtonClicked()函數即可清除視頻源,然後移除videoClass和videoContainer,而不是關閉視頻。通過多次調用closeVideo,您正在爲closeButton創建多個處理程序,這會導致此運行時錯誤。 – bhups 2009-10-31 08:39:32