LibGDX技术求助:TextField不显示且Button触摸无响应
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
vaultInputTextField, but never added it to your Stage. The Stage only renders actors that are part of its hierarchy. - Your
render()method doesn't callstage.draw(), so even if you added the TextField, it wouldn't get rendered.
Why Your Vault1 Image Has No Touch Response
- The
vault1ButtonImage 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()inrender()—this is a completely separate element from thevault1ButtonImage, 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
- Adding Actors to Stage: Both
vaultInputandvault1Buttonare added to the Stage, so they get rendered and can receive input. - Stage Rendering: The
render()method now callsstage.act(delta)(to handle input and updates) andstage.draw()(to render all UI actors). - 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.
- Click Event Handling: Returning
truefromtouchDownensures the click event is fully processed (includingtouchUpif needed).
内容的提问来源于stack exchange,提问作者Filip Fahlström
相关产品推荐
相关产品推荐

