AddChild and removeChild in ActionScript 3 does not work properly
I am wondering that why I get Error #1009: Cannot access a property or method of a null object reference
by using removeChild() function in my as3 code which is as following:
public class MyGame extends MovieClip{
private var myMovieClip:MovieClip;
public function add():void{
myMovieClip = new MyMovieClip();
addChild(myMovieClip);
}
public function remo开发者_如何学运维ve():void{
removeChild(myMovieClip);
}
}
in the remove function, I wanna remove myMovieClip object and then create a new one and use it for continuing of my application.
Any idea will be appreciated Thanks
It appears that myMovieClip is null at the time you are calling remove...
This might be a little safer
function DetachFromParent(mc:MovieClip):void
{
if(mc!= null && mc.parent != null)
{
mc.parent.removeChild(mc);
}
}
You need to check if you even need to remove myMovieClip
:
public function remove():void{
if(myMovieClip) // myMovieClip is not null or undefined, remvove it
removeChild(myMovieClip);
}
精彩评论