Question

i want to show a picture, then wait for get input (i mean touch), after that check it with position of picture. batch must be stay in alert for my input. but i don't know where i must compare, input and position of pic. or how i must use wait, to create delay for get touch input. for example, i use this code. but it just show black screen...

public void render() {
    x = rand.nextFloat() * 1024;
    y = rand.nextFloat() * 700;
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(texture, x, y);
    if (Gdx.input.justTouched()) {
        if (Gdx.input.getX() > x && Gdx.input.getX() < texture.getWidth() + x) {
            if (Gdx.input.getY() > y && Gdx.input.getY() < texture.getHeight() + y) {
                batch.end();
            }
        }
    }
}

or use wait for create delay in show pic.

public void render() {
    x = rand.nextFloat() * 1024;
    y = rand.nextFloat() * 700;
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(texture, x, y);
    try {
        batch.wait();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    batch.end();
}

i just begin gdx and android. sorry for my bad language.

Was it helpful?

Solution

You don't have to wait for input. Main application thread polls input events for you. If, for example, mouse button is pressed, then this event is polled and you can check if left mouse is currently pressed in your render() method and act accordingly. For example:

public void render() {
  ...
  if(Gdx.input.isButtonPressed(Buttons.LEFT)) {
      //move sprite left
  }
}

However i think that better way is to use InputProcessor. Implement InputProcessor and register it:

Gdx.input.setInputProcessor(yourInputProcessor);

In this case, main application thread processes polled events, and calls InputProcessor's callback methods.

See:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top