user1685872
user1685872

Reputation: 11

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. AS3

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:

    }

}

Upvotes: 1

Views: 3775

Answers (3)

Vesper
Vesper

Reputation: 18747

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?

Upvotes: 0

Neil
Neil

Reputation: 8111

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

Upvotes: 1

Roman Trofimov
Roman Trofimov

Reputation: 446

Replace yours

removeChild(SomeSprite); 

with

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

An objects parent can only call removeChild.

Upvotes: 1

Related Questions