场景2D如何处理被触摸的演员?(LibGDX)

5

我在使用libgdx的scene2d时遇到了问题。我找不到任何方法来检查演员是否被触摸。我只能找到一些方法,告诉我演员是否被触摸或释放。在我的游戏中,当演员被按下并保持时,应该每帧执行一些操作,而不仅仅是在我触摸它的那一刻执行一次。当我松开手指时,我想停止这些操作。


也许这可以帮助您:http://stackoverflow.com/questions/32818163/why-doesnt-my-approach-work-to-continuously-repeat-an-action-till-a-libgdx-scen/32942028#32942028 - Sunil Kumar
3个回答

4

您可以在您的InputListener中跟踪此信息。创建一个布尔型字段isTouched,当您得到touchDown时设置为true,当您得到touchUp时设置为false。我在我的俯视射击游戏中使用了这种方法,它效果非常好。


感谢您的回复!我认为那是个好主意,但是当您触摸火按钮并将手指移出按钮时,您的角色仍然会射击。我使用进入和退出而不是 touchDown 和 touchUp,并且这样运作得很好。 - Pawel

3
你可以在渲染方法中这样做来检查你的输入。
gdx.app.log("","touched"+touchdown);

首先设置输入处理器...

Gdx.input.setInputProcessor(mystage);

那么您可以在create方法中为您的演员添加输入监听器。
optone.addListener(new InputListener() {
        @Override
        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
                boolean touchdown=true;
            //do your stuff 
           //it will work when finger is released..

        }

        public boolean touchDown(InputEvent event, float x, float y,
               int pointer, int button) {
               boolean touchdown=false;
            //do your stuff it will work when u touched your actor
            return true;
        }

    });

0

我曾经遇到过类似的问题,而且我还需要知道当前坐标;所以我解决了这个问题,就像这样:

首先,我们扩展了标准监听器:

class MyClickListener extends ClickListener {
    float x, y = 0;

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        this.x = x;
        this.y = y;
        return super.touchDown(event, x, y, pointer, button);
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointer) {
        this.x = x;
        this.y = y;
        super.touchDragged(event, x, y, pointer);
    }
}

然后将一个实例添加到Actor中:

class MyActor extends Actor {
    private final MyClickListener listener = new MyClickListener();
    MyActor() {
        addListener(listener);
    }
    ...
}

在绘制(操作)方法中使用以下代码:
if (listener.getPressedButton() >= 0)
    System.out.println(listener.x + "; " + listener.y);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接