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

2012/4/11

One actor following another

hkinney hkinney

2012/4/11

#
Hey all, kinda new to Greenfoot, but have been programming Java for about a year. I have two classes, Person and Animal, and I'm trying to get Animal to follow Person. I have absolutely no idea how to send the coordinates of Person to the Animal class. I think I need to make the instance of the Person an Actor, and then just use actor.getX(), but I'm not sure how to make the instance of the Person an Actor. I tried something like...
1
2
int range = 600;
Actor actor = getObjectsInRange(range, Person.class);
but I couldn't get that to work, so I'm not entirely sure what to do. I get an incompatible types error when I try that, so I'm not entirely sure what I'm doing wrong. I feel like I have my parameters right for getObjectsInRange, but apparently not. Any help would be appreciated!
matt.milan matt.milan

2012/4/11

#
if there's only 1 person u could try: Person myPerson = (Person) getObjectsInRange(range, Person.class) For more info on this, look up casting
matt.milan matt.milan

2012/4/11

#
you would also want a if (myPerson != null) after the "Person person = ..." code, and before the following code, to prevent a null pointer error
bourne bourne

2012/4/11

#
Well first thing getObjectsInRange doesn't return an Actor which you are trying to stuff it into one. It instead returns a list of Objects. Does Person appear more than once? I mean do you ever have more than one Person? If not then you could have a static Person. Something like have the following in the Person class: public static Person main; public Person() { main = this; } SO then from the Animal class you could obtain the Person by using Person.main And theres a method that works nicely if you need to follow some position public void turnTowards(int x, int y) So you could have the Animal have something like this: turnTowards(Person.main.getX(), Person.main.getY()); // Make direction towards Person move(1); // Move 1 distance in direction (towards Person) If you don't like the static variable approach then you could do something like the following to get the Person: Person p = (Person)(getWorld().getObjects(Person.class).get(0));
rick rick

2012/4/11

#
Bourne, note that the turnTowards() method is only available in the Greenfoot beta version. But is easily implemented by conversion of cartesian coordinates to polar coordinates (see Wikipedia):
1
2
3
4
5
6
7
8
9
10
11
12
public void turnTowards (int x, int y)
 {
     double dx = x - getX();
     double dy = y - getY();
     double angle = Math.atan2(dy,dx)*180.0/Math.PI;
     setRotation( (int)angle );
 }
 
public void turnTowards (Actor a)
{
     turnTowards(a.getX(), a.getY());
}
CKnox CKnox

2014/12/16

#
So I know this post is old, but how can I put the above code into an if statement. It compiles when I put it in like above, but I need it to go into a if statement.
danpost danpost

2014/12/16

#
@CKnox, the code for these method are only executable actions. There is nothing in them that could be tested; so, you must not be explaining youself very well. What exactly is the 'if' condition and action you want to perform. That is 'I want that if --such-n-such-- then --such-n-such-- should happen.'
lordhershey lordhershey

2014/12/16

#
What are you trying to do?
CKnox CKnox

2014/12/17

#
Oh sorry ok well this is the code in an Actor class named Tail:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Tail here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Tail extends Actor
{
    long frameCount;
    /**
     * Act - do whatever the Tail wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        if(frameCount % 30 == 0){
             
        }
         
        frameCount++;
    }  
     public Tail()
    {
        frameCount = 0;
         
        GreenfootImage image = new GreenfootImage(Ground.CELL_SIZE,Ground.CELL_SIZE);
        image.setColor(java.awt.Color.BLACK);
        image.fillRect(0,0,Ground.CELL_SIZE,Ground.CELL_SIZE);
        setImage(image);
    }
    public void turnTowards(int x, int y)
    {
        double dx = x - getX();
        double dy = y - getY();
        double angle = Math.atan2(dy,dx)*180.0/Math.PI;
        setRotation( (int)angle );
    }
    public void turnTowards (Actor a)
    {
        turnTowards(a.getX(), a.getY());
    }
}
And what I want to do is call the turnTowards method from the if (frameCount method in the public void act. Sorry again for the confusion.
danpost danpost

2014/12/17

#
In order to call either turnTowards method, you will need the coordinates or the actor to turn towards.
davmac davmac

2014/12/17

#
Quick example:
1
2
3
if(frameCount % 30 == 0){
     turnTowards(100,100);  // turns towards the location (100,100)
}
As danpost suggests, you need to identify what it is that you want to turn towards.
CKnox CKnox

2015/1/6

#
OK thankyou, but what if the Actor its turning towards is constantly moving? The Worm Actor that I need it to Turn Towards is always moving.
davmac davmac

2015/1/6

#
CKnox wrote...
OK thankyou, but what if the Actor its turning towards is constantly moving? The Worm Actor that I need it to Turn Towards is always moving.
Then you need to have a reference to the target actor, and call getX() and getY() on it to find the location to turn towards. There's a tutorial which covers accessing one object from another, which you might find useful.
CKnox CKnox

2015/1/7

#
OK thankyou thats very helpful!
You need to login to post a reply.