I made a crane which lifts containers of a boat (in a 2D sideview). When i want to lift 1 container with the magnet attached to the crane, it picks up all of the containers on the map. I don't know what causes this and wondered if i could get any help.
All the values passed from class to class work (they interact) so i don't find it necessary to post them all.
Code in the container class which is involved:
Code in the magnet class which is involved:
Thanks for your help
public void act()
{
if (! isGrabbed) {
getSpeed(); // gets the speed of the containers when the boat is moving
setSpeed();
containerFall();
}
retrieveMagnetValues();
if (isGrabbed) {
moveWithMagnet();
}
}
public void retrieveMagnetValues()
{
Harbor world = (Harbor)getWorld();
Magnet magnet = world.getMagnet();
isGrabbed = magnet.getIsGrabbed();
magnetX = magnet.getMagnetX();
magnetY = magnet.getMagnetY();
}
public void moveWithMagnet()
{
setLocation(magnetX, magnetY+30);
}
public void containerFall ()
{
if (getX() < 416 && getY() < 380 - correction) {
setLocation(getX(), getY()+fallingSpeed);
}
else if (getX() >= 416 && getY() < 394 - correction) {
setLocation(getX(), getY()+fallingSpeed);
}
}public void grabContainer()
{
Actor container = getOneObjectAtOffset(0, getImage().getHeight()/2, Container.class);
if (container != null)
{
containerIsGrabbed = true;
}
if (Greenfoot.isKeyDown("space")) {
containerIsGrabbed = false;
}
}
public boolean getIsGrabbed()
{
return containerIsGrabbed;
}

