问题描述:

I'm trying to make real simple game for a school project and I'm new to ActionScript 3. I just learned how to call objects as children to the scene by code but I'm having problem removing them. Removing actually works but gives errors like

"ArgumentError: Error #2025:

at flash.display::DisplayObjectContainer/removeChild()

at breathe_bw_fla::MainTimeline/bulletSpeeder()"

I created two bullet objects, one is facing left and the other one right. They show up where the gun is on the scene. They go left or right and I want to remove them after they leave the scene and they will be called again when character shoots for the second time.

function shoot(event:KeyboardEvent) {

if(event.keyCode == 32 && shootCooldown > 48 && rightOrLeftDown == true && ammo > 0) {

ammo -= 1;

shootCooldown = 0;

if(rightDown == true){

addChild(bulletGoingRight);

bulletGoingRight.x = character.x+character.width/2;

bulletGoingRight.y = character.y;

stage.addEventListener(Event.ENTER_FRAME, bulletSpeeder);

}

if(leftDown == true){

addChild(bulletGoingLeft);

bulletGoingLeft.x = character.x-character.width/2;

bulletGoingLeft.y = character.y;

stage.addEventListener(Event.ENTER_FRAME, bulletSpeeder);

}

}

}

function bulletSpeeder(Event){

if(bulletGoingLeft.x > -10){

bulletGoingLeft.x -= 10;

}

if(bulletGoingLeft.x <= -10){

removeChild(bulletGoingLeft);

}

if(bulletGoingRight.x < 760){

bulletGoingRight.x += 10;

}

if(bulletGoingRight.x >= 760){

removeChild(bulletGoingRight);

}

}

网友答案:

More info for those looking for the answer. Error #2025 is

The supplied DisplayObject must be a child of the caller.

from http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/runtimeErrors.html

Basically you're trying to remove an object that either doesn't have a parent, or is a child of another object.

A simple way to have objects self remove is:

if( this.parent != null )
    this.parent.removeChild( this );

or

if( obj.parent != null )
    obj.parent.removeChild( obj );

if you're not using classes

相关阅读:
Top