Code Question

Non-platform specific questions.

Code Question

Postby Zehper48 » Mon Aug 16, 2010 9:51 am

Hey Everybody,
I'm making a tower defense sort of game.
How can I make the bullet from the tower move to the closest enemy clone within a certain pixel distance?
Thanks alot, and tell me If you don't understand what I am trying to do.
I like score
User avatar
Zehper48
 
Posts: 241
Joined: Sun Jun 11, 2006 1:34 am
Location: Advanced Noob
Score: 4 Give a positive score

Re: Code Question

Postby Game A Gogo » Mon Aug 16, 2010 1:04 pm

you need the GetClone2 function; so place this code in the global script and the GetClone2 should work
Code: Select all
Actor *GetClone2(const char *cname, int cindex)
{
    char buffer[50];
    sprintf(buffer,"%s.%i",cname,cindex);
    return(getclone(buffer));
}

and then to make the bullet go towards the closest enemy, here in the create actor of the bullet:
Code: Select all
unsigned int CDist=640;//So 0 isn't the closest value to 0... :P note any clone OUTSIDE this distance will not be considered... so set this at your need
for(i=0; i<ActorCount("Enemy"); i++)//Let's repeat this for all the clones "alive"
{
    Clone=GetClone2("Enemy",i);//Let's get the current clone's information
    if(distance(x,y,Clone->x,Clone->y)<CDist)//Check if the current clone is the closest yet
    {
        angle=direction(x,y,Clone->x,Clone->y); //set the angle
        CDist=distance(x,y,Clone->x,Clone->y); //reset the closest distance
}
directional_velocity=3;//Speed of bullet


I hope this work!
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: Code Question

Postby Bee-Ant » Mon Aug 16, 2010 4:25 pm

You can also use this method :
Bullet -> Create Actor -> Script Editor :
Code: Select all
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;
    }
}

//Shoot the closest target
sprintf(str,"enemy.%i",point);
check=getclone(str);
angle=direction(x,y,check->x,check->y);
directional_velocity=10;
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Code Question

Postby Zehper48 » Tue Aug 17, 2010 11:53 pm

Thanks so much for replying Game A Gogo and Bee-Ant.
Both methods work great, but for each method, how can I make it so that when an enemy is not within the radius, the bullet isnt created?
Thanks so much for helping,
Greatly appreciated =]
I like score
User avatar
Zehper48
 
Posts: 241
Joined: Sun Jun 11, 2006 1:34 am
Location: Advanced Noob
Score: 4 Give a positive score

Re: Code Question

Postby Game A Gogo » Wed Aug 18, 2010 1:07 am

do the same thing for creating the bullet, and check if the clones are withing region and create the actor according to that
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: Code Question

Postby Bee-Ant » Wed Aug 18, 2010 11:58 am

Zehper48 wrote:Thanks so much for replying Game A Gogo and Bee-Ant.
Both methods work great, but for each method, how can I make it so that when an enemy is not within the radius, the bullet isnt created?
Thanks so much for helping,
Greatly appreciated =]

Code: Select all
Actor *check;
char str[32];
int i,point,radius=1000,create;

//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)<radius)
    {
        radius=distance(x,y,check->x,check->y);
        point=i;
        create=1;
    }
}

//Shoot the closest target if someone in range
if(create==1)
{
    sprintf(str,"enemy.%i",point);
    check=getclone(str);
    angle=direction(x,y,check->x,check->y);
    directional_velocity=10;
}
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest