Is there a way that I can shoot left and face left without having to press the left key? At the moment my player only faces left if I'm pressing the left key, but immediately faces right after I let go. Same goes for shooting. Is there a way to fix that?
Here is my animation code:
And my shooting code:
public void animateRight()
{
swimRight();
if(Greenfoot.isKeyDown("right"))
{
swimRight();
}
}
public void animateLeft()
{
if(Greenfoot.isKeyDown("left"))
{
swimLeft();
}
else if("left".equals(Greenfoot.getKey()))
{
swimLeft();
}
}
public void swimRight()
{
frame = frame + 1;
frame = frame % 3;
String image = "mario"+frame+".gif";
setImage(new GreenfootImage(image));
}
public void swimLeft()
{
frame = frame + 1;
frame = frame % 3;
String downmario = "leftmario"+frame+".gif";
setImage(new GreenfootImage(downmario));
}
public void shoot()
{
if(Greenfoot.isKeyDown("left")) //if left key is pressed, Mario will shoot left
{
DownShell fire = new DownShell();
fire.setSpeed(shellSpeed_);
fire.setRotation(180);
World w = getWorld();
w.addObject(fire, this.getX(), this.getY());
reloadTimer_ = 25;
Greenfoot.playSound("marioshell.wav");
}
else //Mario shoots to the right if left key is not pressed
{
Shell fireThis = new Shell();
fireThis.setSpeed(shellSpeed_);
World w = getWorld();
w.addObject(fireThis, this.getX(), this.getY());
reloadTimer_ = 25;
Greenfoot.playSound("marioshell.wav");
}
}