gpt4 book ai didi

java - 如何为 libgdx 中的按钮添加绘图功能?

转载 作者:行者123 更新时间:2023-12-01 11:14:27 25 4
gpt4 key购买 nike

我正在使用 Screen-2D 构建一个按钮。我想给按钮一个功能,当单击时将绘制一个 Sprite ,我该怎么做。这不是我的全部代码,但足以展示我正在谈论的内容。

public void create () {
buttonStyle = new TextButtonStyle();
buttonStyle.up = skin.getDrawable("button");
buttonStyle.over = skin.getDrawable("buttonpressed");
buttonStyle.down = skin.getDrawable("buttonpressed");
buttonStyle.font = font;
button = new TextButton("START", buttonStyle);

stage.addActor(button);
Gdx.input.setInputProcessor(stage);
button.addListener(new InputListener() {
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
drawTile(200,50);
return true;
}
});
}

// method used to draw a sprite when passing certain coordinates
public void drawTile(int x , int y) {
spriteBatch.draw(sprite, x , y );
}

public void render () {
Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
spriteBatch.begin();
spriteBatch.draw(background, 0, 0);
drawGrid();
spriteBatch.draw(startButton, 0, 0);
stage.draw();
spriteBatch.end()
}

最佳答案

你的想法是对的。请参阅此示例:

button.addListener(new ChangeListener() {
@Override
public void changed (ChangeEvent event, Actor actor) {
drawTile(200,50);
}
});

https://github.com/libgdx/libgdx/wiki/Scene2d.ui#changeevents

关于java - 如何为 libgdx 中的按钮添加绘图功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31996537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com