实施“图像按钮”后,相机不再起作用-Libgdx

时间:2019-03-16 21:45:43

标签: java android libgdx

我正在为Android开发游戏。我希望用户能够通过拖动手指左右滚动。到现在为止,效果一直很好。现在,我正在尝试通过一些按钮来实现Gui。

看起来应该像这样:
 https://i.stack.imgur.com/jf0uZ.png

代码:

public class MainGameScreen implements Screen, InputProcessor {
    Texture background, background_left, background_right;
    public SpriteBatch batch;
    //Graphical user interface
    private Stage GUIStage;
    private InputMultiplexer multiplexer;

//Camera
OrthographicCamera camera;
//Font
private BitmapFont font;
private String message = "Touch me";

//Buttons
private Stage button;
private Texture myTexture;
private TextureRegion myTextureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private ImageButton imageButton;

public MainGameScreen (Trench_Warfare game) {
    this.game = game;
    batch = new SpriteBatch();
    button = new Stage();

    //GUI - Graphical user interface
    GUIStage = new Stage(new ScreenViewport());

    Image gui_background = new Image(new Texture("gui/GUI.png"));
    gui_background.setPosition(0,0);
    gui_background.setTouchable(Touchable.disabled);
    GUIStage.addActor(gui_background);

    //Buttons
    myTexture = new Texture(Gdx.files.internal("gui/button/paper_riflemen.png"));
    myTextureRegion = new TextureRegion(myTexture);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    imageButton = new ImageButton(myTexRegionDrawable); //Set the button up
    button = new Stage(new ScreenViewport()); //Set up a stage for the ui;
    imageButton.isTouchable();
    imageButton.setBounds(0,500,194,200);
    button.addActor(imageButton); //Add the button to the stage to perform rendering and take input.

    //Font
    font = new BitmapFont();
    font.setColor(Color.BLACK);
    font.getData().scale(5);

    //Background
    background = new Texture("level1.png");
    background_left = new Texture("level1_seiten.png");
    background_right = new Texture("level1_seiten.png");

    //Camera
    camera = new OrthographicCamera(Gdx.graphics.getWidth()/*4000*/, Gdx.graphics.getHeight()/*2200*/);
    camera.update();
    camera.setToOrtho(false, Gdx.graphics.getWidth()*1.5f, Gdx.graphics.getHeight()*1.5f);

    button.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            message = "Touch";
            event.handle();//the Stage will stop trying to handle this event
        }
    });

    InputMultiplexer multiplexer = new InputMultiplexer(button, this);
    multiplexer.addProcessor(button);
    multiplexer.addProcessor(this);
    Gdx.input.setInputProcessor(multiplexer);

}

相机运动发生在“ @Override touchDragged”中。我认为我不必在这里展示它。

我现在尝试将按钮实施两天,但无法使它们正常工作。 问题就在底部:

InputMultiplexer multiplexer = new InputMultiplexer(this, button);       
Gdx.input.setInputProcessor(multiplexer);

按此顺序,我可以移动相机,但不能触摸按钮。如果我写(...)(button, this);,则无法再移动相机,但可以单击按钮。当我单击屏幕上的任意位置时,我也可以激活按钮的功能。

希望你能帮助我解决这个问题!

1 个答案:

答案 0 :(得分:0)

根据LibGDX Wiki上的Event-Handling with InputMultiplexer

  

InputMultiplexer会将所有新事件移交给添加到它的第一个InputProcessor。如果该处理器从调用的用于处理该事件的方法中返回false,则表明该事件未处理,并且多路复用器会将事件移交给链中的下一个处理器。通过这种机制,MyUiInputProcessor可以处理属于其小部件之一的任何事件,并将任何其他事件传递给MyGameInputProcessor。

可以通过将InputAdapters的子类中的重写方法的返回值设置为false来解决您的问题,这是一个示例:

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    [Some Logic]
    return false; // Input has not been processed, next processor in the multiplexer will try to handle this
}