I need to program for the school a mudball car fighting game and im stuck at the point where the car needs to turn in the opposite direction where he hits the wall and move like 5 foward. I´ve tried once to code it but using:
public void hitWall(){
if(isTouching(Wall.class)) turn(180);
}
didnt work as planned.
And if needed here is my car code :
public class Car extends Actor { private int ShotTimer = 0; private int health = 100; public void act() { if (ShotTimer > 0) { ShotTimer = ShotTimer - 1; } else if (Greenfoot.isKeyDown("space")) { getWorld().addObject(new Shot(this), getX(), getY()); ShotTimer = 200; // delay next shot } MouseInfo mi = Greenfoot.getMouseInfo(); if(mi!=null) turnTowards(mi); if (Greenfoot.isKeyDown("w")) move(2); if (Greenfoot.isKeyDown("s")) move(-2); } public void turnTowards (int x, int y) { double dx = x-getX(); double dy = y-getY(); double angle = Math.atan2(dy,dx)*180.0/Math.PI; setRotation ( (int)angle ); } public void turnTowards (MouseInfo mi) { turnTowards(mi.getX(), mi.getY()); } public void loseHealth(int amount) { health -= amount; if (health < 1) { getWorld().removeObject (this); } } }