2016-08-12 5 views
0

libgdxのGestureListenerを使用して「TouchUp」を検出する最も簡単な方法は何ですか? (以前のプロジェクトでは、InputListenerを使用していましたが、これにはTouchUpがありましたが、パンやフリングはありませんでした)。gesturelistenerを使用したlibgdx touchup

私が今必要とするのは、指が持ち上げられたら、私のMovingLeftまたはMovingRightブール値をFalseに設定することだけです。あなたは両方のGestureDetectorInputProcessorインタフェースを使用してGdx.input.setInputProcessorに挿入されているInputMultiplexerでそれらを置くことができる

package com.moneylife.stashinvaders; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.input.GestureDetector; 
import com.badlogic.gdx.math.Vector2; 

public class Player { 
    Vector2 position; 
    Texture texture; 
    int speed = 500; 
    float deltaTime; 
    int screenWidth, screenHeight; 
    boolean MovingRight = false, MovingLeft = false; 


    public Player(int ScreenW, int ScreenH){ 
     Gdx.input.setInputProcessor(new GestureDetector(new MyGestureDetector())); 
     texture = new Texture("bazookaman.png"); 
     position = new Vector2(Gdx.graphics.getBackBufferWidth()/2 - texture.getWidth()/2, 0); 
     screenWidth = ScreenW; 
     screenHeight = ScreenH; 
    } 

    public void update(){ 
     deltaTime = Gdx.graphics.getDeltaTime(); 
     if (MovingRight){ 
      position.x += speed * deltaTime; 
     } 
     if (MovingLeft){ 
      position.x -= speed * deltaTime; 
     } 
    } 

    public void draw(SpriteBatch spriteBatch){ 
     spriteBatch.draw(texture, position.x, position.y); 
    } 


    public class MyGestureDetector implements GestureDetector.GestureListener { 
     @Override 
     public boolean touchDown(float x, float y, int pointer, int button) { 
      if (x > position.x) { 
       MovingRight = true; 
      } 
      if (x < position.x){ 
       MovingLeft = true; 
      } 
      return false; 
     } 

     @Override 
     public boolean tap(float x, float y, int count, int button) { 
      return false; 
     } 

     @Override 
     public boolean longPress(float x, float y) { 
      return false; 
     } 

     @Override 
     public boolean fling(float velocityX, float velocityY, int button) { 
      return false; 
     } 

     @Override 
     public boolean pan(float x, float y, float deltaX, float deltaY) { 
      return false; 
     } 

     @Override 
     public boolean panStop(float x, float y, int pointer, int button) { 
      MovingLeft = false; 
      MovingRight = false; 
      return false; 
     } 

     @Override 
     public boolean zoom(float initialDistance, float distance) { 
      return false; 
     } 

     @Override 
     public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) { 
      return false; 
     } 

     @Override 
     public void pinchStop() { 

     } 

    } 
} 

答えて

0

これは、このプロジェクトの私の基本的なニーズにとってはるかに簡単な方法でした。それはむしろ、まったくGestureListener(またはinputprocessor)を使用するよりも、ポーリングと呼ばれています:

if (Gdx.input.isTouched()) { 
      if (Gdx.input.getX() > position.x) { 
       position.x += speed * deltaTime; 
      } 
      if (Gdx.input.getX() < position.x) { 
       position.x -= speed * deltaTime; 
      } 
     } 

作品とプレイヤーの動き(指があまりにも近いプレイヤーになった場合は除くが、私はそれだけで心配ので、ここではない実験だ)シームレスです

1

+0

よろしくお願いいたします。私はこれらについて見てきましたが、それが単にタッチアップを得るために必要かどうかは分かりませんでした。あなたが私に言っていることから、これは最善の選択ですので、今これについて研究します。もう一度ありがとう – Ronny

+0

@ロニーなぜこれらのジェスチャーが単一のインターフェースで結合されていないのかわかりませんが、実際には関係ありません。 – Madmenyo

+0

私は実際にGdx.input.isTouched()を探していました。誰かがIRCで親切に思い出させました。次に、getX()メソッドとgetY()メソッドを使用して入力をポーリングすることは、私の状況ではもっと簡単です。このプロジェクトでは不要なので、GestureListenerコード全体を削除できるようになりました。ちょうど同じ頭痛を苦しんでいる誰もがこれを読んでこれを追加すると思った – Ronny

関連する問題