Remove Child and Global Variables
Hi all, Have a question about remove child, in AS3.
Code looks like this:
import flash.utils.getDefinitionByName;
import flash.display.MovieClip;
testButton1.addEventListener(MouseEvent.CLICK, clickFunction1);
testButton2.addEventListener(MouseEvent.CLICK, clickFunction2);
testButton3.addEventListener(MouseEvent.CLICK, clickFunction3);
testButton4.addEventListener(MouseEvent.CLICK, clickFunction4);
//
var movieName:String="anim1";
var C:Class=Class(getDefinitionByName(movieName));
var anim:MovieClip=new C();
stage.addChild(anim);
anim.stop();
var counterX:int=20;
//
function changeAnim(n){
stage.removeChild(anim);
//trace("animx>>"+stage.anim.x)
var i:int=n;
var movieName:String="anim"+i;
var C:Class=Class(getDefinitionByName(movieName));
var anim:MovieClip=new C();
stage.addChild(anim);
anim.stop();
anim.x=counterX;
anim.y=counterX;
counterX++;
trace("animx<<"+anim.x)
}
function clickFunction1(evt:MouseEvent):void {changeAnim(1);}
function clickFunction2(evt:MouseEvent):void {changeAnim(2);}
function clickFunction3(evt:MouseEvent):void {changeAnim(3);}
function clickFunction4(evt:MouseEvent):void {changeAnim(4);}
The simple idea is that when you press one of four buttons(testButton1,2,3,4), an animation (anim) is removed then recreated at root level from the library. For some reason the removeChild function doesn't work with the following error, the whole thing works fine if the remove child is commented out, but we end up with lots of movie Clips :
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at addchild_fla::MainTimeline/changeAnim()
at addchild_fla::MainTimeline/clickFunction1()
Thanks in advance!
