Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LibGDX加速计-Android_Java_Android_Eclipse_Libgdx_Accelerometer - Fatal编程技术网

Java LibGDX加速计-Android

Java LibGDX加速计-Android,java,android,eclipse,libgdx,accelerometer,Java,Android,Eclipse,Libgdx,Accelerometer,我编写了左右移动玩家的代码,当玩家向左移动时,它有一个动画,当他向右移动时,它有另一个动画 这是代码(dt是deltaTime): 所以我也想用加速计移动播放器(在移动设备上)。我做到了,但现在我不知道如何检查玩家是否向左或向右移动,以给他不同的动画 我的代码: // Moving player android // position.x -= Gdx.input.getAccelerometerX() * speed * dt; if( PLAYER MOVING L

我编写了左右移动玩家的代码,当玩家向左移动时,它有一个动画,当他向右移动时,它有另一个动画

这是代码(dt是deltaTime):

所以我也想用加速计移动播放器(在移动设备上)。我做到了,但现在我不知道如何检查玩家是否向左或向右移动,以给他不同的动画

我的代码:

    // Moving player android
    // position.x -= Gdx.input.getAccelerometerX() * speed * dt;

    if( PLAYER MOVING LEFT)
    {
        // Player moving left....
        currentFrame =  animation.getKeyFrame(4 + (int) stateTime);
    }
    else if (PLAYER MOVING RIGHT)
    {
        // Player moving right....
        currentFrame =  animation.getKeyFrame(8 + (int) stateTime);
    }
    else
        currentFrame = animation.getKeyFrame(12);

这取决于你的比赛方向

position.x -= Gdx.input.getAccelerometerX() * speed * dt;
这个实现看起来不错,因为getAccelerometerX值返回[-10,10]的值

但是,如果你的手机放在桌子上,比方说,这个值不会正好是0。 假设你想在加速度大于0.3f时移动你的播放器

 float acceleration=Gdx.input.getAccelerometerX(); // you can change it later to Y or Z, depending of the axis you want.


        if (Math.abs(acceleration) > 0.3f) // the accelerometer value is < -0.3 and > 0.3 , this means that is not really stable and the position should move
        {
           position.x -= acceleration * speed * dt; // we move it
           // now check for the animations


           if (acceleration < 0) // if the acceleration is negative
               currentFrame = animation.getKeyFrame(4 + (int) stateTime);
           else
               currentFrame = animation.getKeyFrame(8 + (int) stateTime);
           // this might be exactly backwards, you'll check for it
       } else {
           // the sensor has some small movements, probably the device is not moving so we want to put the idle animation
           currentFrame = animation.getKeyFrame(12);
       }
float acceleration=Gdx.input.getAccelerometerX();//您可以稍后将其更改为Y或Z,具体取决于所需的轴。
如果(Math.abs(加速度)>0.3f)//加速计值<-0.3和>0.3,这意味着它不是真正稳定的,位置应该移动
{
位置.x-=加速度*速度*dt;//我们移动它
//现在检查动画
if(加速度<0)//如果加速度为负
currentFrame=animation.getKeyFrame(4+(int)stateTime);
其他的
currentFrame=animation.getKeyFrame(8+(int)stateTime);
//这可能完全是反向的,你要检查一下
}否则{
//传感器有一些小的移动,可能是设备没有移动,所以我们想把闲置的动画
currentFrame=animation.getKeyFrame(12);
}

谢谢你,伙计,这很有效:D只要改变动画,(8-4),因为现在就像月球漫步:D
 float acceleration=Gdx.input.getAccelerometerX(); // you can change it later to Y or Z, depending of the axis you want.


        if (Math.abs(acceleration) > 0.3f) // the accelerometer value is < -0.3 and > 0.3 , this means that is not really stable and the position should move
        {
           position.x -= acceleration * speed * dt; // we move it
           // now check for the animations


           if (acceleration < 0) // if the acceleration is negative
               currentFrame = animation.getKeyFrame(4 + (int) stateTime);
           else
               currentFrame = animation.getKeyFrame(8 + (int) stateTime);
           // this might be exactly backwards, you'll check for it
       } else {
           // the sensor has some small movements, probably the device is not moving so we want to put the idle animation
           currentFrame = animation.getKeyFrame(12);
       }