Help with LibGdx

0

I want to know how do I create an event by tapping an image using LibGdx ... I'm trying to start a game and added an image as a Play button.

    
asked by anonymous 30.05.2016 / 13:37

1 answer

0

You must implement an InputProcessor , and set it as the receiver of the events:

Gdx.input.setInputProcessor(instanciaInputProcessor);

This interface has all kinds of input events, in the case of touch, you will need to use the camera to know the relative position of the touch (if you use only a simple one, with the same screen size and without climbing, no need), then just check if that position is within the limits of the sprite. The libgdx wiki shows a complete example:

public class SimplerTouchTest extends ApplicationAdapter implements InputProcessor {
public final static float SCALE = 32f;
public final static float INV_SCALE = 1.f/SCALE;
public final static float VP_WIDTH = 1280 * INV_SCALE;
public final static float VP_HEIGHT = 720 * INV_SCALE;

private OrthographicCamera camera;
private ExtendViewport viewport;        
private ShapeRenderer shapes;

@Override public void create () {
    camera = new OrthographicCamera();
    viewport = new ExtendViewport(VP_WIDTH, VP_HEIGHT, camera);
    shapes = new ShapeRenderer();
    Gdx.input.setInputProcessor(this);
}

@Override public void render () {
    //Provavelmente você está usando sprite-batch
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    shapes.setProjectionMatrix(camera.combined);
    shapes.begin(ShapeRenderer.ShapeType.Filled);
    shapes.circle(tp.x, tp.y, 0.25f, 16);
    shapes.end();
}

@Override public void resize (int width, int height) {
    viewport.update(width, height, true);
}

@Override public void dispose () {
    shapes.dispose();
}
@Override public boolean touchDown (int screenX, int screenY, int pointer, int button) {
    //Aki vocÊ verifica se o screenX e sceenY estão dentro dos limites da sprite.
    if(screenX > posicaoX && screenX < posicaoX+largura && screenY > posicaoY && screenY < posicaoY+altura){
        //evento aki
    }
    return true;
}
//Abaixo tem mais vários eventos que você tem a obrigação de implementar (ja q e interface, mas use apenas quando quiser)
@Override public boolean mouseMoved (int screenX, int screenY) {
    return false;
}

@Override public boolean touchDragged (int screenX, int screenY, int pointer) {
    return true;
}

@Override public boolean touchUp (int screenX, int screenY, int pointer, int button) {
    return true;
}

@Override public boolean keyDown (int keycode) {
    return false;
}

@Override public boolean keyUp (int keycode) {
    return false;
}

@Override public boolean keyTyped (char character) {
    return false;
}

@Override public boolean scrolled (int amount) {
    return false;
}
}

You can simplify everything, but this is the safest way I know

    
30.05.2016 / 13:56