Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Make continuos movements with controllers in LibGDX

$
0
0

I’ve been trying to make the OUYA controlers work with a LibGDX Framework Practice Game.

Everything is working perfectly expected for one thing : The movements are not continuos.

The problem is, when I press the DPAD or the Joystick into the right for example, it moves for the number of pixels I’ve defined and then stops, I need to release the button and press again.

When I use the Keyboard I put the code on the “render” method, using this method when I press the Right Arrow Key the sprite won’t stop movie until I release the button, but since the controller use a ControllerListener I don’t really know how to solve this issue.

This is what I have so far :

public class MyOntem extends ApplicationAdapter implements ApplicationListener, ControllerListener {
SpriteBatch batch;
Sprite sprite;

@Override
public void create () {
    batch = new SpriteBatch();
    sprite = new Sprite(new Texture("badlogic.jpg"));
}

@Override
public void render () {
    if (Gdx.input.isKeyPressed(Input.Keys.DPAD_RIGHT))
        sprite.setX(sprite.getX() + 10f);

    if (Gdx.input.isKeyPressed(Input.Keys.DPAD_LEFT))
        sprite.setX(sprite.getX() - 10f);

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(sprite, sprite.getX(), sprite.getY());
    batch.end();
}

@Override
public boolean buttonDown(Controller controller, int buttonCode) {
    if(buttonCode == Ouya.BUTTON_Y)
        sprite.setY(sprite.getY() + 1);
    if(buttonCode == Ouya.BUTTON_O)
        sprite.setY(sprite.getY()-1);
    if(buttonCode == Ouya.BUTTON_U)
        sprite.setX(sprite.getX() - 1);
    if(buttonCode == Ouya.BUTTON_A)
        sprite.setX(sprite.getX() + 1);

    return false;
}

@Override
public boolean axisMoved(Controller controller, int axisCode, float value) {
    // Left Stick
    if(axisCode == Ouya.AXIS_LEFT_X)
        sprite.translateX(10f * value);
    if(axisCode == Ouya.AXIS_LEFT_Y)
        sprite.translateY(-10f * value);

    // Right stick
    if(axisCode == Ouya.AXIS_RIGHT_X)
        sprite.rotate(10f * value);
    return false;
}

Thank you in Advance.


Viewing all articles
Browse latest Browse all 434

Trending Articles