Game Development

java – Shifting sprite utilizing the accelerometer in LibGDX?

I do not know easy methods to use accelerometer in LibGDX; how may I exploit the the movement sensor? I am working for a prime down recreation with the movement sensor. I am new to this framework. My sprite picture is already working and I wish to management it utilizing the accelerometer.

All feedback are a lot appreciated, thanks prematurely 🙂

Right here is my code:

public class IngameDayOne extends ApplicationAdapter implements Display {

// Fixed rows and columns of the sprite sheet
personal static ultimate int FRAME_COLS = 5, FRAME_ROWS = 1;
// Objects used
Animation<TextureRegion> walkAnimation; // Should declare body kind (TextureRegion)
personal Texture cat ;
SpriteBatch spriteBatch;
Texture Background;
// A variable for monitoring elapsed time for the animation
float stateTime;


@Override
public void create() {
    // Load the sprite sheet as a texture
    cat = new Texture(Gdx.recordsdata.inner("cat2.png"));


    //Accelerometer
    float accelX = Gdx.enter.getAccelerometerX();
    float accelY = Gdx.enter.getAccelerometerY();

    if (accelX < -1){
        //go up
    }
    if (accelY < -1 ){
        //go left
    }
    if (accelY > +1){
        //go proper
    }


    // Use the break up utility methodology to create a 2D array of TextureRegions. That is
    // doable as a result of this sprite sheet accommodates frames of equal measurement and they're
    // all aligned.
    TextureRegion[][] tmp = TextureRegion.break up(cat, cat.getWidth() / FRAME_COLS, cat.getHeight()/ FRAME_ROWS);

    // Place the areas right into a 1D array within the right order, ranging from the highest
    // left, going throughout first. The Animation constructor requires a 1D array.

    TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
    int index = 0;
    for (int i = 0; i < FRAME_ROWS; i++) {
        for (int j = 0; j < FRAME_COLS; j++) {
            walkFrames[index++] = tmp[i][j];
        }
    }
    // Initialize the Animation with the body interval and array of frames
    walkAnimation = new Animation<TextureRegion>(0.090f, walkFrames);

    // Instantiate a SpriteBatch for drawing and reset the elapsed animation
    // time to 0
    spriteBatch = new SpriteBatch();
    stateTime = 0f;
    Background = new Texture(Gdx.recordsdata.inner("flooring.png")); //File from belongings folder
}

@Override
public void present() {
}

@Override
public void render(float delta) {

}

@Override
public void resize(int width, int top) {

}
@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear display
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time
    // Get present body of animation for the present stateTime
    TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
    spriteBatch.start();
    spriteBatch.draw(Background,0,0);
    spriteBatch.draw(currentFrame, 50, 50); // Draw present body at (50, 50)
    spriteBatch.finish();

}
@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void conceal() {

}

@Override
public void dispose() { // SpriteBatches and Textures should at all times be disposed
    spriteBatch.dispose();
    cat.dispose();
    Background.dispose();
}
}

About the author

Theme control panel

Leave a Comment