Hello. I am having a problem with my "OppositeWorld" game. Every time any of the two Ladybugs has a wall in front of it, it cannot move. I just wanted it to be so the ladybugs cannot move forward if there is a wall in front of it.
Here is the code for the Ladybugs superclass:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ladybugs extends Actor
{
protected static final int EAST = 0;
protected static final int SOUTH = 90;
protected static final int WEST = 180;
protected static final int NORTH = 270;
private int counter = 0;
/**
* Moves the object one cell in the direction it is facing
*/
public boolean canMove()
{
return !facingWall() && !facingEdge();
}
public boolean facingEdge()
{
switch(getRotation()){
case EAST: return getX()==getWorld().getWidth()-1;
case WEST: return getX()==0;
case NORTH: return getY()==0;
case SOUTH: return getY()==getWorld().getHeight()-1;
}
return false;
}
public boolean facingWall()
{
int xOff = 0, yOff = 0;
switch(getRotation()){
case EAST: xOff=1; break;
case SOUTH: yOff=1; break;
case WEST: xOff=-1; break;
case NORTH: yOff=-1; break;
}
return getOneObjectAtOffset(xOff, yOff, Wall.class)!=null;
}
}
Here is the code for the Ladybug1 class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ladybug1 extends Ladybugs
{
private int counter = 0;
public Ladybug1(int size)
{
getImage().scale(size,size);
setRotation(0);
}
public Ladybug1()
{
this(40);
}
public void act()
{
if(canMove()){
checkKeys(); //moves forward one cell
}
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("left")){
setRotation(180);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
if(Greenfoot.isKeyDown("right")){
setRotation(0);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
if(Greenfoot.isKeyDown("up")){
setRotation(270);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
if(Greenfoot.isKeyDown("down")){
setRotation(90);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
}
}
Here is the code for the Ladybug2 class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ladybug2 extends Ladybugs
{
private int counter = 0;
public Ladybug2(int size)
{
getImage().scale(size,size);
setRotation(180);
}
public Ladybug2()
{
this(40);
}
public void act()
{
if(canMove()){
checkKeys(); //moves forward one cell
}
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("left")){
setRotation(0);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
if(Greenfoot.isKeyDown("right")){
setRotation(180);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
if(Greenfoot.isKeyDown("up")){
setRotation(90);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
if(Greenfoot.isKeyDown("down")){
setRotation(270);
counter++;
if(counter==5){
move(1);
counter=0;
}
}
}
}
Thank you for your help and time.

