Libgdx获得触摸事件的压力

时间:2016-09-09 13:58:28

标签: java android libgdx

我正在开发一款关于android的压力敏感游戏。 (或者换句话说游戏结束) 但我知道我想开始使用游戏引擎,我决定使用Libgdx。 但是,GestureDetector.GestureListener和InputProcessor接口不提供android内部MotionEvent的压力参数。 我试图手动将游戏设置为View.OnTouchListener,但这会干扰线程模型......

有关解决方案的任何建议吗?

2 个答案:

答案 0 :(得分:2)

到目前为止我只知道一个选项,即来自getPressure()课程的MotionEvent。该函数将在float中为您提供0到1之间的值。docs

注意:没有压力传感器的设备通过触摸点的大小模拟压力。

压力越大意味着手指覆盖的屏幕区域越多

setOnTouchListener可以为您提供MotionEvent对象,您可以从中获取压力值。

someView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float pressure = event.getPressure();
    }
});

答案 1 :(得分:2)

以下是我向游戏对象添加压力的代码的一部分

final MyGdxGame game = new MyGdxGame();

    View view = initializeForView(game, config);
    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            getInput().onTouch(v, event);
            game.setPressure(event.getPressure());
            Log.v("log", "hooked " + event.getPressure());
            return true;
        }
    });

诀窍是我必须致电initializeForView才能覆盖onTouchListener并将MotionEvent转发给Libgdx的输入处理程序。

相关问题