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

2014/12/22

Getting an x and y location from actors in a neibours list

AndrewHollahan AndrewHollahan

2014/12/22

#
I am making a tower defense game for school and I have come across a problem. I am currently using the get neighbours command to get of list of enemies within the range of the tower. The problem emerges here I need to get the X and Y locations of enemy. After I get the locations I can send those locations to my projectile class so it can move to that location and destroy the object. My teacher and partner suggested assigning a variable in the enemy class that will increment with each one created so I can use a for loop to prioritize the first enemy within range. Sadly the X and Y locations being sent to the actor are not correct causing the projectiles to fly in the wrong direction. the tower is detecting the enemy and firing the projectiles when they come into range but the heading of the projectiles is completely off.
danpost danpost

2014/12/22

#
It is difficult to say how to fix you code when you do not show it (use the 'code' link below the reply box to insert code into your posts -- there is also a link to 'Posting code? read this!' below the box).
AndrewHollahan AndrewHollahan

2014/12/22

#
    public void firing (){
         List Triangles = getNeighbours( 150, true , Triangle.class);
         
         
         if (Triangles.size() != 0){
         Triangle[] array =  new Triangle [Triangles.size()];
         
         Triangles.toArray(array);
         
         int smallestID = 0;
         for ( int i = 0; i <Triangles.size() ; i ++) {
             if (array[smallestID].id > array[i].id){
                 smallestID = i;
                 
                 
                }
             
             
             
         }
          
        int tempX = array[smallestID].getX();
        int tempY = array[smallestID].getY();
             
            
         WaterProjectile water = new WaterProjectile(tempX, tempY);
        //WaterProjectile water = new WaterProjectile(array[smallestID]);
        getWorld().addObject(water, getX(), getY());
        
        
        
        
        
        
        
      }
Super_Hippo Super_Hippo

2014/12/22

#
In the code, there isn't the part for the water projectile to set its rotation. That is probably in the constructor of that class. So the question is, how you use tempX and tempY in the WaterProjectile class to calculate the rotation. Just a few things. I am not sure if it is wanted, but getNeighbours will return objects around the tower in an square area, not a circle. For a circle, you would need to use 'getObjectsInRange'. What does the "id" of a triangle represent? Is it like every triangle has the same speed, so the one who is added first is also closer to the exit? In most TD games, the towers shoot at the enemy which is the closest to the tower. In some games, you can target one, so every tower will shoot at it if possible.
danpost danpost

2014/12/22

#
Instead of passing the location coordinates to the WaterProjectile object being created, just set the rotation right there:
WaterProjectile water = new WaterProjectile();
getWorld().addObject(water, getX(), getY());
water.turnTowards(tempX, tempY);
AndrewHollahan AndrewHollahan

2014/12/22

#
Thank you it works exactly the way it is supposed to now.
AndrewHollahan AndrewHollahan

2014/12/23

#
Sorry hippo I didn't see your reply. The triangle ID was in the triangle class and it would increment everytime a new triangle was created. With the number incrementing the tower would focus the lowest number as it's ID. Lastly I wasn't aware that the neighbours command was a square, it said radius so I assumed it was a circle thank you for the info. I
davmac davmac

2014/12/28

#
Lastly I wasn't aware that the neighbours command was a square, it said radius so I assumed it was a circle thank you for the info.
The documentation doesn't say radius at all. It says:
Return the neighbours to this object within a given distance. This method considers only logical location, ignoring extent of the image. Thus, it is most useful in scenarios where objects are contained in a single cell. All cells that can be reached in the number of steps given in 'distance' from this object are considered. Steps may be only in the four main directions, or may include diagonal steps, depending on the 'diagonal' parameter. Thus, a distance/diagonal specification of (1,false) will inspect four cells, (1,true) will inspect eight cells.
You need to login to post a reply.