Reputation: 315
I keep getting this error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Bell/eFrame()[C:\Users\xx\Desktop\xx\Bell.as:44]
My code:
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
}
}
}
}
Upvotes: 0
Views: 509
Reputation: 3728
Assuming line 44 is _root.bellHolder.removeChild(this);
, this means that this
(an instance of Bell
) is not a child of _root.bellHolder
.
When you add a new Bell
to the display list, you need to make sure that you're adding it to bellHolder
.
Upvotes: 1