What is the simplest way to make Image touchable in libgdx?

社会主义新天地 提交于 2019-12-21 11:59:05

问题


Here's my code:

@Override
public void resize(int width, int height) {
super.resize(width, height);

stage.clear();

// background image
Image backImage = new Image(backTexture);
backImage.setX(0);
backImage.setY(0);
backImage.setWidth(800);
backImage.setHeight(480);

// start game image
Image startImage = new Image(startTexture);
startImage.setX(415);
startImage.setY(180);
startImage.setWidth(323);
startImage.setHeight(69);

stage.addActor(backImage);
stage.addActor(startImage);
}

I'm creating simple game and this is the way I'm creating main menu. I want to make my startImage touchable (clickable) as there is no available listener for Image class. How should I do this? Or is it better to use ImageButton? This way I would have to create image atlas which seems to be a little bit complicated for me now..

EDIT: I tried following:

startImage.addListener(new ClickListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
    {
        game.setScreen(new SplashScreen(game));
        return true;
    }
});

But it still doesn't work..

EDIT:

Gdx.input.setInputProcessor(stage);

needs to be called. Solved.


回答1:


Solution is to call following:

Gdx.input.setInputProcessor(stage);


来源:https://stackoverflow.com/questions/13863138/what-is-the-simplest-way-to-make-image-touchable-in-libgdx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!