I have created a very nice maze but my ball goes right through all of the horizontal walls and it cannot go through the verticals walls sometimes. Can someone help me out with my code?
I originally had different code but my teacher told me to try to use the 'getOneObjectAtOffset' method.
public class Ball extends Animal { private int x = 3; private boolean moving = false; /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); checkHitHWall(); checkHitVWall(); } public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { move(-x); turn(-5); } setRotation(360); if (Greenfoot.isKeyDown("right")) { move(x); turn(5); } setRotation(90); if (Greenfoot.isKeyDown("up")) { move(-x); } if (Greenfoot.isKeyDown("down")) { move(x); } setRotation(0); } public void checkHitHWall() { Actor Ball = getOneObjectAtOffset(getImage().getWidth()/2, getImage().getHeight()/2, HWall.class); if (Ball != null) x = x*-1; } public void checkHitVWall() { Actor Ball= getOneObjectAtOffset(getImage().getWidth()/2, getImage().getHeight()/2, VWall.class); if (Ball != null) x = x*-1; } }