import greenfoot.*;
/**
* Write a description of class moving here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class moving extends boxplatform
{
/**
* Act - do whatever the moving wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act ()
{
checkWalls();
back();
}
public void back()
{
if(checkWalls() == false)
{
move(2);
}
else{
move(-2);
}
}
public boolean checkWalls()
{
int spriteWidth = getImage().getWidth();
int xDistance = (int) (spriteWidth/2);
Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
if(rightWall == null)
{
return false;
}
else
{
stopByRightWall(rightWall);
return true;
}
}
public void stopByRightWall(Actor rightWall)
{
int wallWidth = rightWall.getImage().getWidth();
int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
setLocation (newX - 5, getY());
}
}