You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LibGDX技术求助:TextField不显示且Button触摸无响应

Fixing LibGDX TextField & Button Touch Issues

Got it, let's fix those two issues you're facing with LibGDX—TextField not showing up and the vault1 image not responding to clicks. Here's what's going wrong and how to resolve it step by step:

Why Your TextField Isn't Showing

  • You created the vaultInput TextField, but never added it to your Stage. The Stage only renders actors that are part of its hierarchy.
  • Your render() method doesn't call stage.draw(), so even if you added the TextField, it wouldn't get rendered.

Why Your Vault1 Image Has No Touch Response

  • The vault1Button Image actor you created isn't added to the Stage. Since you set the Stage as the input processor, it only detects touches on actors that are part of the Stage.
  • You're drawing a separate Sprite with drawSprite() in render()—this is a completely separate element from the vault1Button Image, so clicking the drawn sprite won't trigger the button's listener.

Fixed Code Implementation

Here's the adjusted code that addresses both issues and meets your requirement of showing "5" in the TextField when vault1 is clicked:

private Stage stage;
private Skin skin;
private TextField vaultInput;
private Image vault1Button;
private TextureAtlas textureAtlas;

@Override
public void show() {
    // Initialize Stage and set it as input processor
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);

    // Load your UI assets
    textureAtlas = new TextureAtlas("ui/atlas.pack");
    skin = new Skin(Gdx.files.internal("ui/menuSkin.json"), textureAtlas);

    // Create and configure TextField
    vaultInput = new TextField("", skin);
    vaultInput.setPosition(300, 250);
    vaultInput.setSize(300, 40);
    stage.addActor(vaultInput); // Critical: Add TextField to Stage

    // Create vault1 button using the atlas sprite, add to Stage
    Sprite vault1Sprite = textureAtlas.createSprite("vault1");
    vault1Button = new Image(vault1Sprite);
    vault1Button.setPosition(600, 450); // Match your original draw position
    vault1Button.addListener(new ClickListener() {
        @Override
        public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
            System.out.println("Vault1 clicked!");
            vaultInput.setText("5"); // Set the desired number directly
            return true; // Return true to consume the event and enable full click lifecycle
        }
    });
    stage.addActor(vault1Button); // Critical: Add button to Stage
}

@Override
public void render(float delta) {
    // Clear the screen first
    Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // If you have non-UI game sprites to draw, do that here with your batch:
    // batch.begin();
    // ... your game drawing logic ...
    // batch.end();

    // Update Stage logic (input, animations) and render UI
    stage.act(delta);
    stage.draw();
}

// Remove your original drawSprite method—we're using the Stage's Image instead

Key Changes Explained

  1. Adding Actors to Stage: Both vaultInput and vault1Button are added to the Stage, so they get rendered and can receive input.
  2. Stage Rendering: The render() method now calls stage.act(delta) (to handle input and updates) and stage.draw() (to render all UI actors).
  3. Single Source of Truth: We use the Stage's Image for vault1 instead of drawing a separate Sprite—this ensures the clickable area matches what's on screen.
  4. Click Event Handling: Returning true from touchDown ensures the click event is fully processed (including touchUp if needed).

内容的提问来源于stack exchange,提问作者Filip Fahlström

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:15:07