Libgdx GestureDirector平移在处理多个手指时表现异常

时间:2019-06-21 11:13:09

标签: libgdx pan

我正在创建一个游戏,其运动机制应按以下方式工作:点击/按住屏幕右侧,以朝着您所面对的方向加速,然后在屏幕左侧向左或向右滑动屏幕旋转。它的效果足够好,直到玩家尝试用第二根手指旋转同时用第一根手指加速时为止。 pan方法似乎仍然可以用第二根手指运行,但很少出现,如果不触发语句,则gdx.input.getX(i)也会运行。我正在使用GestureDetector类的pan方法。这是一个视频:https://www.youtube.com/watch?v=FmHI81ByPmU&feature=youtu.be

我看了一个类似的问题:libgdx multiple pan events,但答案对我不起作用,将pan设置为false则无济于事,并且控件与按钮无关,因此无法将方法更改为触地得分

pan方法本身:

@Override
        public boolean pan(float x, float y, float deltaX, float deltaY) {

            for(int i = 0 ; i < 2 ; i++){//In case they drag wit second finger
                if(Gdx.input.isTouched(i) && Gdx.input.getX(i) < Gdx.graphics.getWidth() / 2){
                    if(Gdx.input.getDeltaX(i) < - 3){
                        directionListener.onRight();
                        System.out.println(">");
                    }
                    if(Gdx.input.getDeltaX(i) > 3) {
                        directionListener.onLeft();
                        System.out.println("<");
                    }
                    finger = i;//used to stop body from rotating when the finger rotating it is lifted

                    System.out.println(i + " = " + Gdx.input.getX(i));
                }
            }

//Can't replace with touchdown because it does not override method from superclass

            return super.pan(x, y, deltaX, deltaY);
        }

整个班级:

package com.doppelganger.spacesoccer.Helpers;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.input.GestureDetector;

public class SimpleDirectionGestureDetector extends GestureDetector{

    public static int finger = 10;

    public interface DirectionListener {
        void onLeft();

        void onRight();
    }

    public SimpleDirectionGestureDetector(DirectionListener directionListener) {
        super(new DirectionGestureListener(directionListener));
    }

    private static class DirectionGestureListener extends GestureDetector.GestureAdapter {
        DirectionListener directionListener;

        DirectionGestureListener(DirectionListener directionListener){
            this.directionListener = directionListener;
        }


        @Override
        public boolean pan(float x, float y, float deltaX, float deltaY) {

            for(int i = 0 ; i < 2 ; i++){//In case they drag with second finger
                if(Gdx.input.isTouched(i) && Gdx.input.getX(i) < Gdx.graphics.getWidth() / 2){
                    if(Gdx.input.getDeltaX(i) < - 3){
                        directionListener.onRight();
                        System.out.println(">");
                    }
                    if(Gdx.input.getDeltaX(i) > 3) {
                        directionListener.onLeft();
                        System.out.println("<");
                    }
                    finger = i;//used to stop body from rotating when the finger rotating it is lifted

                    System.out.println(i + " = " + Gdx.input.getX(i));
                }
            }
//Can't replace with touchdown because it does not override method from superclass
            return super.pan(x, y, deltaX, deltaY);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我看到了问题,您在触摸屏幕右侧时会忽略输入。这是一行:

if(Gdx.input.isTouched(i) && Gdx.input.getX(i) < Gdx.graphics.getWidth() / 2){

您可以在另一个if语句中验证Gdx.input.getX(i) < Gdx.graphics.getWidth() / 2并添加else语句以检查右侧,如下所示:

if(Gdx.input.isTouched(i)) {
    if (Gdx.input.getX(i) < Gdx.graphics.getWidth() / 2) {
        // POINTER ON LEFT SIDE - SAME CODE
            if(Gdx.input.getDeltaX(i) < - 3) {
                directionListener.onRight();
                System.out.println(">");
            }
            if(Gdx.input.getDeltaX(i) > 3) {
                directionListener.onLeft();
                System.out.println("<");
            }
            finger = i; //used to stop body from rotating when the finger rotating it is lifted
            System.out.println(i + " = " + Gdx.input.getX(i));

    } else {
        // POINTER ON RIGHT SIDE - NEW CODE
        System.out.println("Right side");
    }
}