题
我正在创建Pacman风格的游戏。我正在尝试使用RemoVechild()删除MovieClip的实例。当MovieClip实例“框”击中MovieClip实例“圆”时,将从舞台上删除圆圈。
我在下面收到以下错误:
参数:错误#2025:所提供的DisplayObject必须是呼叫者的孩子。在flash.display :: displayObjectContainer/removeChild()in move/eatcircle()
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Move extends MovieClip {
var ScoreObjects:Array = new Array(); // creates ScoreObjects array
private var inertia:int=8; //amount of friction
var score_field:String;
//var point:MovieClip;
// Constructor--------------------------------------------------------------------
public function Move() {
init();
}
// function init -----------------------------------------------------------------
function init():void {
//stage.frameRate=60;
var score_field:String="";
ScoreObjects[0] = new Circle();
ScoreObjects[0].amount = 1; // amount of point
ScoreObjects[0].name = "circle";
stage.addEventListener(Event.ENTER_FRAME, frameloop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
box.addEventListener(Event.ENTER_FRAME, eatCircle);
wall.addEventListener(Event.ENTER_FRAME, hitWall);
stage.addChild(ScoreObjects[0]); // add Score Objects to stage ------------------------------
trace(ScoreObjects[0]);
ScoreObjects[0].x = 105;
ScoreObjects[0].y = 233;
}
// function eatCircle --------------------------------------------------------------
function eatCircle(event:Event):void {
if (box.hitTestObject(ScoreObjects[0])) {
trace ("I ate the circle");
removeChild(ScoreObjects[0]);
//calcScore();
} else {
trace ("I didn't eat the circle");
}
}
}// end of class
}// end of package
解决方案
if((ScoreObjects[0] as Circle)&&((ScoreObjects[0] as Circle).parent!=null))
{
stage.removeChild(ScoreObjects[0]);
}
其他提示
我没有AS3编译器可以测试这一点,但是由于您确实如此 stage.addChild(ScoreObjects[0])
我相信你应该做 stage.removeChild(ScoreObjects[0])
?
不隶属于 StackOverflow