Jayden.Loone
Jayden.Loone

Reputation: 56

Type Error #1010 (an object is undefined and had no properties) when removing child from an array

I get a type error in this function of an undefined object when i comment out the if statements in the function the error goes away.

spartans is an array

bullets is an array

function loop(event:Event)
{
    for (var bcount=0; bcount < bullets.length; bcount++)
    {
        if (bullets[bcount].x <= 1055)
        {
            bullets[bcount].x = bullets[bcount].x + bulletSpeed;
        }
        else
        {
            removeChild(bullets[bcount])
            bullets.splice(bcount, 1)
            if (bullets.length != 1)
            {
                bcount--;
            }
            
        }
        
    }
        for (var spcount=0; spcount<spartans.length; spcount++)
        {
            spartans[spcount].x = spartans[spcount].x - spartanSpeed;
            if (bullets.length != 0)
            {
                if (bullets[bcount].hitTestObject(spartans[spcount]))
                {
                    removeChild(spartans[spcount])
                    spartans.splice(spcount, 1)
                    removeChild(bullets[bcount])
                    bullets.splice(bcount, 1)
                }
            }
        }


}

Upvotes: 0

Views: 108

Answers (2)

Joe Taras
Joe Taras

Reputation: 15379

Normally the for variable step must be updated only by for instruction.

You decrement bCount in the if statement. So, I prefer, in your case, use for each statement and manage bCount variable in the loop body.

Pay attention about the order to remove your rows in the for.

Finally, bCount variable miss the type, put var bCount:int, the same thing about spcount variable.

EDIT AFTER COMMENT Variable definition:

var bCount:int = 0 instead var bCount = 0
var spcount:int = 0 nstead var spcount = 0

Loop (inverted loop):

for (var bcount:int=bullets.length; bcount >=0; bcount--)
{
    if (bullets[bcount].x <= 1055)
    {
        bullets[bcount].x = bullets[bcount].x + bulletSpeed;
    }
    else
    {
        removeChild(bullets[bcount]);
        // bullets.splice(bcount, 1); - REMOVE IT
        //if (bullets.length != 1)
        //{
        //    bcount--; // DECREMENT FOR VARIABLE, IMHO, IS DEPRECATED
        //}

    }

}

splice function peraphs is not good for you. See here

Upvotes: 0

Vesper
Vesper

Reputation: 18747

You've messed up bullets loop, your loop only moves bullets and splices them, and then you have a completely separate loop that checks vs bullets[bcount], which is already out of its own loop, thus bcount == bullets.length so you're querying outside an array, this causes the 1010 error. Do put all the code for looping spartans inside the bullets loop, beside the "move bullet" statement.

function loop(event:Event)
{
    for (var spcount=0;spcount<spartans.length; spcount++)
    {
           spartans[spcount].x = spartans[spcount].x - spartanSpeed;
    }
    // first move spartans, they too need to move once 
    // then move bullets, and check vs moved spartans
    for (var bcount=0; bcount < bullets.length; bcount++)
    {
        if (bullets[bcount].x <= 1055)
        {
            bullets[bcount].x = bullets[bcount].x + bulletSpeed;
            // and now, after you moved the bullet, loop spartans vs this bullet
            for (spcount=0; spcount<spartans.length; spcount++)
            {
                if (bullets[bcount].hitTestObject(spartans[spcount]))
                {
                    removeChild(spartans[spcount])
                    spartans.splice(spcount, 1)
                    removeChild(bullets[bcount])
                    bullets.splice(bcount, 1)
                    break; // there's no bullet anymore, stop looping spartans
                }
            }
        }
        else
        {
            removeChild(bullets[bcount])
            bullets.splice(bcount, 1)
            bcount--; // decrease anyway, or really loop backwards
        }
    }
}

Also another issue was fixed - you need to move spartans and bullets, and you need a DOUBLE loop to check bullets vs spartans, so while one can move a bullet and then check for all the sparans, one has to move all the spartans once too. So moving spartans was placed into a separate loop.

Upvotes: 1

Related Questions