0
\$\begingroup\$

I am working on a game which uses the accelerometer to move an object called "plane". I know how to add an accelerometer, but I can't get the plane to move.

package com.wyvern.pp.states;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.wyvern.pp.PPlane;


public class PlayState extends State{
    private boolean peripheralAvailable;

    private Texture walls;
    private Texture bg;
    private Texture plane;

    public PlayState(GameStateManager gsm) {
        super(gsm);

        walls = new Texture("walls.png");
        bg = new Texture("bg.png");
        plane = new Texture("plane_left.png");

        cam.setToOrtho(true);

        peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
    }

    @Override
    protected void handleInput() {

    }

    @Override
    public void update(float dt) {
        handleInput();

        float xRot;
        if(peripheralAvailable) {
            xRot = Gdx.input.getAccelerometerX();
        }
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.begin();
        sb.draw(bg, 0, 0, PPlane.WIDTH, PPlane.HEIGHT);
        sb.draw(walls, 0, 0);
        sb.draw(plane, xRot, 15 ); // gives an error 
        sb.draw(walls, PPlane.WIDTH - walls.getWidth(), 0);
        sb.end();
    }

    @Override
    public void dispose() {

    }
}

How do I get the plane to move left and right on the X axis?

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you tried declaring and updating a variable to represent position? That would be a good start. \$\endgroup\$ – Seth Battin Sep 9 '15 at 12:57
  • \$\begingroup\$ There is literally no code in that example to indicate you've tried to move anything. Either you haven't added in the correct part of the example or you haven't tried yet. \$\endgroup\$ – Tom 'Blue' Piddock Sep 15 '15 at 10:22
1
\$\begingroup\$

It gives an error because it doesn't know what xRot is. In your method declaration of update(), you define float xRot;. Because you defined it within the declaration, it only exists within that declaration—it's a local variable.

To fix the error, just move the variable's declaration (float xRot;) up to where all the other fields (variables declared at the top of the class) are, like this:

package com.wyvern.pp.states;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.wyvern.pp.PPlane;


public class PlayState extends State{
    private boolean peripheralAvailable;

    private Texture walls;
    private Texture bg;
    private Texture plane;

    private float xRot; // here

    public PlayState(GameStateManager gsm) {
        super(gsm);

        walls = new Texture("walls.png");
        bg = new Texture("bg.png");
        plane = new Texture("plane_left.png");

        cam.setToOrtho(true);

        peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer);
    }

    @Override
    protected void handleInput() {

    }

    @Override
    public void update(float dt) {
        handleInput();

        // no "float xRot" here

        if(peripheralAvailable) {
            xRot = Gdx.input.getAccelerometerX();
        }
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.begin();
        sb.draw(bg, 0, 0, PPlane.WIDTH, PPlane.HEIGHT);
        sb.draw(walls, 0, 0);
        sb.draw(plane, xRot, 15 ); // no longer gives an error 
        sb.draw(walls, PPlane.WIDTH - walls.getWidth(), 0);
        sb.end();
    }

    @Override
    public void dispose() {

    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.