Page 1 of 2

distance question

PostPosted: Mon Jun 27, 2011 4:18 pm
by Nykos
Hi all, I've been trying to get it to work for all day long and searched on the forums for how to use distance. Please help me.

I have two actors, enemies which wil be randomly created by a timer, and robots which will be created by clicking a button.

What I want to do is ; when the robot is 50 (that may change) pixels from enemies, it starts shooting a it.
I tried various things like:
if(distance(x, y, enemies.x, enemies.y) <=50)
{
create timer, bla bla bla...
}

Well, it does not work, in fact the robot shoots all the time, it does wait to be near enemies. I'm almost 99% sure that it has something to do with correctly setting distance...or clones... OR MAYBE THE EVIL ZOMBIE DUCKS.

Could someone help me with this? please?

Re: distance question

PostPosted: Mon Jun 27, 2011 6:00 pm
by jimmynewguy
You have this in draw actor, so every frame the enemy is 50 pixels away a new timer is created causing 1029345124828901 timers to be made. Have the timer created with the actor then on the timer event check the distance and if it's less than 50 make a bullet otherwise nothing happens

Re: distance question

PostPosted: Mon Jun 27, 2011 6:57 pm
by Game A Gogo
at the end add:
Code: Select all
else DestroyTimer("Event Actor","timer");

(syntax may be wrong)
but this will delete the timer whenever it's not near

Re: distance question

PostPosted: Mon Jun 27, 2011 7:48 pm
by Nykos
Thankls a lot guys, I just tried it and it works but... The robot kills the first two enemies and then stop shooting. He just stays still and waits till enemies invade my screen...

I did what jimmy said, I put the code in the timer:

Code: Select all
if( distance(x, y, enemies.x, enemies.y)<=200)
{
    xvelocity=0;
    CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false);
}

Re: distance question

PostPosted: Tue Jun 28, 2011 8:02 am
by foleyjo
Would you not need to perform the distance calculation for each enemy on the screen?

So it would go something like
where Enemies = result of actorCount on all the enemies


for(n=0; n<Enemies,n++)
{
if( distance(x, y, enemies[n].x, enemies[n].y)<=200)
xvelocity=0;
CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false);
}

Re: distance question

PostPosted: Tue Jun 28, 2011 3:53 pm
by Nykos
yes Foleyjo, i think it should be something like that. I've tried your code but i get an error:"Error line 1; Incompatible types" Maybe the syntax is not good??

Re: distance question

PostPosted: Wed Jun 29, 2011 7:15 am
by foleyjo
:lol: yeah my code was poo to be honest.

It was more of a guide line for how it should be approached.

though Line 1 should be

int n;

and line 2 something like

int enemies = ActorCount("enemy")

Re: distance question

PostPosted: Wed Jun 29, 2011 9:06 am
by Nykos
thanks man, i'll try it right now

Re: distance question

PostPosted: Wed Jun 29, 2011 9:15 am
by Nykos
I'm so sorry, but i can't make it work, I don't know what i did wrong but i still get errors...
Here's the code i wrote:
Code: Select all
int n;
int enemies = ActorCount("enemy");
for(n=0; n<enemies,n++)
{
    if( distance(x, y, enemies[n].x, enemies[n].y)<=200)
    {
        xvelocity=0;
        CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false);
    }
}


and i get those errors:
error line 3: Expected ;
error line 5: unknown type of binhconst
error line 10: Expected ;

Re: distance question

PostPosted: Wed Jun 29, 2011 11:19 am
by foleyjo
Yeah that really won't work because the syntax is awful.

it's more of a guide to how you should be looking at doing it.

I'm not the best for coding. I did notice that

Code: Select all
for(n=0; n<enemies,n++)

should be
Code: Select all
for(n=0; n<enemies;n++)



I think there might be a problem using
Code: Select all
 if( distance(x, y, enemies[n].x, enemies[n].y)<=200)


I think you have to use a getclone type command (search the forums for getclone2) to find which actor it is and then do the test.

Re: distance question

PostPosted: Wed Jun 29, 2011 12:12 pm
by Nykos
thanks a lot man, I'm not bast at coding too.
I'll work on it today. :)

Re: distance question

PostPosted: Wed Jun 29, 2011 12:14 pm
by foleyjo
Just found this on the forums by the great bee-ant. This is how you should do it
note I have made some small changes to the original code to make it relate more to you and added some comments

Code: Select all
int i;
Actor *check;
char str[32];

for(i=0;i<ActorCount("enemy");i++)
{
    sprintf(str,"enemy.%i",i);  // this will make str = the clone you are checking
    check=getclone(str);  //you could also try getclone2(enemy,i) but you will need to add the getclone 2 to your global scripts.
    if(distance(x,y,check->x,check->y)<200)  //As check is the current clone you are checking you can now look at its distance
    {
        xvelocity=0;
        CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false);
    }
}


viewtopic.php?f=2&t=9713&p=67053&hilit=getclone2#p67053

Re: distance question

PostPosted: Wed Jun 29, 2011 12:40 pm
by Nykos
well, first, thanks a lot to foleyjo, game a gogo and jimmynewguy for your time, you've all been a great help.

I mixed some of your codes with another one i found on the forum and it woprks perfectly ( I've benn running it for ten minutes just to be sure and no problem).

here's what i did:
Code: Select all
int n;
int enemies = ActorCount("enemy");
for(n=0; n<enemies;n++)
{
    Actor *check;
char str[32];
int i,point,dist=1000;

//Search the closest target
for(i=0;i<ActorCount("enemy");i++)
{
    sprintf(str,"enemy.%i",i);
    check=getclone(str);
    if(distance(x,y,check->x,check->y)<dist)
    {
        dist=distance(x,y,check->x,check->y);
        point=i;
    }
}
    {
        xvelocity=0;
        CreateActor("projectile", "icon", "(none)", "(none)", 0, 0, false);
    }
}





Once again, thanks a lot guys, +1 for everyone :D

Re: distance question

PostPosted: Wed Jun 29, 2011 1:13 pm
by foleyjo
I'm not sure why you have 2 for loops doing the same loop there?

Re: distance question

PostPosted: Wed Jun 29, 2011 5:38 pm
by Nykos
honestly, i don't really know, i was so tired with this code that i put some of a code, and some of another one, shaked it all with ice, lemon and rhum, and it worked.
I think I re-created the frankenstein monster :D ...
BUt seriously, as i said before, I'm not the best at coding, so if anyone has any idea how to optimize it, or to erase anything obselete, please tell me.