简体   繁体   中英

Camera doesn't work anymore after implementing Image Button - Libgdx

I am working on an game for Android. I want the user to be able to "scroll" to the left and the right side by dragging the finger. That worked perfectly till now. Right now I am trying to implement the Gui with some buttons.

It should look kinda like this:
https://i.stack.imgur.com/jf0uZ.png

Code:

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

}

The Camera movement happens in the " @Override touchDragged " stuff. I don't think I have to show it here.

I am now trying to implement the buttons for two days now but I can't get them to work. The problem is right at the bottom:

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

In this order I can move the camera around but I cant touch the button. If I write (...)(button, this); I can't move the camera around anymore but can click the button. I can also activate the button's function when I click anywhere on the screen.

Hopefully you can help me with this problem!

According to the LibGDX Wiki on Event-Handling with InputMultiplexer :

The InputMultiplexer will hand any new events to the first InputProcessor that was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, the MyUiInputProcessor can handle any events that fall inside one of its widgets and pass on any other events to the MyGameInputProcessor.

Your problem can be solved by setting the returning value of your overriden methods in your InputAdapters' children classes to false, here's an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM