Actionscript 3 Flash AS3-显示对象参数错误-尝试删除和添加加载的内容

Actionscript 3 Flash AS3-显示对象参数错误-尝试删除和添加加载的内容,actionscript-3,flash,removechild,displayobject,argument-error,Actionscript 3,Flash,Removechild,Displayobject,Argument Error,我正在使用flash和as3来尝试构建一个照片加载/查看/上传工具。我使用文件引用来处理浏览/加载/上载功能。我的当前阶段有2个加载框,我将用户选择的图像加载到其中。我需要能够删除图像,但重新选择另一个如果用户决定 因此,在文件加载事件中,我有以下代码: // Load Image into Editor Function. function onMovieClipLoaderComplete(event:Event):void { var loadedContent:DisplayOb

我正在使用flash和as3来尝试构建一个照片加载/查看/上传工具。我使用文件引用来处理浏览/加载/上载功能。我的当前阶段有2个加载框,我将用户选择的图像加载到其中。我需要能够删除图像,但重新选择另一个如果用户决定

因此,在文件加载事件中,我有以下代码:

// Load Image into Editor Function.
function onMovieClipLoaderComplete(event:Event):void {
    var loadedContent:DisplayObject=event.target.content;
    currentLoader =event.target.loader as Loader;

    currentLoader.x=currentX;
    currentLoader.y=currentY;

    currentContent.buttonMode=true;
    currentContent.addChild(currentLoader);
    addChild(currentContent);

    currentLoader.mask = currentMask;

    addChild(replace_btn);
}
在我的“替换”按钮上,我有:

replace_btn.addEventListener(MouseEvent.CLICK, replaceImage);

function replaceImage(event:Event):void {
    currentContent.removeChild(currentLoader);
    removeChild(currentContent);
}
在按下“替换”按钮时,它可以正常工作,但当我将另一个图像加载到加载程序中时-下次按下“替换”按钮或任何后续时间,我的flash as3文件中出现以下参数错误:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-13()
有人知道这是什么意思吗?奇怪的是,它只发生在第二次以后,而不是第一次。我想如果我有: currentContent.addChildcurrentLoader;添加当前内容; 加载,然后 currentContent.removeChildcurrentLoader;删除ChildCurrentContent; 在replace函数上,它是否可以工作

完整的as3代码如下所示,如果这也有帮助的话

我只学了3-4个月的flash,所以请对我宽容一点,如果我的代码没有以最好的方式完成,我深表歉意!:

劳伦

// Set Start Up Values.
var image1_loader:Loader = new Loader();
var image1_content:Sprite = new Sprite();
var image1_mask:Sprite = new Sprite(); image1_mask.graphics.beginFill(0x000000,1);         image1_mask.graphics.drawRect(54, 59, 330, 330); image1_mask.graphics.endFill();

var image2_loader:Loader = new Loader();
var image2_content:Sprite = new Sprite();
var image2_mask:Sprite = new Sprite(); image2_mask.graphics.beginFill(0x000000,1);     image2_mask.graphics.drawRect(384, 59, 330, 165); image2_mask.graphics.endFill();

var currentBtn;
var currentLoader;
var currentContent;
var currentMask;
var currentX;
var currentY;

replace_btn.visible=false;


// Define FileReference.
 var canvasImage:FileReference;


// Image Buttons Function.
image1_btn.addEventListener(MouseEvent.CLICK, start_fileRef);
image2_btn.addEventListener(MouseEvent.CLICK, start_fileRef);

image1_content.addEventListener(MouseEvent.MOUSE_DOWN, setCurrentSelection);
image2_content.addEventListener(MouseEvent.MOUSE_DOWN, setCurrentSelection);

function setCurrentSelection(e:MouseEvent):void {
if (e.currentTarget===image1_content){currentContent=image1_content;}
if (e.currentTarget===image2_content){currentContent=image2_content;}
}


// Browse File Function.
function start_fileRef(e:MouseEvent):void {
trace("onBrowse");
if (e.target===image1_btn){currentBtn=image1_btn; currentLoader=image1_loader;     currentContent=image1_content; currentMask=image1_mask; currentX=54; currentY=59;}
if (e.target===image2_btn){currentBtn=image2_btn; currentLoader=image2_loader; currentContent=image2_content; currentMask=image2_mask; currentX=384; currentY=59;}

canvasImage=new FileReference();
canvasImage.addEventListener(Event.SELECT, onFileSelected);
var imageTypeFilter:FileFilter = new FileFilter("JPG/PNG Files","*.jpeg; *.jpg;*.gif;*.png");
canvasImage.browse([imageTypeFilter]);
}

// Selected File Function.
function onFileSelected(event:Event):void {
trace("onFileSelected");
canvasImage.addEventListener(Event.COMPLETE, onFileLoaded);
canvasImage.addEventListener(ProgressEvent.PROGRESS, onProgress);

var canvasImageName = canvasImage.name;
canvasImage.load();
}


// File Progress Function.
function onProgress(event:ProgressEvent):void {
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
trace("loaded: "+percentLoaded+"%");
}

// File Loaded Function.
function onFileLoaded(event:Event):void {
var fileReference:FileReference=event.target as FileReference;

var data:ByteArray=fileReference["data"];
trace("File loaded");
var movieClipLoader:Loader=new Loader();
movieClipLoader.loadBytes(data);
movieClipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieClipLoaderComplete);

canvasImage.removeEventListener(Event.COMPLETE, onFileLoaded);
}

// Load Image into Editor Function.
function onMovieClipLoaderComplete(event:Event):void {
var loadedContent:DisplayObject=event.target.content;
currentLoader =event.target.loader as Loader;

currentLoader.x=currentX;
currentLoader.y=currentY;

currentContent.buttonMode=true;
currentContent.addChild(currentLoader);
addChild(currentContent);

currentLoader.mask = currentMask;

addChild(replace_btn);


// Reveal Retry Button over Hover Function //
currentContent.addEventListener(MouseEvent.ROLL_OVER, hover);
replace_btn.addEventListener(MouseEvent.ROLL_OVER, hover);
currentContent.addEventListener(MouseEvent.ROLL_OUT, unhover);
replace_btn.addEventListener(MouseEvent.ROLL_OUT, unhover);

function hover(event:Event):void {
    replace_btn.visible=true;
}
function unhover(event:Event):void {
    replace_btn.visible=false;
}

replace_btn.addEventListener(MouseEvent.CLICK, replaceImage);

function replaceImage(event:Event):void {
    currentContent.removeChild(currentLoader);
    removeChild(currentContent);
}

}

此currentContent.removeChildcurrentLoader;应该没问题,但这删除了childcurrent的内容;这很可能是问题的根源。由于您将其添加为currentContent.addChildcurrentLoader,因此必须同时删除容器var currentContent。

不清楚您是否希望替换btn具有两个不同的操作eventlisteners。如果是这样,在replaceImage函数中,您应该删除侦听器replaceImage,以便在不需要它时不调用它。另外,如果还有第二个侦听器,请在添加replaceImage作为侦听器之前将其删除。希望能有帮助