Search code examples
javalibgdx

InputProcessor usage


I am new in game development and am trying to build simple 2D game with multiple screens (mainMenu, playScreen,..). In PlayScreen I'm rendering animation and would like to check if that animation was touched/clicked. How do I check if touch happened inside that animation or not? Since im having multiple screens, is it better to use Screens and InputProcessor seperately (Use same input Processor for all screens) or in same class (each screen uses his own processor? My current code looks like that:

public class InputHandler implements InputProcessor {
private Handler h;
public boolean touch = false;

public InputHandler(Handler h) {
    this.h = h;
}


@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 touchDown(int screenX, int screenY, int pointer, int button) {
    touch = true;
    return false;
}

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

PlayScreen class:

public class PlayScreen implements Screen {
private SpriteBatch batch;
Animation monster;
Texture background;
public TextureAtlas atlas;
TextureRegion region;
float stateTime;
private int height;
private Viewport viewport;
private Camera camera;
private int width;
private float elapsedTime = 0;
ShapeRenderer shapeRenderer;
private Handler h;
private Stage stage;
private InputProcessor processor;

public PlayScreen(Handler h){
    this.h = h;
    batch = h.batch;
    camera = h.camera;
    viewport = h.viewport;
    height = h.height;
    width = h.width;
    stage = new Stage(viewport,batch);
    stateTime = 0f;
    background = new Texture("hd/hd_background.png");
    atlas = new TextureAtlas(Gdx.files.internal("mons1.txt"));
    monster = new Animation(1/15f, atlas.getRegions());

}



@Override
public void show() {
input.setInputProcessor(processor);
}

@Override
public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();

    batch.draw(background ,0,0,width, height);
    elapsedTime += Gdx.graphics.getDeltaTime();

    region = (TextureRegion) monster.getKeyFrame(elapsedTime, true);
    batch.draw(region, width/2-region.getRegionWidth()/2,height - region.getRegionHeight() -30);
}

@Override
public void resize(int width, int height) {
    h.viewport.update(width, height);
    camera.update();
    h.viewport.apply();

}

@Override
public void dispose() {
    background.dispose();
    atlas.dispose();
    batch.dispose();
}

I am also using separate class Handler, which I'm getting all game data from (I am also not sure if that is best decision).


Solution

  • The best way to use InputProcessor is to implement the methods in each Screen.

    public class PlayScreen implements Screen, InputProcessor {}

    Now you have a class which is a Screen and an InputProcessor. The relevant method for a click event is the touchDown method.

    Using the screenX and screenY values you can use some basic math to figure out whether the click is inside your Animation.

    Good luck! If you have any more questions please feel free to post a comment below!