PDA

View Full Version : removeChild problem



tjsl2
March 12th, 2009, 12:00 PM
Hi,

I'm having a problem with removeChild. I'm a newbie to AS3 so my problem is an embarrassingly simple one, but you have to start somewhere.

I'm creating a sprite (_spHolder1) in the DisplayManager class, then within the same class I'm instantiating another class called StartTrans. Within the StartTrans class I want to delete the sprite _spHolder1.

I'm slightly confused with the fact that I'm able to trace _spHolder1 from the StartTrans class, but unable to use removeChild

The error I get is:

Error #1009: Cannot access a property or method of a null object reference.

I've stripped it right down to the problematic code.

Any help would be much appreciated.

Cheers,

tjsl2



package {

import flash.display.*;

import com.tobylow.display.animation.StartTrans;

public class DisplayManager extends Sprite {

public var _spHolder1:Sprite;

public function DisplayManager() {

creatBasicDisplay();
}

private function creatBasicDisplay():void{

_spHolder1 = new Sprite();
addChild(_spHolder1);

startTransition();
}

private function startTransition():void{

var _startTrans = new StartTrans(_spHolder1);
}
}



package{

import flash.display.*;

public class StartTrans extends Sprite{

private var _sp1:Sprite;

public function StartTrans(sp1:Sprite):void{

_sp1 = sp1;
init();
}

private function init():void{

trace(_sp1);
removeChild(_sp1);
_sp1 = null;
}
}
}

irrationalistic
March 12th, 2009, 03:06 PM
Instead of doing removeChild(_sp1); try doing _sp1.parent.removeChild(_sp1);. My only guess here is that you aren't specifying the proper parent to remove _sp1 from, so hopefully telling it where _sp1 resides will help!

tjsl2
March 13th, 2009, 12:00 PM
That works perfectly, thanks.

Nice portfolio by the way.