This site requires JavaScript, please enable it in your browser!
Greenfoot back
Stoeptegel
Stoeptegel wrote ...

2015/1/14

Made crane lifting containers, doesn't work properly.

Stoeptegel Stoeptegel

2015/1/14

#
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:
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);
            }
    }
Code in the magnet class which is involved:
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;
    }
Thanks for your help
danpost danpost

2015/1/14

#
The problem is that the magnet does not really know which container it has grabbed; and, the containers, after getting the magnet values cannot tell if the container grabbed is itself or not. Instead of a boolean 'containerIsGrabbed' field in the magnet class, it would seem more appropriate to have a Container reference field called something like 'containerGrabbed'. In this, the containers do not need to know anything about their current state with the magnet. Being the movement of the container will be controlled by the movement of the magnet, it would also seem more appropriate to have the magnet move the container and not try to have the container move with the magnet. The 'containerGrabbed' field in the class of the magnet will come in handy in doing that.
Stoeptegel Stoeptegel

2015/1/15

#
Thanks danpost, i applied the object reference field and stuff, but somehow it doesn't deselect the unit which is stored in the field and only grab that one. I have called it this:
((Harbor)getWorld()).setContainerGrabbed(this);
And it properly selects a container, how can i make it deselect?
danpost danpost

2015/1/15

#
It appears you did not fully grasp what I was saying. The line you just gave above might be something you would find in the Container class. Again, the containers do not know they are being grabbed -- only the magnet would know which container is being grabbed. Also, the field to hold the container grabbed would be in the magnet class -- as it would be the state of that particular magnet (if you had more magnets, they could be holding other containers). For deselecting, when the magnet (again, notice that it is that magnet that is acting here) has moved the container to wherever it is to go, it will release the container (tell the container it is loaded, clear the 'containerGrabbed' field and then start moving to get any remaining containers. As a safety check, or if magnet location and direction of movement is not enough to maintain control of the way things are to go, you can check the loaded state of the container before grabbing one. As far as I can tell right now, the only code that should be in the class of the container is a field for its loaded state and a getter/setter methods for that field. If you have different types of containers, then a field for its type and getter/setter methods for that field would also be included.
Stoeptegel Stoeptegel

2015/1/15

#
Thanks danpost, that was what i was doing. I wondered how to clear that field. Set "this" to "null" didn't work.
Stoeptegel Stoeptegel

2015/1/15

#
And how can i make the magnet move the containers? I don't even know how i can call containers to move in the magnet class. :(
danpost danpost

2015/1/15

#
Stoeptegel wrote...
Thanks danpost, that was what i was doing. I wondered how to clear that field. Set "this" to "null" didn't work.
You will not be using any code that requires that line of code, or anything like it.
Stoeptegel wrote...
And how can i make the magnet move the containers? I don't even know how i can call containers to move in the magnet class. :(
You need to first figure out how to assign the container grabbed to the 'containerGrabbed' field. Once you do that, you can just use something like the following in the act method of the magnet class to move the container:
if (containerGrabbed != null) containerGrabbed.setLocation(getX(), getY()+(getImage().getHeight()+containerGrabbed.getImage().getHeight())/2);
Stoeptegel Stoeptegel

2015/1/15

#
Thanks! That works! It only still picks up one container tho. For testing i have a red one and a yellow one which i spawn in the world constructor. For some reason it can only grab the same container even when i hover over the other container, it will just teleport the other container to the magnet. Sorry if i'm asking so much, it's for a project and i need to get this working. :O
danpost danpost

2015/1/15

#
Stoeptegel wrote...
It only still picks up one container tho.
What is it?
i have a red one and a yellow one which i spawn in the world constructor.
A red and a yellow what?
it will just teleport the other container to the magnet.
What is the other container (as opposed to what)?
You need to login to post a reply.