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

2014/4/11

actors follow actors

makinetion makinetion

2014/4/11

#
I have been trying for hours, to figure out how i can make one object follow the other, and this is the closest I've gotten.
public class Arm extends Player
{
    public int xx;
    public int yy;
    /**
     * Act - do whatever the Arm wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(xx,yy);
    }    
    
    public void location(int x, int y)
    {
       xx = x;
       yy = y;
    }
}
 public void act() 
    {
        location(getX(), getY());
}
makinetion makinetion

2014/4/11

#
when i run it the arm just goes to 0,0
Super_Hippo Super_Hippo

2014/4/11

#
The second code, in what class is that written? In a subclass? The point is that you have the ints xx and yy which are 0 at the beginning. Then you have the act method in line 11. setLocation(0,0). That's why the arm will move to (0,0).
danpost danpost

2014/4/11

#
One problem is that an Arm object is not a Player object -- only a part of one; and, therefore, Arm should not extend Player. Better might be fore Arm to be an extension of Actor as an inner class of player (written within the Player class). But I suggest that you leave it as just an Actor subclass of its own; unless, you understand the workings of an inner class. Pass the Player object that the arm belongs to in the constructor of the Arm ( 'new Arm(playerObjectName)' ) and have the Arm constructor save it in an instance field to use to get its location.
You need to login to post a reply.