祝大家万圣节快乐:)
我今天的问题是删除子对象时遇到的 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工作正常,但我担心在后台发生的这个错误(并显示在我的输出窗口中)最终会在其他地方导致问题:(
预先感谢您关注此内容! :)
更新: 已修复!问题是我删除了kill VC监听器,但忘记删除了愚蠢的Close Button 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);
}
}
不知道这张图是否有帮助,但是:
最佳答案
这是避免该错误的一种方法:
public function videoSwitch(videoName):void
{
nv.closeOut();
nv.resetNav = false;
if (videoPlaying)
{
vc.clearSource();
removeContainerChildren()
}
addContainerChildren();
closeVideo();
}
protected function removeContainerChildren():void
{
if(container.contains(videoContainer))
container.removeChild(videoContainer);
if(container.contains(vc))
{
container.removeChild(vc)
vc.removeEventListener("KillMovie", removePlayer)
}
}
protected function addContainerChildren():void
{
videoPlaying = true;
vc = new VideoClass(videoName, videoHolder);
vc.addEventListener("KillMovie", removePlayer, false, 0, true);
container.addChild(videoContainer);
container.addChild(vc);
trace("videoPlaying = "+videoPlaying+"\r");
}
关于flash - 使用removeChild方法时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1651835/