Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:53:17+00:00 2026-06-13T04:53:17+00:00

I’m trying to make a spaceship game and I’m having a problem when I’m

  • 0

I’m trying to make a spaceship game and I’m having a problem when I’m using the removeChild() method to remove the projectiles or the enemy ships out of the stage.

Here’s the relevant part of the code:

var bulletsArr:Array = new Array();
var eShipsArr:Array = new Array();


for (var c = 0; c < eShipsArr.length; c++){
stage.addChild(eShipsArr[c]);
}
....

stage.addEventListener(KeyboardEvent.KEY_DOWN,shoot);

function shoot (e:KeyboardEvent):void{
if(e.keyCode==17){
    var pj:projectile = new projectile();
    pj.x = SShip.x;
    pj.y = SShip.y;
    stage.addChild(pj);
    bulletsArr.push(pj);
    if(bulletsArr.length >= 10){
       bulletsArr = bulletsArr.slice(1,10);
    }
}
}
stage.addEventListener(Event.ENTER_FRAME,checkHit);

function checkHit (e:Event):void{
for (var d = 0; d < bulletsArr.length; d++){
    for(var f = 0; f < eShipsArr.length; f++){
        if(bulletsArr[d].hitTestObject(eShipsArr[f])){
            trace(bulletsArr[d].parent);
            stage.removeChild(bulletsArr[d]); //ArgumentError: Error #2025!!!!
        }
    }
}
}

When I use removeChild() with stage, the trace returns null. Without it the trace returns Stage object. In both cases the same error jumps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:53:20+00:00Added an answer on June 13, 2026 at 4:53 am

    Could it be that you are trying to remove a child that was already removed?

    You are not removing it from the array, so the next loop or the next checkHit call will include checks on objects that were possibly already removed from stage. A hitTestObject check would return true even with objects that are not on the stage.

    Untested (note that the bulletes array is being iterated backwards to avoid problems with the length when removing array elements):

    for (var d = bulletsArr.length - 1; d >= 0; d--){
        for(var f = 0; f < eShipsArr.length; f++){
            if(bulletsArr[d].hitTestObject(eShipsArr[f])){
                trace(bulletsArr[d].parent);
                stage.removeChild(bulletsArr[d]); 
                bulletsArr.splice(d, 1); // remove bullet from array
            }
        }
    }
    

    Edit: Here’s some untested (properly typed) code in case it is possible that a single bullet can collide with two or more ships, and all you need to do is remove the bullets:

    for(var f:Number = 0; f < eShipsArr.length; f ++)
    {
        for(var d:Number = bulletsArr.length - 1; d >= 0; d --)
        {
            if(bulletsArr[d].hitTestObject(eShipsArr[f]))
            {
                stage.removeChild(bulletsArr[d]);
                bulletsArr.splice(d, 1);
            }
        }
    }
    

    The loops where interchanged, that way removed bullets are not checked again.

    In case it is possible that a single bullet can collide with two or more ships, and you are planning to do something with ships that collided with the bullet too, then try this:

        for(var d:Number = bulletsArr.length - 1; d >= 0; d --)
        {
            var bullet:DisplayObject = bulletsArr[d];
            var bulletHitSomething:Boolean = false;
    
            for(var f:Number = 0; f < eShipsArr.length; f ++)
            {
                if(bullet.hitTestObject(eShipsArr[f]))
                {
                    if(!bulletHitSomething)
                    {
                        bulletHitSomething = true;
                        stage.removeChild(bullet);
                        bulletsArr.splice(d, 1);
                    }
                    // do other, ship specific stuff here
                }
            }
        }
    

    The current bullet object reference is stored in a variable, and all the checks are done against that variable instead of the element in the array using array access. Unlike the array access method where it coule possibly access a non existent index, this way possible consecutive checks on the same bullet should be fine. Also a variable stores whether the bullet already hit something, so that it doesn’t try to remove that bullet again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.