Crane for greenfoot
public class crane extends Actor {
private Container container;
public void act() {
//some other stuff for moving etc.
if (container == null && Greenfoot.isKeyDown("p") && getObjectsInRange(25, Container.class)) {
//"p" for pick up; you can use any other key if you want;
//25 is the radius in which a container needs to be to pick it up; You can also use any other value;
container = (Container) getObjectsInRange(25, Container.class).get(0);
}
if (container != null && Greenfoot.isKeyDown("d")) {//"d" for drop;
//drop the container;
//you should add a method in the container that does something when it's droped;
container.doSomething();//this line will not compile because there is no doSomething() method in the container class;
//It's just an example so you can delete this line;
container = null;
}
}
public void setLocation(int x, int y) {
//by overriding this method you move the container when you have picked up one;
super.setLocation(x, y);
if (container != null) {
container.setLocation(x, y+5);
//the +5 makes the container stay five fields underneath the crane; You can again use any value you want here;
}
}
}