0
votes

So im trying to make a bullet-ship hitTestObject ,but i don`t know why the variable representing the bullets.length doest change.

so the error comes from this function

function doShips() {
      trace("bcount :" + bcount)
      trace("_bulletsArray length:" + _bulletsArray.length)
      for (var i:int = shipArray.length - 1; i >= 0; i--) {
                shipArray[i].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

                                   while (shipArray[i].hitTestObject(_bulletsArray[bcount])) {
                                    //if we get here it means there`s is a collision

                                     removeChild(_bulletsArray[bcount]);
                                    _bulletsArray.splice(bcount,1);
                                    removeChild(shipArray[i]);
                                    shipArray.splice(i,1);
                          }
                }
      }

}

before that i have also a shoot function that shoots bullets and puts them in the _bulletsArray.

when the traces come it is showing : when i dont shoot bullets it gives me this

_bulletsArray length: 0
bcount: 0

and when i shoot it gives me this :

bcount: 0
_bulletsArray length: 1

or

 bcount: 0
_bulletsArray length: 2

so why doesnt the bcount change when The the _bulletsArray changes , when i am telling it to do so in the for (var bcount= _bulletsArray.length-1; bcount >= 0; bcount--) { even worse - when i datatype the 'bcount to a number 'bcount:Number' it gives me NaN

2

2 Answers

0
votes

Since you are iterating from length-1 down to 0, the value of bcount will be 0 when the loop exits so that is the value the trace will show.

The null parameter error is because you remove a colliding ship and bullet, and then on the next loop of the while statement you test the objects in the arrays at the same indices as the first time. If any of the objects happened to be at the end of the array, that position would now be null. To fix this replace the while-loop with an if statement.

In general you have to be careful when you change arrays while you are iterating over their elements, becuase the position of objects change, and you have to (like you did) iterate from the last element up to the first.

0
votes

Try tracing bcount inside the loop. You're tracing it before the Loop.

function doShips() {
  trace("bcount :" + bcount)
  trace("_bulletsArray length:" + _bulletsArray.length)
  for (var i:int = shipArray.length - 1; i >= 0; i--) {
            shipArray[i].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

                      while(shipArray[i].hitTestObject(_bulletsArray[bcount])) {
                                //if we get here it means there`s is a collision

                                 removeChild(_bulletsArray[bcount]);
                                _bulletsArray.splice(bcount,1);
                                removeChild(shipArray[i]);
                                shipArray.splice(i,1);
                                trace("bcount: " + bcount);
                      }
            }
  }}

Try it.