distance question

Game Editor comments and discussion.

distance question

Postby Nykos » Mon Jun 27, 2011 4:18 pm

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?
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby jimmynewguy » Mon Jun 27, 2011 6:00 pm

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
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: distance question

Postby Game A Gogo » Mon Jun 27, 2011 6:57 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: distance question

Postby Nykos » Mon Jun 27, 2011 7:48 pm

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);
}
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby foleyjo » Tue Jun 28, 2011 8:02 am

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);
}
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Tue Jun 28, 2011 3:53 pm

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??
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby foleyjo » Wed Jun 29, 2011 7:15 am

: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")
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Wed Jun 29, 2011 9:06 am

thanks man, i'll try it right now
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby Nykos » Wed Jun 29, 2011 9:15 am

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 ;
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby foleyjo » Wed Jun 29, 2011 11:19 am

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.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Wed Jun 29, 2011 12:12 pm

thanks a lot man, I'm not bast at coding too.
I'll work on it today. :)
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby foleyjo » Wed Jun 29, 2011 12:14 pm

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
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Wed Jun 29, 2011 12:40 pm

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
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Re: distance question

Postby foleyjo » Wed Jun 29, 2011 1:13 pm

I'm not sure why you have 2 for loops doing the same loop there?
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: distance question

Postby Nykos » Wed Jun 29, 2011 5:38 pm

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.
Nykos
 
Posts: 110
Joined: Sun Dec 19, 2010 11:11 pm
Score: 6 Give a positive score

Next

Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest