Hi,
I am pretty new to AS3 and am trying to build a game based on Pong. I am having trouble however. When a ‘goal’ is scored, i am given the following error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display:isplayObjectContainer/removeChild()
at Pong/ballPosition()
package {
import flash.display.Sprite;
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class Pong extends MovieClip {
private var paddle:Paddle;
private var paddle2:Paddle;
private var ball:Ball;
private var topLine:Line;
private var bottomLine:Line;
public var upArrow, downArrow:Boolean;
public var upArrow2, downArrow2:Boolean;
public var spacebarDown:Boolean;
public var ballOutOfPlay:Boolean;
public var ballDX:Number;
public var ballDY:Number;
public var playerOneScore:Number;
public function Pong() {
//set speed of ball
ballDX = 10;
ballDY = 5;
//set score
playerOneScore = 0;
//set up stage
showPlayerOneScore.text = ""+playerOneScore;
showPlayerTwoScore.text = "0";
topLine = new Line();
addChild(topLine);
topLine.x = stage.stageWidth/2;
topLine.y = 43;
bottomLine = new Line();
addChild(bottomLine);
bottomLine.x = stage.stageWidth/2;
bottomLine.y = 393;
paddle = new Paddle();
addChild(paddle);
paddle.x = 25;
paddle.y = 80;
paddle2 = new Paddle();
addChild(paddle2);
paddle2.x = 523;
paddle2.y = 80;
paddle2.width = 30;
ball = new Ball();
ball.x = 125;
ball.y = 80;
addChild(ball);
//key presses and stage boundary
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
stage.addEventListener(Event.ENTER_FRAME, stageBoundary);
stage.addEventListener(Event.ENTER_FRAME, stageBoundary2);
//animations
paddle.addEventListener(Event.ENTER_FRAME, movePaddle);
paddle2.addEventListener(Event.ENTER_FRAME, movePaddle2);
ball.addEventListener(Event.ENTER_FRAME, moveBall);
//check for paddlecollision
ball.addEventListener(Event.ENTER_FRAME, hitBall);
// check for goal
ball.addEventListener(Event.ENTER_FRAME, ballPosition);
if (ballOutOfPlay) {
removeChild(ball);
}
}
// key pressed
public function keyDownFunction(event:KeyboardEvent):void {
if (event.keyCode == 65) {
upArrow = true;
}
else if (event.keyCode == 90) {
downArrow = true;
}
else if (event.keyCode == 38) {
upArrow2 = true;
}
else if (event.keyCode == 40) {
downArrow2 = true;
}
else if (event.keyCode == 32) {
spacebarDown = true;
}
}
// key released
public function keyUpFunction(event:KeyboardEvent):void {
if (event.keyCode == 65) {
upArrow = false;
}
else if (event.keyCode == 90) {
downArrow = false;
}
else if (event.keyCode == 38) {
upArrow2 = false;
}
else if (event.keyCode == 40) {
downArrow2 = false;
}
}
//move ball
public function moveBall(event:Event):void {
if(spacebarDown) {
ball.x -= ballDX;
ball.y += ballDY;
}
}
// stage boundary
public function stageBoundary(event:Event):void {
if (paddle.y <= 80) {
upArrow = false;
}
else if (paddle.y >= 351) {
downArrow = false;
}
}
public function stageBoundary2(event:Event):void {
if (paddle2.y <= 80) {
upArrow2 = false;
}
else if (paddle2.y >= 351) {
downArrow2 = false;
}
}
//check for goal
public function ballPosition(event:Event) {
if (ball.x < 0) {
ballOutOfPlay = true;
trace("goal");
playerOneScore++;
showPlayerOneScore.text = ""+playerOneScore;
removeChild(ball);
}
}
//move paddles
public function movePaddle(event:Event) {
if(upArrow) {
paddle.y -=10;
}
else if(downArrow) {
paddle.y +=10;
}
}
public function movePaddle2(event:Event) {
if(upArrow2) {
paddle2.y -=10;
}
else if(downArrow2) {
paddle2.y +=10;
}
}
//check for bouncing ball
public function hitBall(event:Event) {
if(ball.hitTestObject(paddle)) {
//trace("hit");
ballDX*=-1;
}
else if(ball.hitTestObject(paddle2)) {
//trace("hit2s");
ballDX*=-1;
}
else if(ball.hitTestObject(topLine)) {
//trace("hitTop");
ballDY*=-1;
}
else if(ball.hitTestObject(bottomLine)) {
//trace("hitBottom");
ballDY*=-1;
}
}
}
}
Any help is greatly appreciated
Thanks
-John