1
votes

i'm trying to make a space invaders type game with bullets shooting and ships crashing into one another using the hitTestObject function but i can't get the removeChild(); function to work without the Error above. Here's the Code what should i do.

import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;

var count:int = 1;

//adding the components
var NewBullet:MovieClip = new Bullet;

    var Ship_M:MovieClip = new Ship; 
    Ship_M.x = 270;
    Ship_M.y = 470;
    addChild(Ship_M);


    var Ship_E:MovieClip = new E_Ship;
    Ship_E.x = 270;
    Ship_E.y = 5;
    addChild(Ship_E);


stage.addEventListener(Event.ENTER_FRAME , Rec);
function Rec(e:Event):void{
  if (NewBullet.hitTestObject(Ship_E))
  {
    removeChild(Ship_E);
    removeChild(NewBullet);

  }

   if (Ship_E.hitTestObject(Ship_M))
   {
      removeChild(Ship_E);
      removeChild(Ship_M);
    }



    }

function Moves(e:Event):void{

            NewBullet.y -= 30;

    if (NewBullet.y < 0 )
        {

         removeChild(NewBullet);

          count++;
    removeEventListener(Event.ENTER_FRAME, Moves);

        }

          trace (count);
    }



//For Moving the Spaceship          
stage.addEventListener(KeyboardEvent.KEY_DOWN, Move);

function Move (event:KeyboardEvent):void{

 switch(event.keyCode)

    {
        case 37: 
            if (Ship_M.hitTestObject(Stop_L1))
            {
                Ship_M.x -=  0;
            }
            else
            {
                Ship_M.x -=  10;
            }
        break;

        case 38:
            if (Ship_M.hitTestObject(Stop_U1))
            {
                Ship_M.x -=  0;
            }
            else
            {
                Ship_M.y -=  10;
            }

        break;

        case 39:
            if (Ship_M.hitTestObject(Stop_R1))
            {
                Ship_M.x -=  0;
            }
            else
            {
                Ship_M.x +=  10;
            }

        break;

        case 40:
            if (Ship_M.hitTestObject(Stop_D1))
            {
                Ship_M.x -=  0;
            }
            else
            {
                Ship_M.y +=  10;
            }

        break;


        case 32:
        addChild(NewBullet);
        NewBullet.x = Ship_M.x;
        NewBullet.y = Ship_M.y;

        addEventListener(Event.ENTER_FRAME, Moves);
        break;

        default:

    }

}
3
Ideally you should name your variables with lowerCamelCase to avoid confusion. Class names typically begin with a capital letter. - crooksy88
Please consider using raster (bitmapData) and copyPixels to 'render' your game for greatly improved performance. - AturSams

3 Answers

1
votes

Replace yours

removeChild(SomeSprite); 

with

if (SomeSprite.parent)
    SomeSprite.parent.removeChild(SomeSprite); 

An objects parent can only call removeChild.

1
votes

You may not need this in your particular circumstance, but I have a helper function to remove a child and make it eligible for GC which goes like so:

private function remove(child:DisplayObject):void
{
    if(child && child.parent)
    {
        child.parent.removeChild(child);
        child = null;
    }
}
0
votes

Most likely you do removeChild(NewBullet) twice, first when it hits Ship_E and second when it leaves boundary. A simple solution will be whenever your NewBullet is removed from stage, set it a flag, say "enabled" which would mean the bullet can trigger events, and check it throughout your code whenever you have to check for the bullet.

Note, you have only one bullet, is it normal?