Skip to main content
stuartf76130223
Inspiring
February 14, 2017
Answered

Remove Child and Global Variables

  • February 14, 2017
  • 2 replies
  • 560 views

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!

This topic has been closed for replies.
Correct answer Colin Holgate

You're using the same variable name as a timeline one and as a function one, and I think that there will be cases where it reads null because it's picking up the wrong variable.

I think you can reuse the timeline variables inside the function without a problem. So, try changing these lines in the function:

  var movieName:String="anim"+i; 

  var C:Class=Class(getDefinitionByName(movieName)); 

  var anim:MovieClip=new C(); 

to:

  movieName = "anim"+i; 

  C = Class(getDefinitionByName(movieName)); 

  anim = new C();  

2 replies

Ned Murphy
Legend
February 14, 2017

The first thing you might try is to avoid declaring two anim vars.  Just have the one that is outside the function so that the function is more certain which one it is trying to target.  inside the function just have...  anim = new C();

Colin Holgate
Colin HolgateCorrect answer
Inspiring
February 14, 2017

You're using the same variable name as a timeline one and as a function one, and I think that there will be cases where it reads null because it's picking up the wrong variable.

I think you can reuse the timeline variables inside the function without a problem. So, try changing these lines in the function:

  var movieName:String="anim"+i; 

  var C:Class=Class(getDefinitionByName(movieName)); 

  var anim:MovieClip=new C(); 

to:

  movieName = "anim"+i; 

  C = Class(getDefinitionByName(movieName)); 

  anim = new C();  

stuartf76130223
Inspiring
February 14, 2017

Thanks! this now works and the removeChild "removes".