libgdx多个平移事件 [英] libgdx multiple pan events

查看:64
本文介绍了libgdx多个平移事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,用户应该用一个手指在屏幕左侧移动玩家,并能够用右手指射击.问题在于,射击时玩家无法移动.我发现了一些相关的问题,但没有一个帮助我.我尝试使用2个不同的GestureListener类.其中一个检查是否触摸了屏幕的右侧,而另一个检查了左侧,仍然不起作用.

In my game the user is supposed to move the player with one finger on the left side of the screen and is able to shoot with the right finger. The problem is that when the shooting, the player can't move anymore. I've found some related questions but none helped me. I tried using 2 different GestureListener classes. One of them checks if the right side of the screen is touched and the other checks the left side, still doesn't work.

这是第一类的平移方法

    public boolean pan(float x, float y, float deltaX, float deltaY) {
        lastTouch = new Vector2(x, y).sub(deltaX, deltaY);
        Vector2 newTouch = new Vector2(x, y);

        Vector2 delta = newTouch.cpy().sub(lastTouch);
        if(lastTouch.x < Gdx.graphics.getWidth()/2 && newTouch.x < Gdx.graphics.getWidth()/2) {
            stage.getPlayer().move(new Vector2(delta.x, -delta.y));
        }
        lastTouch = newTouch;
        return true;
    }

这是第二类的方法

    public boolean pan(float x, float y, float deltaX, float deltaY) {
        lastTouch = new Vector2(x, y).sub(deltaX, deltaY);
        Vector2 newTouch = new Vector2(x, y);

        Vector2 delta = newTouch.cpy().sub(lastTouch);
        if(lastTouch.x > Gdx.graphics.getWidth()/2 && newTouch.x > Gdx.graphics.getWidth()/2){
            if(shoot) {
                bullets.add(new Bullet(WorldUtils.createBullet(stage.getWorld(), stage.getPlayer().getBody().getPosition().x+70, stage.getPlayer().getBody().getPosition().y, new Vector2(delta.x, -delta.y)), new Vector2(delta.x, -delta.y)));
                shoot = false;
            }
            stage.getPlayer().turn(new Vector2(delta.x, -delta.y));
        }
        lastTouch = newTouch;
        return true;
    }

谢谢

推荐答案

LibGDX中的侦听器倾向于使用布尔值返回值来声明输入(如果已完成)的处理.当您返回true时,侦听器将停止处理进一步的操作.

The Listeners in LibGDX tend to use boolean return values to state weather that the input if finished being processed. As you return true the listener stops processing further actions.

GestureListener可以通过从其方法中分别返回true或false来发出是否消耗事件或希望将其传递给下一个InputProcessor的信号.

The GestureListener can signal whether it consumed the event or wants it to be passed on to the next InputProcessor by returning either true or false respectively from its methods.

尝试将return方法更改为false,看看是否可以解决问题,否则请尝试将其转换为使用具有用于手指计数的参数指针的touchDown和touchUp方法

Try changing the return method to false and see if that fixes the issue Otherwise try convert it to use the touchDown and touchUp method that has a parameter pointer for the finger count

这篇关于libgdx多个平移事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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