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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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; ... } |
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 | // 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()); } |