Skip to main content
Known Participant
August 19, 2013
Answered

Error #1010: A term is undefined and has no properties. when it is not even on the stage

  • August 19, 2013
  • 2 replies
  • 968 views

This is an error that i cant even explain to myself why does it trigger, because it doesnt trigger every time - only when i shoot like the 1st or last ship of the Array and it somehow changes and it doesnt splice itself or something like that.

So i have a doShips Function:

function doShips() {

          for (var i:int = shipArray.length - 1; i >= 0; i--) {

                    shipArray.moveDown() //what the code in the Ship and Ship2 class does -> only: this.y += 3

                    for (var bcount= _bulletsArray.length-1; bcount >= 0; bcount--) {

                              //if the bullet is touching the ship

                                        if (shipArray.hitTestObject(_bulletsArray[bcount])) {

                                        //if we get here it means there`s is a collision

                                                  removeChild(_bulletsArray[bcount]);

                                        _bulletsArray.splice(bcount,1);

                                        removeChild(shipArray);

                                        shipArray.splice(i,1);

                              }

                    }

          }

}

When i debug the FLA the error comes from

at ArrayOfShips_newG_withTurret_fla::MainTimeline/doShips()[ArrayOfShips_newG_withTurret_fla.MainTimeline::frame1:100]

line 100 is

if (shipArray.hitTestObject(_bulletsArray[bcount])) {

before that there is a Player on the stage that shoots bullets

//handling the bullets

function doBullets(){

          //make a for loop to iterate all  the _bulletsArray on the screen

          for (var bcount:int = _bulletsArray.length-1; bcount>=0; bcount--) {

                    //make the bullets move Up

                    _bulletsArray[bcount].y -=20;

                    //if the bullet is beyond the screen remove from the stage and the Array

                    if(_bulletsArray[bcount].y < 0) {

                              removeChild(_bulletsArray[bcount])

                              _bulletsArray.splice(bcount,1);

                    }

          }

}

also a function to move the player in the X coordinate.

and also a function that positions the ships,but they are not important because i dont think they have anything to do with the error.

I can show all the code of the FLA if that is going to help somehow to solve this.

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Try to use index loops as little as possible. Use for...each:

a. they are faster

b. they do not depend/break on removing elements from arrays in the middle of iterations.

Try this code:

function doShips():void

{

          for each(var ship:MovieClip in shipArray)

          {

                    Object(ship).moveDown() ;

                    for each(var bullet:MovieClip in _bulletsArray)

                    {

                              //if the bullet is touching the ship

                              if (ship.hitTestObject(bullet))

                              {

                                        //if we get here it means there's is a collision

                                        removeChild(bullet);

                                        _bulletsArray.splice(_bulletsArray.indexOf(bullet), 1);

                                        removeChild(ship);

                                        shipArray.splice(shipArray.indexOf(ship), 1);

                              }

                    }

          }

}

2 replies

Andrei1-bKoviICorrect answer
Inspiring
August 19, 2013

Try to use index loops as little as possible. Use for...each:

a. they are faster

b. they do not depend/break on removing elements from arrays in the middle of iterations.

Try this code:

function doShips():void

{

          for each(var ship:MovieClip in shipArray)

          {

                    Object(ship).moveDown() ;

                    for each(var bullet:MovieClip in _bulletsArray)

                    {

                              //if the bullet is touching the ship

                              if (ship.hitTestObject(bullet))

                              {

                                        //if we get here it means there's is a collision

                                        removeChild(bullet);

                                        _bulletsArray.splice(_bulletsArray.indexOf(bullet), 1);

                                        removeChild(ship);

                                        shipArray.splice(shipArray.indexOf(ship), 1);

                              }

                    }

          }

}

Participating Frequently
August 19, 2013

you are splicing _bulletsArray, but are still looping through it.  maybe use a while?

Known Participant
August 19, 2013

well if i do it with

while (shipArray.hitTestObject(_bulletsArray[bcount])) {

now it gives me

TypeError: Error #2007: Parameter hitTestObject must be non-null.

at the same line

Participating Frequently
August 19, 2013

do this: 

trace("bcount: " + bcount);

trace("_bulletsArray length: " + _bulletsArray.length);  

you'll see