i have a class called man and i want him to follow the mouse only when (W) is pressed can anyone help
Thanks :)
if (Greenfoot.isKeyDown("w")) {
MouseInfo m = Greenfoot.getMouseInfo();
if (m != null) {
turnTowards(m.getX(), m.getY());
}
}
public void turnTo(int x, int y) {
setDirection(x - getX(), y - getY());
}
public void setDirection(int deltaX, int deltaY) {
int direction;
if(deltaX == 0 && deltaY == 0) {
direction = 0;
}
else if(deltaX == 0 && deltaY < 0) {
direction = 270;
}
else if(deltaX == 0 && deltaY > 0) {
direction = 90;
}
else if(deltaX > 0 && deltaY == 0) {
direction = 0;
}
else if(deltaX < 0 && deltaY == 0) {
direction = 180;
}
else {
int w = (int) Math.toDegrees(Math.atan((double)deltaY / (double)deltaX));
if(deltaX > 0) {
direction = w;
}
else {
direction = 180 + w;
}
}
setRotation(direction);
}MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse!=null){
x = mouse.getX();
y = mouse.getY();
}