【问题标题】:Hovering over text not changing colour - libgdx将鼠标悬停在不改变颜色的文本上 - libgdx
【发布时间】:2018-07-15 20:08:23
【问题描述】:

当我将鼠标悬停在字体上时,我试图将其颜色更改为白色,以便用户可以清楚地单击该特定按钮以继续。 然而,我很困惑为什么它没有像白色一样出现,而是保持原来的颜色。 我正在使用 Hiero 来获取我的字体。

if (hovertext1.contains(tp.x, tp.y)) {
        font5.setColor(Color.WHITE);
        if (Gdx.input.isTouched()) {
            game.setScreen(new ChooseLevel(game));
            dispose();
        }
} else {
        font5.setColor(Color.BLACK);
}

我只是困惑为什么当我悬停它时它没有改变颜色?

Hovertext 是使用 Rectangle 制作的变量。

tp - 来自:

Vector3 tp = new Vector3();
@Override
public boolean mouseMoved(int screenX, int screenY) {
    // TODO Auto-generated method stub
    camera.unproject(tp.set(screenX, screenY, 0));
    return false;

我在这里确定了hovertext1;

hovertext1 = new Rectangle(330,255,125,50);

如果我要让它返回 true,我会在哪里做呢?

【问题讨论】:

  • 嗯,可能hovertext1.contains(tp.x, tp.y) 返回falsehovertext1 是什么? tp是什么,tp.xtp.y是怎么计算的?
  • 我已经编辑了问题
  • 以及hovertext1的大小和位置是怎么计算的?你检查了吗,hovertext1.contains(tp.x, tp.y)ever 返回 true 吗?
  • 我不确定它会在哪里返回 true 或 false - 我没有得到它作为布尔值?
  • hovertext1.contains(tp.x, tp.y) 方法返回boolean。有可能它总是返回false,这意味着永远不会调用font5.setColor(Color.WHITE);

标签: java eclipse colors libgdx hover


【解决方案1】:

这不是您问题的直接答案,但我认为使用 Scene2D 库对您的任务会很方便。只是为了说明我的意思 - 这是一个非常简单的示例,说明如何按照您的要求进行操作:

public class MyGdxGame extends ApplicationAdapter {

    Stage stage;

    public void create() {
        stage = new Stage();

        stage = new Stage();
        final Label label = new Label("TEST", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        label.setColor(Color.BLACK);
        label.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2);
        label.addListener(
                new InputListener() {
                    @Override
                    public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                        label.setColor(Color.WHITE);
                    }

                    @Override
                    public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                        label.setColor(Color.BLACK);
                    }
                }
        );
        stage.addActor(label);
        Gdx.input.setInputProcessor(stage);
    }

    public void render() {
        Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        float delta = Gdx.graphics.getDeltaTime();
        stage.act(delta);
        stage.draw();
    }

}

了解更多关于Scene2Dhttps://github.com/libgdx/libgdx/wiki/Scene2d

【讨论】:

    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多