Libgdx -stage游戏内幕与输入处理器冲突

时间:2017-03-07 08:29:40

标签: user-interface libgdx

在我的LibGdx游戏中,我想在游戏画面中创建一个暂停按钮。为此,我创建了一个这样的舞台对象。

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

我想包括这句话:

Gdx.input.setInputProcessor(stage);

但另一个声明已经存在;

Gdx.input.setInputProcessor(inputmultiplexer);

因此,当我添加一个暂停按钮时,它会显示,但该按钮的监听器似乎无效。

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

如何解决此问题?

输入多路复用器代码:

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

内幕游戏课程' show method:

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

1 个答案:

答案 0 :(得分:1)

一个InputProcessor,它委托给其他InputProcessors的有序列表。如果处理器返回true,则事件的委派停止,这表示事件已被处理。

Stage实现了InputProcessor,您可以创建另一个实现并放入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);

修改

将您的舞台添加到多路复用器并以这种方式返回:

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

设置返回值:

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