0

I got a moving square wich moves around in my window and this works fine. But when I try to use public void clicked from libgdx I can't get it to work. This is how my void looks like:

public void clicked(InputEvent event,float x,float y){
    rect.y = y;
    rect.x = x;
}

Above this im also adding the movement from the box and this works fine so its not rect.y and rect.x. Why aren't rect.y and rect.x changing to the float x and float y from my void when Clicked?

Libgdx Void:

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/ClickListener.html#clicked-com.badlogic.gdx.scenes.scene2d.InputEvent-float-float-

Why isn't this working?

Thanks for reading/helping!

1 Answer 1

1

Put this in your game's render method:

if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
    rect.x = Gdx.input.getX();
    rect.y = Gdx.input.getY();
}

rect must be defined within your game's class.

6
  • Can you explain this solution a bit more? If i put this code in my gamerederer class it ends up with an error cause rect is private.
    – Stefan
    Jan 20, 2015 at 13:21
  • The current mouse location is consistently stored in Gdx.input. Jan 20, 2015 at 13:25
  • Allright when i try this the square ends up dissapearing from the screen? if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)) { myWorld.getRect().x = Gdx.input.getX(); myWorld.getRect().y = Gdx.input.getY(); }
    – Stefan
    Jan 20, 2015 at 13:27
  • As for the placement of this condition, it should always be called in the render loop for continuous testing. If you have a render method defined in your GameRenderer class and a rect object there, you can define a new GameRenderer in you game's class and draw indirectly from this object. Jan 20, 2015 at 13:27
  • x and y are the lower left coordinates, you know that right? Jan 20, 2015 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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