This site requires JavaScript, please enable it in your browser!
Greenfoot back
tylers
tylers wrote ...

2012/4/21

range

tylers tylers

2012/4/21

#
how do you get an actor to shoot at an other actor when the second actor gets in range of the first one? ive got this so far
 java.util.List zom = getObjectsInRange(50,gun.class);
                int z = zom.size();
                for(int i=0; i <z ; i++)
                {
                    
    } 
danpost danpost

2012/4/21

#
Change line 1 to 'List zom = getObjectsInRange(50, gun.class);'. Drop line 2 and change line 3 to 'for (int i = 0; i < zom.size(); i++)'. Insert at line 5 'gun g = (gun) zom.get(i);' My question is this: unless you plan on spraying all incoming gun.class objects at once, why are you iterating through all the gun.class objects in range with the for loop? The only reason I see use for a for loop here is possibly to determine which gun.class object is closest, so it would shoot at that one.
tylers tylers

2012/4/22

#
i want it to shoot at the closest one to it
danpost danpost

2012/4/22

#
You will need to track two things within the for loop, that need to be initialized before the for loop. One to hold to closest gun object found, so far ('closestGun'), and one to hold the closest distance of that gun object ('closeness'). As you go through the for loop, compare the distance of the current gun class object with 'closeness' and if it is closer than replace the values of both variables. When the for loop is done, 'closestGun' will contain the correct gun class object. Calculate the angle between 'this' and the 'closestGun' and pass that to the projectile object class constructor, so it can set its proper rotation.
You need to login to post a reply.