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

2015/1/17

How to check actor compatibility?

Stoeptegel Stoeptegel

2015/1/17

#
I have trucks and containers, and the containers can only get loaded onto the trucks if they are the same color. I assume i need to call the color values of the "containerPicked" and not the container class itself because it might not check the right container. Does anyone know how the "containerPicked" can take values from the container class? Anyone ideas?
 public void containerContact ()
    {
        Harbor world = (Harbor)getWorld();
        Container container = world.getContainer(); 
        containerPicked = getOneObjectAtOffset(0, (getImage().getHeight()/2) - containerHeight, Container.class);
 
        if (! isStillHeld && containerPicked != null) // isStillHeld checks if another object is controlling the container right now
        {
            

            if (this.isBlue == true && container.getBlue() == true) {
                setContainerLoaded(containerPicked);
                isLoaded = true;
            }
            else if (this.isRed == true && container.getRed() == true) {
                setContainerLoaded(containerPicked);
                isLoaded = true;
            }
            else if (this.isGrey == true && container.getGrey() == true) {
                setContainerLoaded(containerPicked);
                isLoaded = true;
            }
            else if (this.isYellow == true && container.getYellow() == true) {
                setContainerLoaded(containerPicked);
                isLoaded = true;
            }
        }
    }
danpost danpost

2015/1/17

#
Line 5 uses 'getOneObjectAtOffset'. The documentation shows that it returns an object of type Actor (regardless of what class of object you are looking for). To access anything in the Container class with the field 'containerPicked', you need to let it be known that the field holds a Container type object.
Container cp = (Container)containerPicked;
This line takes the actor (containerPicked) and casts it as a Container type object and then sets the new variable 'cp', which is declared to hold a Container object, to that actor.
You need to login to post a reply.