在 libgdx 中使 Image 可触摸的最简单方法是什么? [英] What is the simplest way to make Image touchable in libgdx?

查看:22
本文介绍了在 libgdx 中使 Image 可触摸的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

@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);
}

我正在创建简单的游戏,这就是我创建主菜单的方式.我想让我的 startImage 可触摸(可点击),因为 Image 类没有可用的侦听器.我该怎么做?还是使用 ImageButton 更好?这样我就必须创建图像图集,这对我来说似乎有点复杂..

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..

我尝试了以下操作:

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

但是还是不行..

Gdx.input.setInputProcessor(stage);

需要调用.解决了.​​

needs to be called. Solved.

推荐答案

解决方案是调用如下:

Gdx.input.setInputProcessor(stage);

这篇关于在 libgdx 中使 Image 可触摸的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆