Search code examples
libgdxaccelerometer

Toggle between accelerometer and touchDown in libGDX


I'm using the touchDown method (InputProcessor)to move a sprite in the x-axis, i also want it to be moved using the accelerometer.

Is there a way in libGDX to toggle between the two methods and can they both be implemented together (so that the movement is done using the accelerometer as long as the screen is not touched or will they conflict with each other when phone is tilted and screen is touched)


Solution

  • You can do polling.

    if (Gdx.input.isTouched()) {
      // screen touched logic
    } else {
      // check for accelerometer
      float accelX = Gdx.input.getAccelerometerX();
      float accelY = Gdx.input.getAccelerometerY();
      float accelZ = Gdx.input.getAccelerometerZ();
      // do stuff with the accel(s)
    }