[F8] Locating closet enemy...
alright so I just started back up with this space invader style game and my "heat-seeking" missile isn't working out exactly how it should. I think the problem is that it calls a function to locate the closest enemy which isn't actually finding the closest enemy. I am not going to put the actual code cause it will be confusing but the basic code is as follows:
Code:
function LocClos(xcoord, ycoord, bult){ //xcoord ycoord and name of the missile
closest=9000;
var closestName:String;
if (getHighestEnemyName()<0){
return;
}
highest = getHighestEnemyName()
for (i=0;i<=highest;i++){
if (xcoord>this["enemy"+i]._x){
tempx = xcoord-this["enemy"+i]._x;
} else if (this["enemy"+i]._x>xcoord){
tempx = this["enemy"+i]._x-xcoord;
}
if (ycoord>this["enemy"+i]._y){
tempy = ycoord-this["enemy"+i]._y;
} else if (this["enemy"+i]._y>ycoord){
tempy = this["enemy"+i]._y-ycoord+1000;//+1000 because the missile cannot go backwards
}
tempDistance=tempy + tempx;
if(tempDistance<closest){
closest=tempDistance;
this[bult].closestName="enemy"+i;
}
return(this[bult].closestName);
}
}
//the following is in the bullet movement code:
this[me]._y-=this[me].speed;
//check for different bullet movement types
if(this[me].movType == 1){
LocClos(this[me]._x, this[me]._y, this[me]._name);
if (this[me]._x>this[LocClos(this[me]._x, this[me]._y, this[me]._name)]._x){
this[me]._x-=this[me].speedx;
}else if(this[LocClos(this[me]._x, this[me]._y, this[me]._name)]._x>this[me]._x){
this[me]._x+=this[me].speedx;
}
}
ugh this code looks so messy... anyways hopefully this is understandable? If anything needs clarification, let me know... Thanks in advance,
ChaseNYC