Constant input received while processing despite no input - Java

I have been working in Java for the last week and I am taking an online course where we work with processing. This code should create a rocket that will fly across the screen based on user input. However, it always flies in the opposite direction, and the only way to prevent it from flying backward is to push forward. Even then, it only stands still. After some testing, we found that the movingBackward variable gets triggered all the time, but there doesn't seem to be a reason for that. My teacher and I are stumped and any suggestions and advice is 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;
        }
    }
}

      

}

+3


source to share


1 answer


if(movingBackward);

      

you are effectively telling java not to do anything if movingBackward

- true

. As a result, it will process curly braces after it is a simple block that has no particular syntax effect and is always processed. Remove the semicolon and it should work.

Same problem in both instances



if (key == 's');

      

I personally prefer to place the opening brace on the same line as that if

(or try

, catch

, else

etc.)), making mistakes like these are easier to detect. This is a preference though.

+5


source







All Articles