Hi, I'm really new to green foot and have been working on coding a 2d racing game for the past week. In order to get the barriers around the race track to work I implemented the following code I found made by danpost. The code works and stops the cars from going out of the track, but they won't turn left or right anymore, instead the car goes up and down vertically.
Here is the code I implemented for the barriers:
This is the code prior to me creating the barriers
any assistance you guys could give would be greatly appreciated
public void act() { int dx = 0, dy = 0; if (Greenfoot.isKeyDown("W")) dx = 6; if (Greenfoot.isKeyDown("S")) dx = -6; if (Greenfoot.isKeyDown("A")) dy = -4; if (Greenfoot.isKeyDown("D")) dy = 4; setLocation(getX()+dx, getY()+dy); if (isTouching(Tire.class)) { setLocation(getX()-dx, getY()-dy); } }
public void act() { if (Greenfoot.isKeyDown("Up")) { move(6); } if (Greenfoot.isKeyDown("Down")) { move(-6); } if (Greenfoot.isKeyDown("Right")) { turn(4); } if (Greenfoot.isKeyDown("Left")) { turn(-4); }