Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
you are splicing _bulletsArray, but are still looping through it. maybe use a while?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
do this:
trace("bcount: " + bcount);
trace("_bulletsArray length: " + _bulletsArray.length);
you'll see
Copy link to clipboard
Copied
Well that is strange.
when i dont shoot it gives me
_bulletsArray length: 0
bcount: 0
_bulletsArray length: 0
bcount: 0
and when i shoot
bcount: 0
_bulletsArray length: 1
bcount: 0
_bulletsArray length: 1
... bcount doesnt change ,so does the problem come from me checking if shipArray is hitting a _bulletsArray[WITH always a 0 property/number/element].But why when i give it to be
for (var bcount:int = _bulletsArray.length-1; bcount >= 0; bcount--) {
or im totally missing the obvious.
Copy link to clipboard
Copied
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);
}
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now