簡體   English   中英

Libgdx加速度計不起作用?

[英]Libgdx Accelerometer not working?

由於某種原因,當我傾斜屏幕時,該塊沒有移動,並且我不知道出了什么問題。 為了澄清起見,我將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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM