I have it so that the player turns toward the mouse and move back and forth with the W and S keys, but I am not quite sure how to make the player move left and right, while staying relative to the rotation of the player. Please help and thank you!!
I have included the code for the player:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends SmoothMover
{
public boolean atWorldEdge = false;
private double WalkSpeed = 3.0;
private int TurnSpeed = 5;
public boolean Alive;
public double Health = 100;
public int Ammo = 100;
private boolean isShooting;
public int XPos;
public int YPos;
private boolean Moving;
private float Rotation;
private int DamageDelay = 1;
GreenfootImage[] images = new GreenfootImage[20];
private int imageNumber;
public Player()
{
for( int i=0; i<images.length; i++ )
{
images[i] = new GreenfootImage( "survivor-move_shotgun_" + i + ".png" );
}
setImage( images[imageNumber] );
}
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
XPos = getX();
YPos = getY();
if(Health > 0)
{
KeyboardInput();
Rotation = getRotation();
CheckCollision();
if(Moving == true)
{
Animate();
}
}
else
{
getWorld().removeObject(this);
}
}
/**
* KeyboardInput - Gets the keyboard keys that are pressed and acts on it.
*/
public void KeyboardInput()
{
if(Greenfoot.isKeyDown("w"))
{
move(WalkSpeed);
Moving = true;
}
else if(Greenfoot.isKeyDown("s"))
{
move(-WalkSpeed);
Moving = true;
}
else
{
Moving = false;
imageNumber = 0;
}
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null && mouse.getX() != this.getX() && mouse.getY() != this.getY())
{
turnTowards(mouse.getX(), mouse.getY());
}
// if(Greenfoot.isKeyDown("left"))
// {
// turn(-TurnSpeed);
// }
// else if(Greenfoot.isKeyDown("right"))
// {
// turn(TurnSpeed);
// }
// if(Greenfoot.isKeyDown("space"))
// {
// isShooting = true;
// Shoot();
// }
// else
// {
// isShooting = false;
// }
}
public void Animate()
{
imageNumber = ( imageNumber + 1 ) % images.length;
setImage( images[imageNumber] );
}
public void Shoot()
{
getWorld().addObject(new Bullet(getRotation()), getX(), getY());
}
public void CheckCollision()
{
if(getOneIntersectingObject(Zombie.class) != null)
{
DamageDelay--;
if(DamageDelay <= 0)
{
Health = Health - 0.1;
DamageDelay = 1;
}
}
}
}

