Help with Libgdx

Asked

Viewed 390 times

0

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

1 answer

0


You must implement a Inputprocessor, and define it as a receiver of 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, same screen size and without scaling, you do not need)Then just make sure that position is within the confines of 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;
}
}

It has to simplify everything, but this way is the safest I know

Browser other questions tagged

You are not signed in. Login or sign up in order to post.