Dear fellow greenfooters,
I want a container, to get fixed at the top of the crane when I click on a container. I've been trying many codes and Im really stuck.
This is what I (with the help of another post on this forum) got by now.
Whats wrong with this code?
This screenshot might help in understanding the scenario:
Thanks in advance
public class Crane extends Actor
{
private Container7 container;
public void act() {
if (!getIntersectingObjects(Container7.class).isEmpty()) {
}
if (container == null && Greenfoot.isKeyDown("p") && !getObjectsInRange(25, Container.class).isEmpty()) {
container = (Container7) getObjectsInRange(25, Container7.class).get(0);
}
if (container != null && Greenfoot.isKeyDown("d")) {
container = null;
}
}
public void setLocation(int x, int y) {
super.setLocation(x, y);
if (container != null) {
container.setLocation(x, y+5);
}
}
Thanks in advance


Also, you can rotate the crane (with the mouse) and move the crane to the left and right using the arrow keys at the same time.