libgdx touchDown called just once

前端 未结 3 1484
孤街浪徒
孤街浪徒 2021-01-19 04:03

I\'m new to LibGdx, and have problem with input handling.

My player needs to shoot bullets whenever touch is down. But it seems that this method is called just once.

相关标签:
3条回答
  • 2021-01-19 04:38

    I have use Continuous Input handle

    This is done by setting a flag to your actor eg:

    public class Player{
      private boolean firing = false;
      private final Texture texture = new Texture(Gdx.files.internal("data/player.png"));
    
      public void updateFiring(){
        if (firing){
            // Firing code here
            Gdx.app.log(TAG, "Firing bullets");
        }
      }
    
      public void setFiring(boolean f){
        this.firing  = f;
      }
    
      Texture getTexture(){
        return this.texture;
      }
    
    }
    

    Now in my Application class implementing InputProcessor

    @Override
    public void create() {
        player= new Player();
        batch = new SpriteBatch();
        sprite = new Sprite(player.getTexture());
        Gdx.input.setInputProcessor(this);
    }
    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
        batch.begin();
        sprite.draw(batch);
        // Update the firing state of the player
        enemyJet.updateFiring();
        batch.end();
    }
    
    @Override
    public boolean keyDown(int keycode) {
        switch (keycode){
            case Input.Keys.SPACE:
                Gdx.app.log(TAG, "Start firing");
                player.setFiring(true);
    
        }
        return true;
    }
    
    @Override
    public boolean keyUp(int keycode) {
        switch (keycode){
            case Input.Keys.SPACE:
                Gdx.app.log(TAG, "Stop firing");
                player.setFiring(false);
    
        }
        return true;
    }
    

    Done

    0 讨论(0)
  • 2021-01-19 04:43

    Look into "polling" on the touch input, instead of getting Input events. So, in your render or update method, use something like this:

     boolean activeTouch = false;
    
     if (Gdx.input.isTouched(0)) {
        if (activeTouch) {
           // continuing a touch ...
        } else {
           // starting a new touch ..
           activeTouch = true;
        }     
     } else {
        activeTouch = false;
     }
    
    0 讨论(0)
  • 2021-01-19 04:45

    I've succeeded to work this out, without pooling concept.

    I have custom InputProccesor, int I've used similar logic like P.T. mentioned. I touchDown I start thread that shoots bullets and do some calculation, because I access to some methods from Renderer class I've to use

       Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
            // process the result, e.g. add it to an Array<Result> field of the ApplicationListener.
            shootBullet(screenX, screenY);
            }
        });
    

    To avoid OpenGL context exception.

    In touchUp method I cancel shooting thread.

    Tnx for idea!

    0 讨论(0)
提交回复
热议问题