TextButton isPressed() 不工作
TextButton isPressed() not working
我在使用 scene2d 时遇到问题。我以编程方式创建了一个 TextButton(没有 json 文件),出于某种原因,当我按下它时,isPressed() 不会 return 为真。不仅如此,我还设置了我的 TextButtonStyle 的 downFontColor,但它也没有做任何事情。这是我第一次使用 scene2d,所以我还是新手。我正在 android 上进行测试。
我使用了扩展 TextButton 的自定义 class,这与问题无关,它只是一个空的 class。不要说我已经在文本按钮上测试过它。这是 classes :
MainMenu,我在其中创建舞台:
public MainMenu(Main main) {
this.main = main;
font_neutral = Tools.generateFont(Gdx.files.internal("Fonts/Android.ttf"), Color.WHITE, 150);
initViewport();
//Scene2D
GlyphLayout glyphLayout = new GlyphLayout(); //Used for getting width/height of textButton. Set text, then get width
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.font = font_neutral;
style.fontColor = new Color(1, 1, 1, 1);
style.downFontColor = new Color(40, 130, 100, 1);
style.checkedFontColor = Color.BLUE;
CustomTextButton button_play = new CustomTextButton("Play", style);
glyphLayout.setText(style.font, "Play");
float x = camera.viewportWidth / 3 - glyphLayout.width / 2;
float y = camera.viewportHeight / 2 - glyphLayout.height / 2;
button_play.setPosition(x, y);
button_play.setTouchable(Touchable.enabled);
stage = new Stage(viewport);
stage.addActor(button_play);
}
@Override
public void show() {
}
public void update(float delta) {
stage.act(delta);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(1, 1, 1, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glEnable(GL20.GL_BLEND);
stage.draw();
}
再次感谢!
Stage
是一个InputProcessor
,你需要告诉LibGDX听Stage
。在初始化你的 Stage
.
之后放置 Gdx.input.setInputProcessor(stage)
我在使用 scene2d 时遇到问题。我以编程方式创建了一个 TextButton(没有 json 文件),出于某种原因,当我按下它时,isPressed() 不会 return 为真。不仅如此,我还设置了我的 TextButtonStyle 的 downFontColor,但它也没有做任何事情。这是我第一次使用 scene2d,所以我还是新手。我正在 android 上进行测试。
我使用了扩展 TextButton 的自定义 class,这与问题无关,它只是一个空的 class。不要说我已经在文本按钮上测试过它。这是 classes :
MainMenu,我在其中创建舞台:
public MainMenu(Main main) {
this.main = main;
font_neutral = Tools.generateFont(Gdx.files.internal("Fonts/Android.ttf"), Color.WHITE, 150);
initViewport();
//Scene2D
GlyphLayout glyphLayout = new GlyphLayout(); //Used for getting width/height of textButton. Set text, then get width
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.font = font_neutral;
style.fontColor = new Color(1, 1, 1, 1);
style.downFontColor = new Color(40, 130, 100, 1);
style.checkedFontColor = Color.BLUE;
CustomTextButton button_play = new CustomTextButton("Play", style);
glyphLayout.setText(style.font, "Play");
float x = camera.viewportWidth / 3 - glyphLayout.width / 2;
float y = camera.viewportHeight / 2 - glyphLayout.height / 2;
button_play.setPosition(x, y);
button_play.setTouchable(Touchable.enabled);
stage = new Stage(viewport);
stage.addActor(button_play);
}
@Override
public void show() {
}
public void update(float delta) {
stage.act(delta);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(1, 1, 1, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glEnable(GL20.GL_BLEND);
stage.draw();
}
再次感谢!
Stage
是一个InputProcessor
,你需要告诉LibGDX听Stage
。在初始化你的 Stage
.
Gdx.input.setInputProcessor(stage)