Hey there,
I'm very new to Greenfoot and have a very basic problem that surely everyone of you will be able to solve in a minute:
In my game you are able to push crates (called Crate.class) so that you can safely exit a labyrinth of those. Some rules are that you can only push one crate at once, if there are two behind each other it won't be possible to push them. Furthermore if there's a brick (Cobble.class) which is completely immovable. Means that you can't push a crate if there's a brick behind it.
Now my problem is pushing the crates. What I've done so far is:
and going on to the class Crate:
That piece of code up there where I commented "STUCK HERE" should help me referring to the method of another class, in our case Crate.class. Unfortunately I don't really know how to greenfoot and what it really means. Now I found out that the get(0) refers to the first Object of Cobble.class which has been created. When I insert a second crate and I try pushing that one, the first one moves while my Player is going inside the second crate. So it detects the object as one of the class Crate, but moves the first one created. When I change the number of get() to 1, only the second crate spawned is movable.
Is there a way of referring to the latest detected object from Crate.class? The method getOneObjectAtOffset() should return one specific object, shouldn't it? Can I somehow refer to this detected object and make it move?
I hope I could explain properly what my issue is; I'm neither good at English nor at Greenfoot. If you need more detail please tell me.
Greetings LoopingSample
public class Player extends Actor
{
int rotation;
public static int delay;
public static int hori;
public static int verti;
int count;
// Methoden der Klasse Player (yeah I'm German)
void moveRight() {
if (Greenfoot.isKeyDown("right")) {
setRotation(90);
hori = 1;
verti = 0;
blocked();
pushCrate();
setLocation(getX() + hori, getY());
for (count = 1; count <= 20; count++)
{
if (!Greenfoot.isKeyDown("right")) {
count = 120;
}
Greenfoot.delay(1);
}
}
}
void moveLeft() {
if (Greenfoot.isKeyDown("left")) {
setRotation(270);
hori = -1;
verti = 0;
blocked();
pushCrate();
setLocation(getX() + hori, getY() + verti);
for (count = 1; count <= 20; count++)
{
if (!Greenfoot.isKeyDown("left")) {
count = 120;
}
Greenfoot.delay(1);
}
}
}
void moveUp() {
if (Greenfoot.isKeyDown("up")) {
setRotation(0);
hori = 0;
verti = -1;
blocked();
pushCrate();
setLocation(getX(), getY() + verti);
for (count = 1; count <= 20; count++)
{
if (!Greenfoot.isKeyDown("up")) {
count = 120;
}
Greenfoot.delay(1);
}
}
}
void moveDown() {
if (Greenfoot.isKeyDown("down")) {
setRotation(180);
hori = 0;
verti = 1;
blocked();
pushCrate();
setLocation(getX(), getY() + verti);
for (count = 1; count <= 20; count++)
{
if (!Greenfoot.isKeyDown("down")) {
count = 120;
}
Greenfoot.delay(1);
}
}
}
void blocked() {
if (getOneObjectAtOffset(hori, verti, Cobble.class) !=null) {
hori = 0;
verti = 0;
}
}
public void pushCrate() {
Actor crate = getOneObjectAtOffset(hori, verti, Crate.class);
int var = crate;
if (crate != null) {
if (getOneObjectAtOffset(hori*2, verti*2, Cobble.class) !=null) {
hori = 0;
verti = 0;
}
else {
((Crate)getWorld().getObjects(Crate.class).get(0)).pushCrate(); //STUCK HERE! Got this one from the internet and it doesn't work out properly!
}
}
}
// ACT-METHODE
public void act()
{
moveRight();
moveLeft();
moveUp();
moveDown();
}
public class Crate extends Actor
{
public static boolean blocked;
void pushCrate() {
if (getOneObjectAtOffset(Player.hori, Player.verti, Crate.class) !=null) {
Player.hori = 0;
Player.verti = 0;
}
setLocation(getX() + Player.hori, getY() + Player.verti);
}
void blockedCrate() {
if (getOneIntersectingObject (Cobble.class) != null) {
setLocation(getX() - Player.hori, getY() - Player.verti);
blocked = true;
((Player)getWorld().getObjects(Player.class).get(0)).moveBack();
}
}
public void act()
{
blockedCrate();
}
}

