子弹没有射到我的宇宙飞船精灵旁边(LIbgdx)

时间:2015-06-07 17:45:11

标签: java android libgdx game-physics

我每次触摸屏幕时都试图从子弹精灵中射出一颗子弹,但子弹精灵只是从屏幕的左角移动。请帮忙

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

主类

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 个答案:

答案 0 :(得分:1)

问题是你把每个新子弹都放到shipLocation,这在比赛期间没有更新。使用船舶的当前坐标并改为创建新的2d向量。