In the code below you can see my code for the Actor Car that is moving by pressing W (forward) / s (backwards) and turning to the direction of the Mouse. It is shooting by pressing the space button and has a delay wich is great and I even needed 2 days to find that out so my last problem for the start of this schoolproject is the direction it shoots : IT goes directly up no matter where i drive / look:
public class Car extends Actor
{
private int ShotTimer = 0;
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());
}
}
