Skip to main content
Known Participant
January 21, 2011
Question

Shooting Game - losing life problem

  • January 21, 2011
  • 1 reply
  • 374 views

Hi, I am trying to create as simple game in which a ship shoots asteroids - which seems to work although I do get errors :

Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()

I am also detecting a ship - asteroid collision which when it happesns the first time makes the ship glow red gotoAndPlay(6);

but after 4 collisions i want the ship to disappear gotoAndPlay(87) but this part doesn't work. Thanks in advance

//import some important flash libraries.
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;

var countCollisions:Number =0 ;
var astArray:Array = new Array();
var speed:Number = 10;
//initializes variables.
var key_left:Boolean = false;
var key_right:Boolean = false;
var key_up:Boolean = false;
var key_down:Boolean = false;
var key_space:Boolean = false;
var shootLimiter:Number=0;
//Checks if the player presses a key.

var timer:Timer = new Timer(2000, 10);
timer.addEventListener(TimerEvent.TIMER, AddAsteroid);
timer.start();

stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);

var ship:MovieClip = new Ship()
var backgr:MovieClip = new Background();

backgr.x = 0;
backgr.y = 45;
addChild(backgr);
addChild(ship);

function AddAsteroid(e:TimerEvent):void
{

  
var asteroid:MovieClip = new Asteroid();   
asteroid.x = 800;
asteroid.y = Math.round(Math.random()*700);
astArray.push(asteroid);  
addChild(asteroid);
asteroid.addEventListener(Event.ENTER_FRAME,asteroidmove);   

function asteroidmove(e:Event):void
{
asteroid.x--


for(var i=0; i<astArray.length; i++)
{
 

  if(astArray.hitTestObject(ship))
  {
  countCollisions++;
  ship.gotoAndPlay(6);
   if(countCollisions ==4)
   {
   ship.gotoAndPlay(87);
   }
  removeChild(astArray);
 
  removeEventListener(Event.ENTER_FRAME,asteroidmove);
    }
   }


}

}

addEventListener(Event.ENTER_FRAME,backgroundmove);

function backgroundmove(e:Event):void
{
backgr.x -= 1;
  
if(backgr.x < -2110)
{
backgr.x = 0;
}
}

//Lets the function main play every frame.
addEventListener(Event.ENTER_FRAME,Main);

//create the function main.
function Main(event:Event){
CheckKeys();
}

//create the function KeyDown.
function KeyDown(event:KeyboardEvent){
if(event.keyCode == 37){  //checks if left arrowkey is pressed.
  key_left = true;
}
if(event.keyCode == 39){  //checks if right arrowkey is pressed.
  key_right = true;
}
if(event.keyCode == 38){  //checks if up arrowkey is pressed.
  key_up = true;
}
if(event.keyCode == 40){  //checks if down arrowkey is pressed.
  key_down = true;
}
if(event.keyCode == 32){  //checks if down arrowkey is pressed.
  key_space = true;
 
}
}

function KeyUp(event:KeyboardEvent){
if(event.keyCode == 37){  //checks if left arrowkey is released.
  key_left = false;
}
if(event.keyCode == 39){  //checks if right arrowkey is released.
  key_right = false;
}
if(event.keyCode == 38){  //checks if up arrowkey is released.
  key_up = false;
}
if(event.keyCode == 40){  //checks if down arrowkey is released.
  key_down = false;
}
if(event.keyCode == 32){  //checks if down arrowkey is released.
  key_space = false;
}
}

function CheckKeys()
{
shootLimiter += 1;

if(key_left)
{
  setDirection(1);
  ship.x -= 5;
}
if(key_right)
{
  setDirection(0);
  ship.x += 5;
 
}
if(key_up){
  ship.y -= 5;
}
if(key_down){
  ship.y += 5;
}

if((key_space) && (shootLimiter > 8))
{
shootLimiter = 0;
var b = new Bullet();
addChild(b);
b.x = ship.x + 50;
b.y = ship.y + 3;
addEventListener(Event.ENTER_FRAME,moveBullet);
}

function moveBullet(e:Event):void
{
b.x +=10;
 
  if(b.x > 600)
     {
   if(contains(b))
   {
   removeChild(b);
   }
          removeEventListener(Event.ENTER_FRAME,moveBullet);

     }
for(var i=0; i<astArray.length; i++)
{
  if(astArray.hitTestObject(b))
  {
  removeChild(b);
  removeChild(astArray);
 
  removeEventListener(Event.ENTER_FRAME,moveBullet);
    }
   }
}
}
function setDirection(param) {
if (param == 0) {
 
  ship.scaleX = 1;
} else {
  ship.scaleX = -1;
}

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
January 21, 2011

you're probably trying to remove something that's already been removed.  use the following to eliminate that problem:

if(astArray.stage){

  removeChild(astArray);

}

also, you should be removing that array element and ready it for gc if you no longer use it

NarcudAuthor
Known Participant
January 21, 2011

Thanks for your help. This has now got rid of the error, still can't work

out why the countCollisions is not working. Could it be that while the collision goes to play frames inside the ship movieclip to start it glowing,

it isn't able to go to frame 87 where i have alpha tweened the ship to make it disappear ?

for(var i=0; i<astArray.length; i++)
{
 

  if(astArray.hitTestObject(ship))
  {
  countCollisions++;
  ship.gotoAndPlay(6);
 
   if(countCollisions ==4)
   {
   ship.gotoAndPlay(87);
   }
  if(astArray.stage)
  {
        removeChild(astArray);
        removeEventListener(Event.ENTER_FRAME,asteroidmove);
    }