1
\$\begingroup\$

I created a separate inputHandler class implementing InputProcessor. I'm using Event Handling (libgdx github page: https://github.com/libgdx/libgdx/wiki/Event-handling)

public class inputHandler implements InputProcessor {
    private cheetah cheetah;
    private gameWorld myWorld;
    private float cheetahX;

    private float scaleFactorX;
    private float scaleFactorY;

    public inputHandler(gameWorld myWorld, float scaleFactorX,
            float scaleFactorY) {
        this.myWorld = myWorld;
        cheetah = myWorld.getCheetah();
        cheetahX = cheetah.getX();

        int midPointY = myWorld.getMidPointY();

        this.scaleFactorX = scaleFactorX;
        this.scaleFactorY = scaleFactorY;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        System.out.println(cheetahX);
        cheetahX += 5;
        System.out.println(cheetahX);
        return true;
    }

cheetahX is updating in the console, but not on the screen. When I click on the DesktopLauncher window, the console prints cheetahX + 5 properly, but the image of the cheetah sprite remains still.

gameWorld class:

public gameWorld(int midPointY, int gameHeight) {
    currentState = GameState.MENU;
    this.midPointY = midPointY;
    cheetah = new cheetah(33, midPointY - 15, 10, 29);
}

public void update(float delta) {
    runTime += delta;
}

gameScreen class:

@Override
public void render(float delta) {
    // TODO Auto-generated method stub
    runTime += delta;
    world.update(delta);
    renderer.render(delta, runTime);
}

thanks in advance!

\$\endgroup\$
1
\$\begingroup\$

Are you assigning cheetahX to something or drawing with it? You did not post your drawing method or where you use cheetahX further on in your code.

I predict that Cheetah holds your InputHandler then you need this:

public class inputHandler implements InputProcessor {
    private cheetah cheetah;
    private gameWorld myWorld;
    private float cheetahX;
    public float getCheetahX() { return cheetahX; } // This line

    private float scaleFactorX;
    private float scaleFactorY;

    public inputHandler(gameWorld myWorld, float scaleFactorX,
            float scaleFactorY) {
        this.myWorld = myWorld;
        cheetah = myWorld.getCheetah();
        cheetahX = cheetah.getX();

        int midPointY = myWorld.getMidPointY();

        this.scaleFactorX = scaleFactorX;
        this.scaleFactorY = scaleFactorY;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        System.out.println(cheetahX);
        cheetahX += 5;
        System.out.println(cheetahX);
        return true;
    }

Now you can access it from cheetah. Somewhere in your update method you should grab it and assign the cheetah position with it.

public void update()
{
    position.x = inputHandler.getCheetahX();
    // Or
    batch.draw(cheetahTexture, inputHandler.getCheetahX(), position.y, ...);
}

I do not know how you draw or how you would like to use that variable so unless you supply all of your relevant code I cannot help you further.

\$\endgroup\$
  • \$\begingroup\$ Thank you! This solved the problem and my sprite is now moving perfectly :) \$\endgroup\$ – resfever Jul 27 '15 at 0:37
  • \$\begingroup\$ No problem and welcome to the SE network ;) \$\endgroup\$ – Madmenyo Jul 27 '15 at 7:04

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.