i have an array of tanks (0-44) which march down the screen and you shoot them. when there is a collision between a bullet and tank, it deletes that tank from the array. however, for some reason when tank44 is deleted, it ****s the whole array up and makes variables such as the _y = undefined. ive tried splicing tank44 and the same error occurs. can someone explain why this would happen to an array? this project is due at midnight and i just found this error
function createTanks() {
Tanks = new Array();
tankNum = 0;
// create three rows of 15
for(var y=0;y<3;y++) {
for(var x=0;x<15;x++) {
// create new tank and set position
attachMovie("tank", "tank"+tankNum, tankNum);
tankClip = this["tank"+tankNum];
tankClip._x = x*35+175;
tankClip._y = y*75-30;
// add to array
Tanks.push(tankClip);
tankNum++;
}
}
// set initial direction/speed
tankDirection = 0.5;
}
function moveTanks() {
// assume no change in direction
var newDirection = false;
// loop through all Tanks
for(var i=0;i<Tanks.length;i++) {
............
// see whether the last one hit bottom
trace(Tanks[i-1]._y);
if(Tanks[i-1]._y == undefined){
trace("ERROR!");
}
if (Tanks[i-1]._y > 425) {
removeTanks();
gotoAndStop("lose");
}
}
function checkCollision(bullet) {
// loop though all Tanks
for(j=Tanks.length-1;j>=0;j--) {
tank = Tanks[j];
// see whether the bullet is close to the tank
if (distance(bullet,tank) < 20) {
// remove from array
delete Tanks[j];
//}
// go to "pop" frame of tank
tank.gotoAndPlay(2);
// add to score
_root.score += 100;
//trace(tankLength);
// if no more invaders, then game over
if(tankLength < 1) {
gotoAndStop("win");
removeTanks();
}
// return true, because there was a collision
return(true);
}
}
// return false, because there was a collision
return(false);
}
function removeTanks(){
// remove tanks from screen & array
for(j=Tanks.length-1;j>=0;j--) {
delete Tanks[j];
tempTank="tank"+j;
eval(tempTank).removeMovieClip();
}
}
function distance(clip1, clip2) {
// find distance between two movie clips
dx = clip1._x - clip2._x;
dy = clip1._y - clip2._y;
return (Math.sqrt(dx*dx+dy*dy));
}