Hey everyone! I want one actor to follow another. More specifically being in the same X Y coords as another actor. Also, if a button is not being pressed, the following actor goes offscreen automatically. Help would be appreciated! :D


public class A extends Actor { private B b; //some other variables and code; public A(B b) { this.b = b; } public void act() { //follow actor b; setLocation(b.getX(), b.getY()); } } public class B extends Actor { ... //somewhere you have to add the object A to the world. So do this: private A a = new A(this);//this chould be global; getWorld().addObject(a, getX(), getY());//in a method or the construktor; ... }
// in the world class constructor Squggly squggly = new Squggly; Robot robot = new Robot(squggly); // the rest is in the Robot class // instance fields Squggly squggly = null; boolean squgglyShowing = false; // the constructor public Robot(Squggly squggly) { this.squggly = squggly; } // in the act method checkSquggly(); // add the method private void checkSquggly() { if (!squgglyShowing && Greenfoot.isKeyDown("space")) { getWorld().addObject(squggly, getX(), getY()); squgglyShowing = true; } if (squgglyShowing && !Greenfoot.isKeyDown("space")) { getWorld().removeObject(squggly); squgglyShowing = false; } if (squgglyShowing) squggly.setLocation(getX(), getY()); }