单击按钮时屏幕响应 [英] Screen responds when button is clicked

查看:75
本文介绍了单击按钮时屏幕响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个屏幕,其中有一个字符和一个按钮.当我触摸屏幕时,角色会跳跃(使用Gdx.input.justTouched实现).单击该按钮时会打开PauseMenu(按钮是舞台的演员,并且单击是使用button.addListener(new ChangeListener(){....})实现的.我的InputProcessor设置在舞台上

Basically, I have a screen where I have a character and a button. Character jumps when I touch the screen (implemented using Gdx.input.justTouched). The button opens PauseMenu when clicked (button is an actor of a stage and click is implemented using button.addListener(new ChangeListener() {.... }). My InputProcessor is set on stage

Gdx.input.setInputProcessor(stage);

问题是当单击按钮时,我的角色跳了起来(但他不应该跳),并且暂停菜单打开了.我正在通过InputMultiplexer搜索,但是没有用(或者我用了错误的方式).

The problem is when the button is clicked my character jumps (but he shouldn't) and the pause menu opens. I was searching through InputMultiplexer but that didn't work (or maybe I used it in a wrong way).

感谢您的任何建议!

推荐答案

您可以使用InputMultiplexer.

首先InputMultiplexer的工作方式.
InputMultiplexer中,您可以执行许多InputProcessor,而InputProcessor具有返回布尔值的方法.
此布尔值表示是否处理了事件.
因此,如果该方法返回false,则InputMultiplexer将该事件提供给下一个InputProcessor.
如果该方法返回true,则说明该事件已处理,并且该事件不会转到下一个InputProcessor.

First how the InputMultiplexer works.
In the InputMultiplexer you can do many InputProcessors and an InputProcessor has methods which return boolean.
This boolean value says whether the event was handled or not.
So if the method returns false, the InputMultiplexer give the event to the next InputProcessor.
If the method returns true the event was handled and the event won't go to the next InputProcessor.

现在,在这种情况下,我们为屏幕制作InputProcessor:TestScreenInput:

Now we make an InputProcessor for our screen in this case TestScreenInput:

public class TestScreenInput implements InputProcessor {

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        System.out.println("Touch down");
        character.jump();
        return true;
    }

    @Override
    ... //all other methods from InputProcessor
}

touchDown方法返回true,因此下一个InputProcessor不会成为touchDown事件

The touchDown method returns true, so the next InputProcessor won't become the touchDown event

在屏幕类(TestScreen)中,我们创建Stage:

In our screen class (TestScreen) we create our Stage:

public class TestScreen implements Screen {
    private Stage stage;

    @Override
    public void show() {
        stage = new Stage();
    }
}

现在,我们将使用ChangeListener创建TextButton.
问题是,我们怎样才能说该事件已得到处理?
ChangeListener::changed(ChangeEvent event, Actor actor)不返回布尔值.

Now we will create our TextButton with a ChangeListener.
The problem is, how we can say that the event is handled?
The ChangeListener::changed(ChangeEvent event, Actor actor) doesn't return a boolean.

当我们查看Stage类时,我们可以找到touchDown方法,该方法返回:

When we look into the Stage class we can find the touchDown method and the method returns:

boolean handled = event.isHandled();
return handled;

eventEvent的类型,并且ChangeEvent扩展Event
在我们的changed(ChangeEvent event, Actor actor)方法中,我们有一个ChangeEvent.因此,我们要做的就是设置此事件的处理方式.
Event中有一种方法:

event is a Type of Event and ChangeEvent extends Event
In our changed(ChangeEvent event, Actor actor) method we have a ChangeEvent. So all we have to do is to set this event handled.
In Event there is a method:

public void handle () { handled = true;}

现在我们知道创建按钮的一切:

Now we know all to create our Button:

TextButton button = new TextButton("Click", skin);
button.addListener(new ChangeListener() {
    @Override
    public void changed(ChangeEvent event, Actor actor) {
        System.out.println("Click Button");
        event.handle(); //set the event handled = true
    }
});

stage.addActor(button);

最后,我们创建我们的InputMultiplexer.重要的是,该阶段早于我们的TestScreenInput,因为TestScreenInput会将touchDown标记为已处理,而stage永远不会收到它们.

Finally, we create our InputMultiplexer. Important is that the stage come before our TestScreenInput because the TestScreenInput will mark the touchDown as handled and the stage never will receive them.

InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(stage);
multiplexer.addProcessor(new TestScreenInput());
Gdx.input.setInputProcessor(multiplexer);

这篇关于单击按钮时屏幕响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆