Input 使用输入处理器触地事件检测对象触碰

Input 使用输入处理器触地事件检测对象触碰,input,libgdx,touch,Input,Libgdx,Touch,我想在一个触摸硬币的物体上收集硬币。所以我喜欢这个 if (Gdx.input.justTouched()) { touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); game.camera.unproject(touchPos); for (int i = 0; i < coins.length; i++) { Rectangle textureBounds =

我想在一个触摸硬币的物体上收集硬币。所以我喜欢这个

if (Gdx.input.justTouched()) {
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         game.camera.unproject(touchPos);
        for (int i = 0; i < coins.length; i++) {
            Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());

if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
        //after touch something happens
            }
        }
}
    }
public class MyInputProcessor implements InputProcessor,GestureListener {


public static boolean isTouchDown=false;
public static boolean isTouchUp=false;
public static boolean isTap=false;
public static boolean isLongPress=false;
public static boolean isFling=false;
public static boolean isSwipeDown=false;
public static boolean isSwipeUp=false;
public static boolean isSwipeLeft=false;
public static boolean isSwipeRight=false;

public static boolean isZoomed=false;
public static float zoomInitDist=0;
public static float zoomDist=0;


public MyInputProcessor() {
    // TODO Auto-generated constructor stub
     System.out.println("My Input Processor Created..");

}

public InputMultiplexer returnInput() {
    // TODO Auto-generated method stub
      InputMultiplexer im = new InputMultiplexer();
      GestureDetector gd = new GestureDetector(this);
      im.addProcessor(gd);
      im.addProcessor(this);

    return im;
}

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
    // TODO Auto-generated method stub
    this.isTouchDown=true;
    return true;
}

@Override
public boolean tap(float x, float y, int count, int button) {
    // TODO Auto-generated method stub
    this.isTap=true;
    return true;
}

@Override
public boolean longPress(float x, float y) {
    // TODO Auto-generated method stub
    this.isLongPress=true;
    return true;
}

@Override
public boolean fling(float velocityX, float velocityY, int button) {
    // TODO Auto-generated method stub
    if(Math.abs(velocityX)>Math.abs(velocityY)){
        if(velocityX>0){
                this.isSwipeRight=true;
        }else{
            this.isSwipeLeft=true;
        }
    }else{
        if(velocityY>0){
             this.isSwipeDown=true;
        }else{                                  
              this.isSwipeUp=true;
        }
    }
    return true;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean zoom(float initialDistance, float distance) {
    // TODO Auto-generated method stub
    this.isZoomed=true;
    this.zoomInitDist=initialDistance;
    this.zoomDist=distance;
    return true;
}

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public void pinchStop() {
    // TODO Auto-generated method stub

}

@Override
public boolean keyDown(int keycode) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean keyUp(int keycode) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean keyTyped(char character) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    // TODO Auto-generated method stub

    return true;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean scrolled(int amount) {
    // TODO Auto-generated method stub
    return true;
}
public class MyInputProcessor implements InputProcessor {

    Vector3 touchPos;
    GameScreen gameScreen;

    public static InputMultiplexer getMux(GameScreen gameScreen){
        InputMultiplexer im = new InputMultiplexer();

        im.addProcessor(gameScreen.stage);
        im.addProcessor(new MyInputProcessor(gameScreen));

       return im;
    }        

    public MyInputProcessor(GameScreen gameScreen){

        touchPos=new Vector3();
        this.gameScreen=gameScreen;            // keep reference to access data member of GameScreen

    }

    @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) {
       touchPos.set(screenX,screenY,0);   
       gameScreen.game.camera.unproject(touchPos);

        Coin coins[]=gamescreen.coins;

        for (int i = 0; i < coins.length; i++) {
            Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());

            if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
                //after touch something happens
            }
        }

        return false;
    }

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

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

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}
}

在gameScreen类中

MyInputProcessor myInputProcessor = new MyInputProcessor();
    InputMultiplexer im = myInputProcessor.returnInput(stage);
    Gdx.input.setInputProcessor(im);

但我不知道如何在输入处理器触地事件中获得这个触碰位置。

您的
MyInputProcessor
应该是这样的

if (Gdx.input.justTouched()) {
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         game.camera.unproject(touchPos);
        for (int i = 0; i < coins.length; i++) {
            Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());

if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
        //after touch something happens
            }
        }
}
    }
public class MyInputProcessor implements InputProcessor,GestureListener {


public static boolean isTouchDown=false;
public static boolean isTouchUp=false;
public static boolean isTap=false;
public static boolean isLongPress=false;
public static boolean isFling=false;
public static boolean isSwipeDown=false;
public static boolean isSwipeUp=false;
public static boolean isSwipeLeft=false;
public static boolean isSwipeRight=false;

public static boolean isZoomed=false;
public static float zoomInitDist=0;
public static float zoomDist=0;


public MyInputProcessor() {
    // TODO Auto-generated constructor stub
     System.out.println("My Input Processor Created..");

}

public InputMultiplexer returnInput() {
    // TODO Auto-generated method stub
      InputMultiplexer im = new InputMultiplexer();
      GestureDetector gd = new GestureDetector(this);
      im.addProcessor(gd);
      im.addProcessor(this);

    return im;
}

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
    // TODO Auto-generated method stub
    this.isTouchDown=true;
    return true;
}

@Override
public boolean tap(float x, float y, int count, int button) {
    // TODO Auto-generated method stub
    this.isTap=true;
    return true;
}

@Override
public boolean longPress(float x, float y) {
    // TODO Auto-generated method stub
    this.isLongPress=true;
    return true;
}

@Override
public boolean fling(float velocityX, float velocityY, int button) {
    // TODO Auto-generated method stub
    if(Math.abs(velocityX)>Math.abs(velocityY)){
        if(velocityX>0){
                this.isSwipeRight=true;
        }else{
            this.isSwipeLeft=true;
        }
    }else{
        if(velocityY>0){
             this.isSwipeDown=true;
        }else{                                  
              this.isSwipeUp=true;
        }
    }
    return true;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean zoom(float initialDistance, float distance) {
    // TODO Auto-generated method stub
    this.isZoomed=true;
    this.zoomInitDist=initialDistance;
    this.zoomDist=distance;
    return true;
}

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public void pinchStop() {
    // TODO Auto-generated method stub

}

@Override
public boolean keyDown(int keycode) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean keyUp(int keycode) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean keyTyped(char character) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    // TODO Auto-generated method stub

    return true;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean scrolled(int amount) {
    // TODO Auto-generated method stub
    return true;
}
public class MyInputProcessor implements InputProcessor {

    Vector3 touchPos;
    GameScreen gameScreen;

    public static InputMultiplexer getMux(GameScreen gameScreen){
        InputMultiplexer im = new InputMultiplexer();

        im.addProcessor(gameScreen.stage);
        im.addProcessor(new MyInputProcessor(gameScreen));

       return im;
    }        

    public MyInputProcessor(GameScreen gameScreen){

        touchPos=new Vector3();
        this.gameScreen=gameScreen;            // keep reference to access data member of GameScreen

    }

    @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) {
       touchPos.set(screenX,screenY,0);   
       gameScreen.game.camera.unproject(touchPos);

        Coin coins[]=gamescreen.coins;

        for (int i = 0; i < coins.length; i++) {
            Rectangle textureBounds = new Rectangle(coins[i].getX(), coins[i].getY(), coins[i].getWidth(),coins[i].getHeight());

            if (textureBounds.contains(touchPos.x, touchPos.y) && !coins[i].isBreakBool() && coins[i].isCoinVisible()) {
                //after touch something happens
            }
        }

        return false;
    }

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

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

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

为什么
GestureDetector
,你在使用它?不。我目前没有使用它。包含了有问题的完整类。@Abhishekaryan如果(Gdx.input.justtouch()){}编写代码时,类的名称是什么?它在gameScreen类中,我把所有的游戏逻辑放在那里。在更新(float delta)中调用justtouch().code已编辑。@Abhishekaryan谢谢您的帮助!它工作起来没有什么变化。没有包括Gdx.input.setInputProcessor(新的MyInputProcessor(这个))并且只在游戏屏幕上保留了硬币相关的东西。如果你想检测舞台上的触摸,你需要将舞台添加为inputprocessor,我已经编辑了我的答案,请检查