Libgdx加速度计不起作用

Libgdx加速度计不起作用

由于某种原因,当我倾斜屏幕时,该块没有移动,并且我不知道出了什么问题。为了澄清起见,我将cfg.accelerometer设置为true,但是将cfg.compass设置为false。这是源代码-

public void update()
{

    x += velX;
    y += velY;

    //movement

    //left
    if (Gdx.input.isKeyPressed(Keys.LEFT))
    {
        velX = -speed;
    }

    //right
    if (Gdx.input.isKeyPressed(Keys.RIGHT))
    {
        velX = speed;
    }

    //up
    if (Gdx.input.isKeyPressed(Keys.UP))
    {
        velY = -speed;
    }

    //down
    if (Gdx.input.isKeyPressed(Keys.DOWN))
    {
        velY = speed;
    }

    if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer))
    {
        velX = Gdx.input.getAccelerometerX();
        velY = Gdx.input.getAccelerometerY();
    }

    //stop
    if (!Gdx.input.isKeyPressed(Keys.LEFT) && !Gdx.input.isKeyPressed(Keys.RIGHT))
    {
        velX = 0;
    }

    if (!Gdx.input.isKeyPressed(Keys.UP) && !Gdx.input.isKeyPressed(Keys.DOWN))
    {
        velY = 0;
    }

    //collision with edges of screen
    if (x <= 0)
    {
        x = 0;
    }

    if (x >= 1920 - width)
    {
        x = 1920 - width;
    }

    if (y <= 0)
    {
        y = 0;
    }

    if (y >= 1080 - height)
    {
        y = 1080 - height;
    }

    long recoveryElapsed = (System.nanoTime() - recoveryTimer)/1000000;
    if (recoveryElapsed > 2000)
    {
        recovering = false;
        recoveryTimer = 0;
    }

    System.out.println(lives+ " lives, recovering, "+recovering);

}


帮助将不胜感激,谢谢。我没有找到显示示例的教程,因此我真的不知道自己在做什么,但我看不出有什么问题。

最佳答案

您正在设置Accel值,然后如果未按这些键,则将它们再次设置为0。像这样做:

//stop
if (!Gdx.input.isKeyPressed(Keys.LEFT) && !Gdx.input.isKeyPressed(Keys.RIGHT))
{
    velX = 0;
}

if (!Gdx.input.isKeyPressed(Keys.UP) && !Gdx.input.isKeyPressed(Keys.DOWN))
{
    velY = 0;
}

if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer))
{
    velX = Gdx.input.getAccelerometerX();
    velY = Gdx.input.getAccelerometerY();
}

关于java - Libgdx加速度计不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21819536/

10-09 09:25