2014-03-04 47 views
0

我不断收到此错误:ArgumentError:错误#2025:提供的DisplayObject必须是调用者的孩子。在flash.display使用::的DisplayObjectContainer/removeChild之()在贝尔/ eFrame()[C:\用户\ XX \桌面\ XX \ Bell.as:44]提供的DisplayObject必须是调用者的孩子#2025

我的代码:

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); 

我是小白,所以我需要知道确切的代码中使用。谢谢!

+0

一些事情可以尽量排除故障:1)确保你没有删除对象两次(在你已经显示的代码中有两个地方可以删除它,2)确保你试图删除你添加的同一个对象。这听起来像是你添加了多个铃铛,所以可以想象,你将某些铃铛混淆在某个地方,并且正在删除一个已经被删除/从未添加过的铃铛等。 –

+0

我该怎么做? – user3376601

回答

0

首先,从你的代码,removeChild之可以从则hitTest条件和GAMEOVER condition.Second触发两次,您可以在更通用的方法从显示列表中删除铃:

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

我在哪里放这个代码? – user3376601

+0

在您的eFrame处理程序中。交换你的实现'_root.bellHolder.removeChild(this);'与我的。 –

+0

您好,先生,太棒了! – user3376601

相关问题