提供的DisplayObject必须是调用者#2025的子级

时间:2014-03-04 19:01:28

标签: actionscript-3 object movieclip removechild addchild

我一直收到这个错误:

  

ArgumentError:错误#2025:提供的DisplayObject必须是子级   来电者。在flash.display :: DisplayObjectContainer / removeChild()   在Bell / eFrame()[C:\ Users \ xx \ Desktop \ xx \ Bell.as:44]

我的代码:

package{
import flash.display.MovieClip;
import flash.events.*;
public class Bell extends MovieClip {

var _root:Object;//this will symbolize the main timeline

public function Bell() {
    addEventListener(Event.ADDED, beginClass);
    addEventListener(Event.ENTER_FRAME, eFrame);
}

private function beginClass(e:Event):void{
    _root = MovieClip(root);

    if(_root.bellTotal == 1){//if it's the first bell created
        this.x = Math.random()*525;//place it in a random spot on the stage
        _root.bellLastCoord = this.x;
    } else {//otherwise, 
        //In order to keep the next bell from being too far away from the previous bell, place it up to 250px away
        this.x = _root.bellLastCoord + (Math.random()*500)-250;
        if(this.x > 537.5){//if it is off the stage to the right
            this.x -= 250;//set it inside the stage
        } else if (this.x < 12.5){//same with too far left
            this.x += 250;
        }
    }
    this.y = _root.bellTop;//set the y's value off the stage
}
private function eFrame(e:Event):void{
    this.y += 3;//move the bell slowly downwards
    if(this.hitTestObject(_root.mcMain)){//if this touches the main character
        _root.mainJumping = true;//make him jump
        _root.jumpSpeed = _root.jumpSpeedLimit*-1;//reset the jumpSpeed
        _root.scoreInc += 10;//increase the amount that the score will increase
        _root.score += _root.scoreInc;//add this to the score
        var scoreText:ScoreAdd = new ScoreAdd();
        _root.bellHolder.addChild(scoreText);//add some text to the stage
        scoreText.x = this.x;//set the coordinates for the text
        scoreText.y = this.y;
        scoreText.txtScore.text = _root.scoreInc;//set the text to the amount the score increased by

        this.removeEventListener(Event.ENTER_FRAME, eFrame);//remove the listeners
        _root.bellHolder.removeChild(this);//and finally remove him from the stage

        _root.startedJumping = true;
    }

    if(_root.gameOver){//if the game is over
        this.removeEventListener(Event.ENTER_FRAME, eFrame);//remove the listeners
        _root.bellHolder.removeChild(this);//and remove from stage
    }
}
}

但是,我已经确定我用bellHolder添加了一个新铃:

var newBell:Bell = new Bell();//create a new bell instance
    bellHolder.addChild(newBell);

我是菜鸟,所以我需要知道要使用的确切代码。谢谢!

1 个答案:

答案 0 :(得分:0)

首先,从你的代码中,removeChild可以从hotTest条件和游戏条件触发两次。第二,你可以用更通用的方式从显示列表中删除bell:

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