0
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

buttonDown() and other methods inControllerListener only fired when the key is pressed.If you want constant movement you need a variable and set for example when KeyRight is called and unset when keyRight is released or another key like KeyLeft pressed There is a good sample here

http://www.gamefromscratch.com/post/2014/11/24/LibGDX-Video-Tutorial-Handling-Keyboard-Mouse-and-Touch-Input.aspx

As you see

   public boolean keyDown(int keycode) {
      if(keycode == Input.Keys.RIGHT)
         movingRight = true;
      return true;
   }

movingRight equals true whenever right arrow key pressed and in render() method try

   public void render () {

      if(movingRight)
         sprite.translateX(1f);
       //...
}
\$\endgroup\$
1
  • \$\begingroup\$ Sorry for taking so long to answer. It work perfectly ! Thank you once again :) \$\endgroup\$
    – aliasbody
    Commented Feb 18, 2015 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .