实现图像按钮后相机不再工作 - Libgdx

我正在开发一款安卓游戏。我希望用户能够通过拖动手指向左和向右“滚动”。直到现在,这一切都很完美。现在,我正在尝试使用一些按钮实现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);


        }

    });

}

相机运动发生在“”东西中。我不认为我必须在这里展示它。


我现在正试图实现按钮两天,但我无法让它们工作。问题就在底部:@Override touchDragged


InputMultiplexer multiplexer = new InputMultiplexer(this, button);       

Gdx.input.setInputProcessor(multiplexer);

按照这个顺序,我可以移动相机,但我不能触摸按钮。如果我写,我不能再移动相机,但可以点击按钮。当我单击屏幕上的任意位置时,我还可以激活按钮的功能。


希望你能帮助我解决这个问题!(...)(button, this);


守着一只汪
浏览 98回答 1
1回答

慕雪6442864

根据 LibGDX Wiki on Event-Handling with InputMultiplexer:输入多路复用器会将任何新事件传递给添加到其中的第一个输入处理器。如果该处理器从调用来处理事件的方法返回 false,则表示事件未得到处理,多路复用器会将事件传递给链中的下一个处理器。通过这种机制,MyUiInputProcessor可以处理属于其小部件之一的任何事件,并将任何其他事件传递给MyGameInputProcessor。通过将 InputAdapters 的子类中已覆盖方法的返回值设置为 false,可以解决您的问题,下面是一个示例:@Overridepublic 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}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java