1 - In your double for loop you will be testing each actor against itself. Also each time you are setting distance back to 1000
2- You are finding the closest enemy but it looks like it's not being used.
3 looking at your code it appears to shoot for every enemy regardless of distance. Your code says
Loop 1 For each enemy create these variables and give the integers a value of 1000,
Loop 2 For each enemy search for which is the closest and store its clone number in point Then end the loop 2
Stop moving
Shoot
Then end loop1
Now it looks like you want to search all the enemys but only shoot at the closest as long as the closest is less than 1000! So maybe try this
- Code: Select all
int n;
int enemies = ActorCount("enemy");
Actor *closest; //This actor will be the closest clone
Actor *check; //This actor will be the current clone that you are checking
int dist = 1000;
for(n=0; n<enemies;n++) //For each enemy
{
check = getclone2("enemy",n) //See below for info on get clone2.
if(distance(x,y,check->x,check->y)<dist) //This will check to see if the enemy is in shooting range until an enemy is less than this range
{ //Then it will check to see if the enemy being checked is closer than the current closest
dist=distance(x,y,check->x,check->y); //distance will go to the closest enemy below 1000
closest = check; // this closest actor is not the one being checked. Not using this for anything but thought you might want it for future reference
}
}
if (dist < 1000) //Only need to shoot if the closest actors distance was less than 1000
{
xvelocity=0; // stop moving
CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false); //shoot forward. If you want to shoot towards the enemy then change this to
//CreateActor("projectile", "icon", "(none)", "(none)",closest->x,closest->y, false);
}
You will also need to add the getclone2 to game-editor. II suggest you make a global script for this and then add the folloiwing code
I did a search for the user who made this but can't find it so can someone point out who credit should go to.
- Code: Select all
Actor *getclone2 ( char *actorName, long cloneNumber )
{
char resultString[256] = "";
char cloneNumberAsString[10];
Actor *resultActor;
strcpy ( resultString, actorName );
strncat ( resultString, ".", 1 );
sprintf (cloneNumberAsString, "%i", cloneNumber);
strncat ( resultString, cloneNumberAsString, 10 );
resultActor = getclone(resultString);
return resultActor;
}
The only thing I can guarantee will work here is the getclone2 because that was written by someone who knows what they are doing where as I am just guessing.
It's good though because while helping you with this I'm learning and may use the same thing one day