可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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(); } } }