1

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: currentContent.addChild(currentLoader); addChild(currentContent); on load and then just currentContent.removeChild(currentLoader); removeChild(currentContent); 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! :)

Lauren

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

}
Lauren Tilter
  • 81
  • 1
  • 2
  • It is not clear if you want for the replace_btn to have 2 different actions (eventlisteners). If it is so, in replaceImage function you should remove the listener replaceImage so it is not called when it is not needed. Also, if there is a second listener, remove it before adding replaceImage as listener. Hope it helps – Mircea Apr 26 '12 at 11:29

1 Answers1

0

This currentContent.removeChild(currentLoader); should be fine, but this removeChild(currentContent); is most likely the problem source. Because you added it as currentContent.addChild(currentLoader), you must remove the container var (currentContent) with it as well.

paddotk
  • 1,359
  • 17
  • 31