Flash AS3 - DisplayObject Argument Error - Trying to Remove and Add Loaded Content

Hi

I am working with flash and as3 to try and get a photo loader/viewer/upload tool built. I am using file reference to handle the browse/load/upload functions. My current stage has 2 loader boxes that i load the user selected image into. I need to be able to remove the image though and re-select another if the user decides.

So on the file loaded event i have this code:


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

And on my replace button i have:


    replace_btn.addEventListener(MouseEvent.CLICK, replaceImage);
	
	function replaceImage(event:Event):void {
	    currentContent.removeChild(currentLoader);
		removeChild(currentContent);
	}

On pressing the replace button once it works fine but when i load another image into the loader - the next time i press the replace button (or any subsequent time) i get the following argument error in my flash as3 file:


    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
	at flash.display::DisplayObjectContainer/removeChild()
	at MethodInfo-13()

Does anyone know what this means? It’s odd that it only happens on the second time onwards not at first. I thought that if i have: [B]currentContent.addChild(currentLoader); addChild(currentContent);[/B] on load and then just [B]currentContent.removeChild(currentLoader); removeChild(currentContent);[/B] on the replace function it would work?

Full as3 code is shown below if that also helps

I have only been learning flash for 3-4 months so please go easy on me and i apologise if my code is not done in the best way! :slight_smile:

Lauren


// Imports.
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;


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