libgdx 使用 gesturelistener 进行修饰

libgdx touchup using gesturelistener

使用 libgdx 中的 GestureListener 检测 "TouchUp" 的 best/easiest 方法是什么? (在以前的项目中,我使用了 InputListener,它有 TouchUp 但没有平移或滑动)。

我现在需要做的就是在手指抬起后将我的 MovingLeft 或 MovingRight 布尔值设置为 False。

package com.moneylife.stashinvaders;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;

public class Player {
    Vector2 position;
    Texture texture;
    int speed = 500;
    float deltaTime;
    int screenWidth, screenHeight;
    boolean MovingRight = false, MovingLeft = false;


    public Player(int ScreenW, int ScreenH){
        Gdx.input.setInputProcessor(new GestureDetector(new MyGestureDetector()));
        texture = new Texture("bazookaman.png");
        position = new Vector2(Gdx.graphics.getBackBufferWidth() / 2 - texture.getWidth() / 2, 0);
        screenWidth = ScreenW;
        screenHeight = ScreenH;
    }

    public void update(){
        deltaTime = Gdx.graphics.getDeltaTime();
        if (MovingRight){
            position.x += speed * deltaTime;
        }
        if (MovingLeft){
            position.x -= speed * deltaTime;
        }
    }

    public void draw(SpriteBatch spriteBatch){
        spriteBatch.draw(texture, position.x, position.y);
    }


    public class MyGestureDetector implements GestureDetector.GestureListener {
        @Override
        public boolean touchDown(float x, float y, int pointer, int button) {
            if (x > position.x) {
                MovingRight = true;
            }
            if (x < position.x){
                MovingLeft = true;
            }
            return false;
        }

        @Override
        public boolean tap(float x, float y, int count, int button) {
            return false;
        }

        @Override
        public boolean longPress(float x, float y) {
            return false;
        }

        @Override
        public boolean fling(float velocityX, float velocityY, int button) {
            return false;
        }

        @Override
        public boolean pan(float x, float y, float deltaX, float deltaY) {
            return false;
        }

        @Override
        public boolean panStop(float x, float y, int pointer, int button) {
            MovingLeft = false;
            MovingRight = false;
            return false;
        }

        @Override
        public boolean zoom(float initialDistance, float distance) {
            return false;
        }

        @Override
        public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
            return false;
        }

        @Override
        public void pinchStop() {

        }

    }
}

您可以同时使用 GestureDetectorInputProcessor 接口并将它们放在一个 InputMultiplexer 中,该 InputMultiplexer 被插入到 Gdx.input.setInputProcessor.

对于我在这个项目中的基本需求,这是一种更简单的方法。它被称为轮询,而不是完全使用 GestureListener(或输入处理器):

if (Gdx.input.isTouched()) {
            if (Gdx.input.getX() > position.x) {
                position.x += speed * deltaTime;
            }
            if (Gdx.input.getX() < position.x) {
                position.x -= speed * deltaTime;
            }
        }

作品和播放器移动是无缝的(不包括手指是否离播放器太近,但我只是在这里试验所以不担心)