This site requires JavaScript, please enable it in your browser!
Greenfoot back
MoizRashid
MoizRashid wrote ...

2014/1/15

Racing game

MoizRashid MoizRashid

2014/1/15

#
I have this racing game for school, and I need some help. Currently, my track is white, and the walls are black. I used this code to stop it from going into the wall, it does stop, but gets stuck in the wall and cannot move, please help!!
/**
     * Checks if player is close to the wall. If yes, the player will stop, 
     * if not, the player will continue moving.
     */
    public boolean canMove(String direction, GreenfootImage background) { 
        try {
            if(direction == "up") {
                if(background.getColorAt(getX(),getY() - 2).equals(Color.BLACK) || 
                background.getColorAt(getX() -2,getY() - 2).equals(Color.BLACK)
                || background.getColorAt(getX() + 2,getY() - 2).equals(Color.BLACK)) {
                    return false;
                }
            } else if(direction == "down") {
                if(background.getColorAt(getX(),getY() + 2).equals(Color.BLACK)
                || background.getColorAt(getX() - 2,getY() + 2).equals(Color.BLACK)
                || background.getColorAt(getX() + 2,getY() + 2).equals(Color.BLACK)) {
                    return false;
                }
            } else if(direction == "left") {
                if(background.getColorAt(getX() - 2,getY()).equals(Color.BLACK)
                || background.getColorAt(getX() - 2,getY() + 2).equals(Color.BLACK)
                || background.getColorAt(getX() - 2,getY() - 2).equals(Color.BLACK)) {
                    return false;
                }
            } else if(direction == "right") {
                if(background.getColorAt(getX() + 2,getY()).equals(Color.BLACK)
                || background.getColorAt(getX() + 2,getY() - 2).equals(Color.BLACK)
                || background.getColorAt(getX() + 2,getY() + 2).equals(Color.BLACK)) {
                    return false;
                }
            }
        } catch(Exception e) {
            return false;
        }
        return true;
    }
Conrox27 Conrox27

2014/1/15

#
well i have been having the same problem so i cantr help
You need to login to post a reply.