Search code examples
inputlibgdxtouch

Using input processor touchdown event to detect an object touch


I want to collect coin on a touching the coin object.So i did like this.

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

I increments the score and everything is proper except the touch.I want to make the coin invisible/moving immediately after touching down.This is not happening with justTouched().

So I want to use input processor touch down event for this. I have my input processor class with touch down event like this.

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

}

Inside gameScreen class,

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

But I am confused of how to get this touch position on touchdown event of input processor.


Solution

  • Your MyInputProcessor should be like this.

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

    Set it as InputProcessor inside your GameScreen class.

    Gdx.input.setInputProcessor(MyInputProcessor.getMux(this));