如何使用Libgdx手势检测单向滚动

时间:2015-02-19 21:43:24

标签: java android libgdx gesturedetector

我设法做了一个简单的游戏,其中主要元素是向上滚动背景。但是,我无法弄清楚如何使应用程序仅单向滚动。背景不应该向下滚动。 WorldScreen.camera.y是通过背景滚动的相机。

以下是我的代码:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;
import com.vahlaville.game.screens.WorldScreen;

public class GestureListenerC implements GestureListener{

public static float velX, velY;
public static boolean flinging = false;
float initialScale = 1;

public boolean touchDown (float x, float y, int pointer, int button) {
    flinging = false;
    initialScale = WorldScreen.camera.zoom;
    return false;
}

@Override
public boolean tap (float x, float y, int count, int button) {
    //Gdx.app.log("GestureDetectorTest", "tap at " + x + ", " + y + ", count: " + count);
    return false;
}

@Override
public boolean longPress (float x, float y) {
    //Gdx.app.log("GestureDetectorTest", "long press at " + x + ", " + y);
    return false;
}

@Override
public boolean fling (float velocityX, float velocityY, int button) {
    //Gdx.app.log("GestureDetectorTest", "fling " + velocityX + ", " + velocityY);
    flinging = true;
    velX = WorldScreen.camera.zoom * velocityX * 0.5f;
    velY = WorldScreen.camera.zoom * velocityY * 0.5f;
    if(WorldScreen.value <= 0)WorldScreen.value = 0;
    return false;
}

@Override
public boolean pan (float x, float y, float deltaX, float deltaY) {
    // Gdx.app.log("GestureDetectorTest", "pan at " + x + ", " + y);
        if(!WorldScreen.ending){
    if(WorldScreen.value != 0){
        WorldScreen.camera.position.add(0, Math.abs(deltaY * WorldScreen.camera.zoom), 0);
        WorldScreen.value -= Math.abs(1 * deltaY);
    }
    if(WorldScreen.camera.position.y <= 0) WorldScreen.camera.position.y = 0;
        }
    if(WorldScreen.value <= 0)WorldScreen.value = 0;
    return false;
}

@Override
public boolean panStop (float x, float y, int pointer, int button) {
    //Gdx.app.log("GestureDetectorTest", "pan stop at " + x + ", " + y);
    return false;
}

@Override
public boolean zoom (float originalDistance, float currentDistance) {

    return false;
}

@Override
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer) {
    return false;
}

public void update () {
        if(!WorldScreen.ending){
    if (flinging) {

        if(WorldScreen.value != 0){
            velY *= 0.98f;
            WorldScreen.value -= 1.5f * velY;
            WorldScreen.camera.position.add(0, velY * Gdx.graphics.getDeltaTime(), 0);
            if (Math.abs(velY) < 0.01f) velY = 0;
        }
        if(WorldScreen.camera.position.y <= 480){ WorldScreen.camera.position.y = 480; WorldScreen.value = 384400000;}
    }
        }
    if(WorldScreen.value <= 0)WorldScreen.value = 0;
}

}

0 个答案:

没有答案