So in my Scenario I have a class called Block.class and a Player named Pengu.class. Now what I want to do is: when a Pengu is pushing a Block that is in contact with a Block, both Blocks get pushed and not overlayed. The code of pushing the Block should be in the Block.class if possible.
Code of Block.class
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class Block extends Mover
{
/**
* Act - tut, was auch immer Block tun will. Diese Methode wird aufgerufen,
* sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden.
*/
public void act()
{
checkFall();
moveBlock();
}
public void checkFall()
{
if (onGround()) {
setVSpeed(0);
}
else {
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
}
public boolean onGround()
{
Cloud under1 = (Cloud) getOneObjectAtOffset(0, getImage().getHeight()/2-6 , Cloud.class);
Cliff under2 = (Cliff) getOneObjectAtOffset(0, getImage().getHeight()/2-6 , Cliff.class);
Block under3 = (Block) getOneObjectAtOffset(0, getImage().getHeight()/2-6 , Block.class);
Platform under4 = (Platform) getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
return (under1 != null)||(under2 != null)||(under3 != null)||(under4 != null);
}
public void moveBlock()
{
if(isTouching(Pengu.class)) {
if (Greenfoot.isKeyDown("Shift") == false)
{
if (Greenfoot.isKeyDown("A") )
{
setLocation ( getX() - Mover.speed, getY());
}
if (Greenfoot.isKeyDown("D") )
{
setLocation ( getX() + Mover.speed, getY());
}
}
if(isTouching(Block.class)) {
if (Greenfoot.isKeyDown("Shift") == false)
{
if (Greenfoot.isKeyDown("A") )
{
setLocation ( getX() - Mover.speed, getY());
}
if (Greenfoot.isKeyDown("D") )
{
setLocation ( getX() + Mover.speed, getY());
}
}
}
}
}
}

