Error #2025 AS3

怎甘沉沦 提交于 2019-12-11 07:41:38

问题


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:

  1. my bullets are defined as MovieClips ,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?

  1. 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))) {
          countHits[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)


回答1:


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));


来源:https://stackoverflow.com/questions/5955411/error-2025-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!