Why is my bullet stuck while I haven't released my space bar ?

I'm learning Processing by programming a very basic shoot'em'up. Here is the code :

game.pde

PlayerShip theShip;

void setup() {
  size(1024, 768);
  theShip = new PlayerShip(0, height-80);
}

void draw() {
  background(0);
  theShip.draw();
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {
      theShip.setLeft(true);
    } else if (keyCode == RIGHT) {
      theShip.setRight(true);
    }
  }
  if (key == ' ') {
    theShip.setFire(true);
  } 
}

void keyReleased() {
  if (key == CODED) {
    if (keyCode == LEFT) {
      theShip.setLeft(false);
    } else if (keyCode == RIGHT) {
      theShip.setRight(false);
    }
  }
  if (key == ' ') {
    theShip.setFire(false);
  } 
}

MoveableObject.pde

abstract class MoveableObject {
  protected PVector position;
  protected PVector velocity;
  protected PImage img;

  MoveableObject(float startx, float starty) {
    position = new PVector(startx, starty);
  }

  MoveableObject(PVector startPosition) {
    position = startPosition;
  }

  protected void init(String image, float velX, float velY) {
    img = loadImage(image);
    velocity = new PVector(velX, velY);
  }

  protected abstract void update();

  public void draw() {
    if (position.x < 0) {
      position.x = 0;
    }
    if (position.x > width-img.width) {
      position.x = width-img.width;
    }

    if (position.y < 0) {
      position.y = 0;
    }
    if (position.y > height-img.height) {
      position.y = height-img.height;
    }

    image(img, position.x, position.y);
  }
}

PlayerBullet.pde

class PlayerBullet extends MoveableObject {
  private final float VELOCITY_X= 0;
  private final float VELOCITY_Y = -5;
  private boolean visible = false;

  PlayerBullet(float startx, float starty) {
    super(startx, starty);
    init("bullet.png", VELOCITY_X, VELOCITY_Y);
  }

  PlayerBullet(PVector startPosition) {
    super(startPosition);
    init("bullet.png", VELOCITY_X, VELOCITY_Y);
  }

  private void setPosition(float x, float y) {
    position.x = x;
    position.y = y;
  }

  public boolean isVisible() {
    return visible;
  }

  public void hide() {
    visible = false;
  }

  public void show(PVector startPosition) {
    visible = true;
    setPosition(startPosition.x, startPosition.y);
  }

  public void update() {
    position.add(velocity);
    if (position.y < 10) {
      hide();
    }
  }

  @ Override
  public void draw() {
    update();
    super.draw();
  }
}

PlayerShip.pde

class PlayerShip extends MoveableObject {
  private final int VELOCITY_X = 10;
  private final int VELOCITY_Y = 0;
  private PlayerBullet bullet;
  private boolean turnLeft;
  private boolean turnRight;
  private boolean fired;

  PlayerShip(int startx, int starty) {
    super(startx, starty);
    init("spaceship2.png", VELOCITY_X, VELOCITY_Y);
  }

  PlayerShip(PVector startPosition) {
    super(startPosition);
    init("spaceship2.png", VELOCITY_X, VELOCITY_Y);
  }

  private PVector getBulletStartPosition() {
    return new PVector(position.x + img.width/2 -10 , position.y - 25);
  }

  public void setLeft(boolean value) {
     turnLeft = value;
  }

  public void setRight(boolean value) {
     turnRight = value;
  }

  public void setFire(boolean value) {
    fired = value;
  }

  public void update() {
    if (turnLeft) {
      velocity.x = VELOCITY_X * -1;
      position.add(velocity);
    }
    if (turnRight) {
      velocity.x = VELOCITY_X;
      position.add(velocity);
    }
    if (fired) {
      bullet.show(getBulletStartPosition());
    }
  }

  @ Override
  public void init(String image, float velX, float velY) {
    super.init(image, velX, velY);
    bullet = new PlayerBullet(getBulletStartPosition());
  }

  @ Override
  public void draw() {
    update();

    super.draw();
    if (bullet.isVisible()) {
      bullet.draw();
    }
  }
}

Here is my problem : when I press my space bar, the bullet is stuck in front of the ship. I have to release the space bar for the bullet to move. I can't figure this out. Can you help me ?

Thanks in advance

Answers

Sign In or Register to comment.