AS3 - Error #2025: The supplied DisplayObject must be a child of the caller

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

After 2 days of trying and searching for an answer I still didn't found it. I keep getting Error #2025: The supplied DisplayObject must be a child of the caller. I'm making a game where if the user hits an enemy, the enemy get destroyed. The code:

My main class

package classes {    import flash.display.MovieClip; import flash.events.Event; import flash.utils.Timer;  public class Main extends MovieClip {     var enemyTimer:Timer;      public function Main()      {         var user:ship = new ship();         addChild(user);         user.name = "user";         user.initialize();          enemyTimer = new Timer(2000);         enemyTimer.addEventListener("timer", sendEnemy);         enemyTimer.start();          function sendEnemy(e:Event)         {             var badboy:enemy = new enemy();             addChild(badboy);             badboy.initialize();         }     } } }

the enemy class

package classes.enemy {    import flash.display.MovieClip; import flash.events.Event;  public class Enemy extends MovieClip {     var speed:Number;      public function initialize()     {         addEventListener("enterFrame", enterFrame);     }      public function Enemy()      {         this.x = 700;         this.y = Math.random()*200 + 50;         speed = Math.random()*5 + 5;     }      function enterFrame(e:Event)     {         this.x -= speed;          if(this.hitTestObject(parent.getChildByName("user")))         {             kill();         }     }      function kill()     {         removeEventListener("enterFrame", enterFrame);         stage.removeChild(this);     } } }

The files are in different folders (classes > Main.as & classes.enemy.Enemy.as), don't know if that has anything to do with it.

Any help would be appreciated.

回答1:

That's probably because you try to remove the Enemy MovieClip from stage, that it is not a (direct) child of.

I suggest you change this:

stage.removeChild(this);

to this:

this.parent.removeChild(this);

When you have a reference to a DisplayObject, like this in this case, you can always remove it from its parent, even if you don't know what that parent is. Or rather, you can remove it if you know it is on the display list, so you could also first check that it is, by doing:

if(this.parent) {     this.parent.removeChild(this); }


回答2:

I have got the solution: Just copy and paste the script and create few essentials symbols on stage, and in library; then, check it.

import flash.display.MovieClip;  var myArr:Array = []; abc.startDrag(true); var mymc:MovieClip = new MovieClip(); addChild(mymc);  init();  function init() {     for (var i=0; i<25; i++)     {         var par:Particle = new Particle();         par.x = Math.random() * stage.stageWidth;         par.y = Math.random() * stage.stageHeight;         mymc.addChildAt(par,0);         myArr.push(par);     }     this.addEventListener(Event.ENTER_FRAME, hitTes); }  function hitTes(e:Event):void {     for (var j=0; j<myArr.length; j++)     {         if (abc.hitTestObject(myArr[j]))         {             if (myArr[j].parent)             {                 myArr[j].parent.removeChild(myArr[j]);             }          }     } }


回答3:

I think you misplaced a method. See if this revision helps. (Also note that this does not include any cleanup of objects, which will eventually be a problem.)

package classes {    import flash.display.MovieClip; import flash.events.Event; import flash.utils.Timer;  public class Main extends MovieClip {     public var enemyTimer:Timer;     public var user:ship = new ship();     public var badboy:enemy = new enemy();      public function Main()      {         user = new ship();         addChild(user);         user.name = "user";         user.initialize();          enemyTimer = new Timer(2000);         enemyTimer.addEventListener("timer", sendEnemy);         enemyTimer.start();      }       // *** I moved this out of the constructor:     public function sendEnemy(e:Event):void     {         badboy = new enemy();         badboy.name = "badboy"; // you probably have to make this unique, though.         addChild(badboy);         badboy.initialize();     } } }


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