简体   繁体   English

子弹不在我的太空飞船精灵(LIbgdx)旁边射击

[英]Bullet not shooting next to my spaceship sprite(LIbgdx)

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;
    }
  }
}

The problem is you put every new bullet to shipLocation , which is not updated during the game. 问题是您将所有新项目符号放置到shipLocation ,游戏中该项目符号不会更新。 Use current coordinates of the ship and create a new 2d vector instead. 使用船的当前坐标并创建一个新的2d向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM