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

2015/3/27

calculate distance from one object to another?

h123n h123n

2015/3/27

#
Hi, I have really been trying to come up with a way for my NPC class to figure out how far away (in pixels) class Toby is. The problem is that when I try to use the getX/getY ints, they are not static so I keep getting an error. I don't want to have to make another set of integers just for the X and Y, so could anyone tell me how I could go about doing this? Here's my code for the NPC:
import greenfoot.*;

/**
 * Base class for all npcs in the game
 * 
 * @author (h123n) 
 * @version (1.0)
 */
public class NPC extends ScrollActor
{
    int speed = 5;
    int damage = 10;
    int health = 50;
    int distfromtargx = 0;
    
    String name;
    /**
     * Act - do whatever the NPC wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }
    public void follow()
    {
        Toby.getX() - this.getX() = distfromtargx;
    }
}
thanks.
danpost danpost

2015/3/27

#
Looking at the Actor class API, you will find that the only two methods that return an Actor object are 'getOneIntersectingObject' and 'getOneObjectAtOffset', neither of which will suit your needs. Therefore, you will need to use either the Actor class 'getObjectsInRange' method or the World class 'getObjects' method which both return List objects. You will need to ensure the list returned is not empty before getting the first element from the list (which should be the actor you want, provided there is only one of that type actor in the world). Then, you will need to cast the object extracted from the list as an Actor type before using any Actor class methods on it. Try to create the code according to the above. If you have difficulties, post what you tried and include any error messages you are getting in their entirety.
h123n h123n

2015/3/27

#
Ok, I tried to follow what you said but found it was slightly difficult. I did however come up with a new approach. The idea is that there is a test radius at 100 pixels and if Toby is in there it decreases to 99, then if Toby is still there 98 and so on. The problem is when I run the code it gives me an error in the terminal window. Here's the code:
        import greenfoot.*;

/**
 * Base class for all npcs in the game
 * 
 * @author (h123n) 
 * @version (1.0)
 */
public class NPC extends ScrollActor
{
    int speed = 5;
    int damage = 10;
    int health = 50;
    int tryrange = 101;
    
    String name;
    /**
     * Act - do whatever the NPC wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        follow();
    }
    public void follow()
    {
      tryrange--;
      Toby toby = (Toby)getObjectsInRange(tryrange, Toby.class);
      if (toby != null)
      {
          follow();
      }
    }
}
h123n h123n

2015/3/27

#
actually, I found the error. I had my Guard class (subclass of NPC calling follow();) at the same time as NPC.
danpost danpost

2015/3/27

#
I cannot see how the code in the NPC class could possibly do what you want at all times. Also, if you are running (or, if you have) an 'act' method in your Guard class, then it would override the one in the NPC class; in other words, both will not run 'at the same time' unless you call 'super.act();' from the Guard class within the 'act' method or a method it calls. Actually, I cannot be sure that the best place for the 'follow' method is in the NPC class. Do you have other NPC subclasses that will be calling that method or is Guard the only one? Also, instead of 'looking' for the Toby actor all the time, it would be more direct to keep a reference to it in your subclass of World with a 'getter' method to return it, that these Guard objects (and any other NPCs that need it) can just get it from the world,
You need to login to post a reply.