-
-
Save anonymous/160881ed9ded965ed129 to your computer and use it in GitHub Desktop.
Getting objects at your crane
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment