Bullet not shooting next to my spaceship sprite(LIbgdx)

207 views Asked by At

I'm trying to get a bullet to shoot out of the spaceship sprite every time the screen is touched but the bullet sprite just moves from the left corner of the screen. Please help

Bullet Class

public class Bullet {
  public Vector2 bulletLocation = new Vector2(0,0);
  public Vector2 bulletVelocity = new Vector2(0,0);

  public Bullet(Vector2 sentLocation,Vector2 sentVelocity){
    bulletLocation = new Vector2(sentLocation.x,sentLocation.y);
    bulletVelocity = new Vector2(sentVelocity.x,sentVelocity.y);
  }

  public void Update(){
    bulletLocation.x+=bulletVelocity.x;
    bulletLocation.y+= bulletVelocity.y;
  }
}

Main Class

public class MyGdxGame extends ApplicationAdapter implements ApplicationListener,InputProcessor {
  SpriteBatch batch;
  private Sprite spriteLeftButton;
  private Sprite spriteRightButton;
  private Sprite spaceShip;
  private Texture buttonTexture;
  private OrthographicCamera camera;
  private Rectangle leftButton, rightButton, spaceshipr;
  private TextureRegion region;
  private Sprite spriteBullet;
  private Array<Rectangle> raindrops;
  private long lastDropTime;
  private Rectangle raindrop;
  private Rectangle camSize;
  Vector2 shipLocation = new Vector2(0, 0);
  Vector2 cursorLocation = new Vector2(0, 0);
  float screenWidth = 0;
  float screenHeight = 0;

  Bullet testBullet = new Bullet(shipLocation, new Vector2(10, 0));

  ArrayList<Bullet> bulletManager = new ArrayList<Bullet>();

  @Override
  public void create() {

    //gets screen dimensions
    screenWidth = Gdx.graphics.getWidth();
    screenHeight = Gdx.graphics.getHeight();

    raindrops = new Array<Rectangle>();
    raindrop = new Rectangle();
    raindrops.add(raindrop);
    lastDropTime = TimeUtils.nanoTime();

    camera = new OrthographicCamera();
    camera.setToOrtho(false, 800, 480);

    batch = new SpriteBatch();
    buttonTexture = new Texture(Gdx.files.internal("pics/idf_frigate_tut_06.jpg"));
    spaceShip = new Sprite(buttonTexture);
    spaceShip.setPosition(300, 300);


    spaceshipr = new Rectangle();
    spaceshipr.x = 300 / 2 - 64 / 2;
    spaceshipr.y = 20;
    spaceshipr.width = 64;
    spaceshipr.height = 64;

    buttonTexture = new Texture(Gdx.files.internal("pics/bullet.jpg"));
    spriteBullet = new Sprite(buttonTexture);

    //try buttonTexture
    Gdx.input.setInputProcessor(this);
  }

  @Override
  public void render() {

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


    camera.update();
    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    batch.draw(spaceShip, spaceshipr.x, spaceshipr.y);
    if(Gdx.input.isTouched()){
      batch.draw(spriteBullet, spaceshipr.x,spaceshipr.y);

      int counter = 0;
      while (counter < bulletManager.size()) {

        Bullet currentBullet = bulletManager.get(counter);
        currentBullet.Update();

        batch.draw(spriteBullet, currentBullet.bulletLocation.x,currentBullet.bulletLocation.y);
        counter++;
      }
    }
    batch.end();
  }

  @Override
  public boolean keyDown(int keycode) {
    return false;
  }

  @Override
  public boolean keyUp(int keycode) {
    return false;
  }

  @Override
  public boolean keyTyped(char character) {
    return false;
  }

  @Override
  public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if (Gdx.input.isTouched()) {
      Vector3 touchPos = new Vector3();
      touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(touchPos);
      spaceshipr.x = touchPos.x - 64;
      spaceshipr.y = touchPos.y - 64;

      raindrop.x = touchPos.x + 50;
      raindrop.y = touchPos.y + 50;

      Bullet myBullet = new Bullet(shipLocation, new Vector2(0,10));
      bulletManager.add(myBullet);
    }

    return true;
  }

  @Override
  public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
  }

  @Override
  public boolean touchDragged(int screenX, int screenY, int pointer) {
    if(Gdx.input.isTouched()) {
      Vector3 touchPos = new Vector3();
      touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(touchPos);
      spaceshipr.x = touchPos.x - 64;
      spaceshipr.y = touchPos.y - 64;
    }
  }
}
1

There are 1 answers

0
vojta On

The problem is you put every new bullet to shipLocation, which is not updated during the game. Use current coordinates of the ship and create a new 2d vector instead.