I have been working in Java for the past week, and I am taking an online/offline course where we work with Processing. This code is supposed to create a rocket that would fly around the screen based on the input of the user. However, it always flies backwards, and the only way to prevent it from flying backwards is to press forward. Even then, it only stands still. Through some testing, we have found out that the movingBackward variable is constantly triggered, but there seems to be no reason for it. My teacher and I are stumped, and any and all suggestions/advice are greatly appreciated.
package processing2;
import processing.core.PApplet;
public class Processing2 extends PApplet
{
private static final long serialVersionUID = 1L;
public float rotationAmount = 180;
public boolean rotateLeft = false;
public boolean rotateRight = false;
public float speed = 10;
public float x = 400;
public float y = 350;
public boolean moveForward = false;
public boolean movingBackward = false;
public boolean moving = false;
public void setup()
{
size(800, 700);
}
public void draw()
{
background(255, 255, 255);
move();
changeRotation();
translate(x, y);
rotate(rotationAmount);
drawRocketShip();
}
public int rocketX = 0;
public int rocketY = 0;
public void drawRocketShip()
{
stroke(0, 149, 185);
fill(0, 149, 185);
rect(rocketX, rocketY, 75, 50);
triangle(rocketX + 75, rocketY + 1, 100, rocketY + 25, rocketX + 75, rocketY + 49);
fill(255, 255, 255);
ellipse(rocketX + 60, rocketY + 25, 30, 15);
stroke(0, 149, 185);
strokeWeight(3);
fill(255, 255, 255);
triangle(rocketX + 25, rocketY, rocketX - 15, rocketY - 25, rocketX, rocketY);
triangle(rocketX + 25, rocketY + 50, rocketX - 15, rocketY + 75, rocketX, rocketY + 50);
if(moving)
{
fill(255, 0, 0);
noStroke();
triangle(rocketX - 10, rocketY + 10, rocketX - 30, rocketY + 25, rocketX - 10, rocketY + 40);
}
}
public void keyPressed()
{
if(key == 'a')
{
rotateLeft = true;
}
if(key == 'd')
{
rotateRight = true;
}
if(key == 'w')
{
moveForward = true;
moving = true;
}
if (key == 's');
{
movingBackward = true;
moving = true;
}
}
public void keyReleased()
{
if(key =='a')
{
rotateLeft = false;
}
if(key == 'd')
{
rotateRight = false;
}
if(key == 'w')
{
moveForward = false;
moving = false;
}
if (key == 's');
{
movingBackward = false;
moving = false;
}
}
public void move()
{
if(moveForward)
{
x += speed * cos(rotationAmount);
y += speed * sin(rotationAmount);
}
if(movingBackward);
{
x += -speed * cos(rotationAmount);
y += -speed * sin(rotationAmount);
}
}
public void changeRotation()
{
if(rotateLeft)
{
rotationAmount -= .08;
if(rotationAmount < 0)
{
rotationAmount = 2 * PI;
}
}
if(rotateRight)
{
rotationAmount += .08;
if(rotationAmount > 2* PI)
{
rotationAmount = 0;
}
}
}
}
if(movingBackward);
you are effectively telling java to do nothing if movingBackward is true. As a result, it will treat the curly braces after as a simple block that has no particular syntactic effect and is always processed. Remove the semicolon and it should work.
Same issue at both instances of
if (key == 's');
I personally prefer placing opening braces on the same line as the if (or try, catch, else, etc...), which makes mistakes like these easier to spot. That's preference though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With