0
votes

In my LibGdx game,I want to create a pause button inside game screen.For this,I created a stage object like this.

Stage stage;
stage = new Stage(game.viewPort);

I wanted to include this statement:

Gdx.input.setInputProcessor(stage);

But another statement is already there;

Gdx.input.setInputProcessor(inputmultiplexer);

So when I add a pause button,It is showing up but listener for the button seems to be not working.

private void drawPauseButton() {

    pauseButton = new ImageButton(new TextureRegionDrawable(pauseTexture),
            new TextureRegionDrawable(pausePressTexture));
    stage.addActor(pauseButton);

    pauseButton.setPosition(Constants.WORLD_WIDTH,30, Align.bottomRight);

    pauseButton.addListener(new ActorGestureListener() {
        @Override
        public void tap(InputEvent event, float x, float y, int count, int button) {
            super.tap(event, x, y, count, button);
            game.setScreen(new PauseScreen(game));

            // dispose();
        }
    });
}

How can I resolve this issue?

Input multiplexer code:

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 isKeyDown=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 tap(float x, float y, int count, int button) {
    // TODO Auto-generated method stub

    return false;
}

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

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

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

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

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

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

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

}

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

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

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



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

@Override
public boolean scrolled(int amount) {
    // TODO Auto-generated method stub
    return false;
}


@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

     MyInputProcessor.isTouchDown=true;
     return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {

         MyInputProcessor.isTouchUp=true;
        return false;
}

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

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

Inside gamescreen class' show method:

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

1 Answers

1
votes

An InputProcessor that delegates to an ordered list of other InputProcessors. Delegation for an event stops if a processor returns true, which indicates that the event was handled.

Stage implements InputProcessor, you can create another implementation and put into Gdx.input

Stage stage=new Stage();               // your 1st InputProcessor
InputPorcessor processor=new InputPorcessor();      //2nd  InputProcessor
...                                                 // may be 3rd  

InputMultiplexer inputMultiplexer=new InputMultiplexer();
inputMultiplexer.addProcessor(stage);
inputMultiplexer.addProcessor(processor);

Gdx.input.setInputProcessor(inputMultiplexer);

EDIT

Add your stage into your multiplexer and return in this way :

public InputMultiplexer returnInput(Stage stage) {

     InputMultiplexer im = new InputMultiplexer();
     GestureDetector gd = new GestureDetector(this);
     im.addProcessor(stage);
     im.addProcessor(gd);
     im.addProcessor(this);

     return im;
}

Set return value:

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