I'm trying to build game like chicken invaders and i get this eror :
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at superstudent7_fla::MainTimeline/moveBullet()
this problem occurs when my space ship shoots.
to solve this , i need to know 2 things:
my bullets are defined as
MovieClip
s ,and they are not on stage.. so i brought them to the stage like this:function shooting(e:Event):void { var Bullet:bullets = new bullets(); // bullets is class name of my movieClip ... ... ... addChild(Bullet); Bullet.addEventListener(Event.ENTER_FRAME,moveBullet); }//End of shooting
i need to know if its ok add the bullet to the stage like this? or there is another way?
here is the code that makes the bullet move:
function moveBullet(e:Event):void { e.target.y -=10; for(var i=0;i<enemy.numChildren;i++) { if(e.target.hitTestObject(enemy.getChildAt(i))) { c开发者_运维百科ountHits[i]=countHits[i]+1; e.target.removeEventListener(Event.ENTER_FRAME,moveBullet); removeChild(MovieClip(e.target)); //here is the problem ... .... .... }//End if }//End for ...... ..... }//End of moveBullet
enemy- is the container of all The Enemies (movieClips)
It seems that the class that has the moveBullet
function is not the same as the container of all the enemies, so you're removing a MovieClip that is not a child of the container, as the error message explains. You could do this:
if(MovieClip(e.target).parent)
{
MovieClip(e.target).parent.removeChild(MovieClip(e.target));
}
That removes the target of the Event from whatever parent it is added to. And doesn't remove it if it's not added to the display list anywhere, so you don't get other errors.
Alternatively, since you state that enemy
is the container, you could do this:
enemy.removeChild(MovieClip(e.target));
精彩评论