Here's my updated code: private var moduleloader:ModuleLoader; private var unload_button:Button; private var vbox:VBox; private var _aChildren:Array=[];
private function unloadclick(event:Event):void { var iIndexClicked:int = parseInt(event.target.id); modulestile.removeChild(_aChildren[iIndexClicked]); *error* modulestile.removeChild(event.target); *error* } public function AddColumnGraphPanel():void { var iNewIndex:int = _aChildren.length; vbox = new VBox(); modulestile.addChild(vbox); moduleloader = new ModuleLoader(); moduleloader.url = "test.swf"; _aChildren[iNewIndex] = vbox.addChild(moduleloader); unload_button = new Button(); unload_button.id = String(iNewIndex); unload_button.label = "unload"; unload_button.addEventListener(MouseEvent.CLICK, unloadclick); vbox.addChild(unload_button); } <mx:Tile id="modulestile" direction="horizontal"> </mx:Tile> I have the following error in the script: 1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject. Thanks again :) -David --- In flexcoders@yahoogroups.com, "Tracy Spratt" <[EMAIL PROTECTED]> wrote: > > That is correct, you are not storing references correctly. First I will > explain what is wrong with your code, but then, after seeing what you > are trying to do, will suggest a slightly different approach. > > > > First, you are mixing Array, which is accessed by index and "associative > array", which is an Object and accessed by property name: > > private var _oChildren:Object = new Object(); > > > > Second, you are creating a new instance of that each time you click > addModule. This overwrites what was in there before. Don't do that. > > > > Third, you need a unique name for each reference you add to the > associative array. Don't use "one" every time, that just puts a new > reference in the "one" property. You can use anything you want for this > property name. Pick something that is meaningful. How will you > identify which child you want to remove? Is it purely numerical? If so > you can use "1", "2", etc. Usually there is some string that is logical > for this, like an id, or an image's base file name. whatever, remember > it must uniquely identify the instance you want to later remove. > > > > Now, after looking at your code a bit more, and seeing that you do not > have a clear string or id you can use for identifying the references, I > am gong to suggest a different approach. > > private var _aChildren:Array= []; //declare an initialize > an Array > > > > ... > > public function addModule():void { > var iNewIndex:int = _aChildren.length; //this will be the next > available index in the array > > button = new Button(); > > button.id = String(iNewIndex); //NOTE: you can NOT use this id as a > reference, but you CAN access its value > button.label = "unload"; > button.addEventListener(MouseEvent.CLICK, onClickUnload); > tiletest.addChild(button); > > moduleloader = new ModuleLoader(); > moduleloader.url = "test.swf"; > _aChildren[iNewIndex] = tiletest.addChild(moduleloader); //NOTE: the > associated button id property value matches the index of the array ref > }// addModule > > > > private function onClickUnload (event:Event):void { > var iIndexClicked:int = parseInt(event.target.id); > > tiletest.removeChild(_aChildren[iIndexClicked]); > tiletest.removeChild(event.target); > }// onClickUnload > > > > Try this. it is untested. > > > > Tracy > > > > ________________________________ > > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On > Behalf Of sailorsea21 > Sent: Friday, October 17, 2008 11:45 AM > To: flexcoders@yahoogroups.com > Subject: [flexcoders] Re: sailorsea21 - RemoveChild question. > > > > Hi Tracy, > > I don't think I'm successfully storing the reference of the children > into the array. > > This is what I had tried: > > private var moduleloader:ModuleLoader; > private var button:Button; > private var _aChildren:Array; > > private function click(evt:MouseEvent):void { > tiletest.removeChild(_aChildren["one"]); > tiletest.removeChild(button); > } > > public function AddModule():void { > moduleloader = new ModuleLoader(); > moduleloader.url = "test.swf"; > button = new Button(); > button.label = "unload"; > button.addEventListener(MouseEvent.CLICK, click); > tiletest.addChild(button); > _aChildren = new Array(); > _aChildren["one"] = tiletest.addChild(moduleloader); > } > > But I keep getting the "ArgumentError: Error #2025: The supplied > DisplayObject must be a child of the caller." error. > > Thanks again. > > -David > > --- In flexcoders@yahoogroups.com <mailto:flexcoders% 40yahoogroups.com> > , "Tracy Spratt" <tspratt@> wrote: > > > > Well, you did not do what I suggested. > > > > > > > > What do you think is in "moduleloader" when you click the remove > button? > > The last thing you put in it. > > > > > > > > Re-read my response. > > > > > > > > Tracy > > > > > > > > ________________________________ > > > > From: flexcoders@yahoogroups.com <mailto:flexcoders% 40yahoogroups.com> > > [mailto:flexcoders@yahoogroups.com <mailto:flexcoders% 40yahoogroups.com> > ] On > > Behalf Of sailorsea21 > > Sent: Thursday, October 16, 2008 4:38 PM > > To: flexcoders@yahoogroups.com <mailto:flexcoders% 40yahoogroups.com> > > Subject: [flexcoders] Re: sailorsea21 - RemoveChild question. > > > > > > > > Hi Tracy, thank you for your quick response. > > I'm a little stuck on this one... I can't get it to work... > > > > I'm now able to load a module and unload it. But if I load several > > modules, I'm only able to unload the latest one. > > > > When I try unloading the other loaded modules I receive the follow > > error: > > > > ArgumentError: Error #2025: The supplied DisplayObject must be a > > child of the caller. > > at flash.display::DisplayObjectContainer/removeChild() > > at > > > mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::$remo > <http://www.adobe.com/2006/flex/mx/internal::$remo> > > <http://www.adobe.com/2006/flex/mx/internal::$remo > <http://www.adobe.com/2006/flex/mx/internal::$remo> > > > veChild()[E:\dev\3.1.0 > > \frameworks\projects\framework\src\mx\core\UIComponent.as:5074] > > at mx.core::Container/removeChild()[E:\dev\3.1.0 > > \frameworks\projects\framework\src\mx\core\Container.as:2267] > > at content_module_template_blank/click() > > [C:\Inetpub\wwwroot\LWY\UI\UI Score\UI Score 004 > > \src\content_module_template_blank.mxml:29] > > > > This is my code: > > > > import mx.states.RemoveChild; > > import mx.modules.ModuleLoader; > > import mx.controls.Button; > > > > private var moduleloader:ModuleLoader; > > private var button:Button; > > > > public function loadmodule():void { > > moduleloader = new ModuleLoader(); > > tiletest.addChild(moduleloader); > > moduleloader.url = "test.swf"; > > button = new Button(); > > button.label = "unload"; > > button.addEventListener(MouseEvent.CLICK, click); > > tiletest.addChild(button); > > } > > > > private function click(evt:MouseEvent):void { > > tiletest.removeChild(moduleloader); > > tiletest.removeChild(button); > > } > > > > <mx:Tile width="100%" height="100%" id="tiletest"/> > > <mx:LinkButton label="Load" click="loadmodule()" buttonMode="true"/> > > > > Thank you very much. > > > > -David. > > >