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

2014/1/6

How to "pick up", "move" and "drop" other objects

FoXX FoXX

2014/1/6

#
Hi all! I'm Foxx! Its my first time here. Nice to meet you all =) Right now, im trying to program a game. Its all about moving containers from a ship to a port. At the port the player needs to drop the container at a place where there's enough space. I've made a crane object with Greenfoot and gave it a keylistener in the act-method (up, down, left, right arrows). So its already possible to move the crane. But i still cannot pick up, move and drop containers. I also need a function(i think "if(...){ }") to control the player("is he smashing other containers with his crane? If yes, game over!" or "Player has touched the water, so game over!") Could someone please explain how i should do that? If needed i could also post a pic of the game... general information: -1 world -10 actors(crane, container1, container2 water, boat, etc) Excuse me, if i ask too much...
Gevater_Tod4711 Gevater_Tod4711

2014/1/6

#
To check whether the player touches other objects with the crane you can use the method getIntersectingObjects(java.lang.Class cls) from the Greenfoot API. E.g. to check whether the crane is touching the water you could do this:
//in the act method of your crane:
if (!getIntersectingObjects(water.class).isEmpty()) {
    //The player is touching the water -> Game Over;
}
To check whether the player smashes other containers you could probably use the same code. Picking up, moving and droping containers is more difficult. Therefore you need a variable in the crane that referes to the current picked up container (if a container is picked up at the moment). You could pick up, and drop a container using a key when the crane is near a container. Therefore it would be good to have a superclass for all the containers. When your superclass is called Container you can use some code like this in your crane:
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;
        }
    }


}
FoXX FoXX

2014/1/6

#
Gevater_Tod4711 Thank you for your reply! It all worked with getIntersectingObjects! Thanks again! Unfortunately i still struggle with the second part of the story. Maybe i just didn't tell you the whole idea. Maybe this printscreen will help to understand... About the Superclass-part: im not sure how to implement it here. I have a normal Actor class called "container". If i type private container container; , does it mean i now create a subclass of "container" called "container"? I also got this Syntaxerror, it tried some things but i always have the same part turning red with the same error... Another question: i created two classes: "container" and "container2". containter is = and container2 is | | Is there another way to, lets say, turn the object? Many thanks for your help!
Gevater_Tod4711 Gevater_Tod4711

2014/1/6

#
I think I now understand how you want the game to work. Which part is it you're having problems with? A superclass is a class that is "abouve". E.g. all your actors in the world (container, kraan1, ...) are subclasses of Actor so Actor is their superclass. You can create a superclass for your container classes by creating a new subclass of actor and then setting your classes container and container2 subclasses of this class. You can make them subclasses by changing the class declaration: Instead of public class container extends Actor you can write public class container extends ContainerSuperclass or however your superclass is called. The syntax error was my fail. The line should look like this: if (container == null && Greenfoot.isKeyDown("p") && !getObjectsInRange(25, Container.class).isEmpty()) {
0868626 0868626

2014/1/16

#
g
You need to login to post a reply.