这是我的代码。请任何人都可以帮助我。我是初学者。
它给了我这个错误
ArgumentError:错误#2025:提供的DisplayObject必须是子级 来电者。在flash.display :: DisplayObjectContainer / removeChild() 在final2_fla :: MainTimeline / loop()
import flash.events.Event;
import flash.events.MouseEvent;
var kUp:Boolean = false;
var kDn:Boolean = false;
var kLt:Boolean = false;
var kRt:Boolean = false;
var speedX:Number = 0;
var speedY:Number = 0;
var level:Array=new Array();
var scr:Number = 0;
var cn_arr:Array = new Array();
var score:Number = 0;
var rep:Replay=new Replay();
var player:Player=new Player();
addChild(player);
player.x=200;
player.y=250;
//scoreboard.txt.text = score.toString();
var camera = new Rectangle(0,0,stage.stageWidth,stage.stageHeight);
var scoreboard:ScoreBoard = new ScoreBoard();
addChild(scoreboard);
for (var i:int = 0; i< numChildren; i++)
{
if (getChildAt(i) is platform)
{
level.push(getChildAt(i).getRect(this));
//trace("ok");
}
}
for (i = 0; i<numChildren; i++)
{
if (getChildAt(i) is cn )
{
cn_arr.push(getChildAt(i));
}
}
function initGame(e:Event):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function keyPressed(e:KeyboardEvent):void
{
//trace("pressed");
if (e.keyCode == Keyboard.A)
{
kRt = true;
//trace("kRt");
}
if (e.keyCode == Keyboard.SPACE)
{
kUp = true;
//trace("Jump");
}
if (e.keyCode == Keyboard.D)
{
kLt = true;
//trace("kRt");
}
if (e.keyCode == Keyboard.S)
{
kDn = true;
//trace("kRt");
}
}
function keyReleased(e:KeyboardEvent):void
{
//trace("notpressed");
if (e.keyCode == Keyboard.A)
{
kRt = false;
//trace("UnkRt");
}
if (e.keyCode == Keyboard.SPACE)
{
kUp = false;
//trace("UnkYUp");
}
if (e.keyCode == Keyboard.D)
{
kLt = false;
//trace("UnkLT");
}
if (e.keyCode == Keyboard.S)
{
kDn = false;
//trace("UnkDN");
}
}
stage.addEventListener(Event.ENTER_FRAME,initGame);
function loop(e:Event):void
{
if (kRt)
{
speedX = -5;
}
else if (kLt)
{
speedX = 5;
}
else
{
speedX *= 0.5;
}
player.x += speedX;
for (i=0; i<level.length; i++)
{
if (player.getRect(this).intersects(level[i]))
{
if (speedX> 0)
{
player.x = level[i].left - player.width / 2;
}
if (speedX < 0)
{
player.x = level[i].right + player.width / 2;
}
speedX = 0;
}
}
var jumpable:Boolean = false;
speedY += 1;
player.y += speedY;
for (i=0; i<level.length; i++)
{
if (player.getRect(this).intersects(level[i]))
{
if (speedY > 0)
{
player.y = level[i].top - player.height / 2;
speedY = 0;
jumpable = true;
//player.gotoAndStop("jump");
}
if (speedY < 0)
{
player.y = level[i].bottom + player.height / 2;
speedY *= -0.5;
}
}
}
for (var j:int=0; j<cn_arr.length; j++)
{
if ((cn_arr[j]).hitTestObject(player))
{
removeChild(cn_arr[j]);
//removeChild(cn_arr[j]);
score += 2;
//var a:String=scr.toString();
scoreboard.txt.text = score.toString();
//score.x=50;
}
}
if (kUp && jumpable)
{
speedY = -15;
}
if(player.hitTestObject(ground))
{
removeChild(player);
addChild(rep);
rep.x=stage.stageWidth/2;
rep.y=stage.stageHeight/2;
rep.btn_re.addEventListener(MouseEvent.CLICK,oncl);
function oncl(e:MouseEvent):void
{
removeChild(rep);
addChild(player);
player.x=166;
player.y=205;
}
}
scoreboard.x = camera.x + 280;
scoreboard.y = camera.y + 370;
/*trace(scoreboard.y + " score");
trace(camera.y);*/
camera.x = (player.x - camera.width+150);
camera.y = (player.y - camera.height+250);
if (camera.x >= 2000)
{
camera.x = 2000;
}
if (camera.x <= 0)
{
camera.x = 0;
}
if (camera.y >= 0)
{
camera.y = 0;
}
if (camera.y <= -610)
{
camera.y = -610;
}
scrollRect = camera;
}
enter code here
答案 0 :(得分:0)
此错误表示您尝试删除的子项不在显示列表中。因此,无论您在哪里使用(例如)displayObject.removeChild(child);
,都应首先使用if (displayObject.contains(child)) { // do removal }
进行检查。这将解决您的错误。