我当前正在创建一个清单系统,用户可以在其中从清单列表集中选择一个项目,并且右侧的图标将更新。
到目前为止,我已经实现了ClickListener来从清单中获取当前选中的商品,并调用了我创建的.getImage()方法来获取选中商品的图标。
我用了table.add(image);将图标添加到表格中。但是,将图标添加到表格后,它将填满该空间,并且当用户在另一个项目上进行选择时,将在右侧创建另一列。这就是问题。
如何在用户选择其他项目而不是在右侧创建另一列时更新图像区域?
目前是这样的:https://imgur.com/a/O6SW8gi
我想要用用户单击的最新项目更新剑的区域。
这是我的代码:
package com.sps.game.inventory;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.sps.game.controller.InventoryController;
import com.sps.game.controller.PlayerController;
import java.util.ArrayList;
public class PlayerInventory {
public Stage stage;
public SpriteBatch sb;
private Viewport viewport;
private Skin skin = new Skin(Gdx.files.internal("core/assets/pixthulhuui/pixthulhu-ui.json"));
private List<Item> inventory;
private List<Image> itemImages;
private InventoryController inventoryController;
private InputProcessor oldInput;
Table table = new Table(skin);
public PlayerInventory(SpriteBatch sb, PlayerController playerController) {
this.sb = sb;
viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera());
stage = new Stage(viewport, sb);
inventoryController = new InventoryController();
inventory = inventoryController.getInventoryList();
// itemImages = inventoryController.getImageList();
}
//THIS IS WHERE THE IMAGE IS ADDED
private void formatting() {
stage = new Stage();
Label inventoryLabel = new Label("Inventory", skin);
final Label imageLabel = new Label("Item", skin);
table.setDebug(true);
table.defaults();
table.center();
table.setFillParent(true);
table.add(inventoryLabel);
table.add(imageLabel);
table.row();
table.add(inventory); //need a way to get the current item selected
inventory.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
Item clickedItem = inventory.getSelected();
Image clickedImage = clickedItem.getImage();
table.add(clickedImage);
System.out.println(clickedItem.getName());
}
});
// stage.addActor(itemImages);
stage.addActor(table);
}
public void setInput() {
oldInput = Gdx.input.getInputProcessor(); //Get the old input from the user.
Gdx.input.setInputProcessor(stage); //Set the input to now work on the inventory.
}
public void update() {
if (Gdx.input.isKeyPressed(Input.Keys.I) && oldInput == null) {
formatting();
setInput();
}
if (Gdx.input.isKeyPressed(Input.Keys.O) && oldInput != null) {
stage.dispose();
Gdx.input.setInputProcessor(oldInput);
oldInput = null;
}
}
public void dispose() {
stage.dispose();
}
}
图像以格式设置方法()-
添加inventory.addListener(new ClickListener() { public void clicked(InputEvent event, float x, float y) { Item clickedItem = inventory.getSelected(); Image clickedImage = clickedItem.getImage(); table.add(clickedImage); System.out.println(clickedItem.getName()); }
答案 0 :(得分:0)
已解决。每次单击屏幕时,都不应使用table.add(clickedImage)。 add()创建一个新的单元格。相反,在初始布局期间仅添加一次占位符图像,并保留对其的引用。在我的ClickListener中,我改用clickedImage.setDrawable()来更改显示的图像。