我目前正在创建一个库存系统,用户可以在其中从库存清单集合中选择一个项目,右侧的图标将会更新。
到目前为止,我已经实现了一个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()); }
我发现了这个问题,看看LibGDX Wiki Table - Adding Cells,看来你正在使用的add()
方法不能替换单元格中的前一个actor,它只会在行中添加另一个。相反,您应该将显示的图像与List
区分开来
public class PlayerInventory {
[..........................]
Item clickedItem; // This will save your clicked item
Image clickedImage; // This will save your clicked image;
[..........................]
inventory.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
if (clickedItem == null && clickedImage == null) {
clickedItem = inventory.getSelected(); // This line changed
clickedImage = clickedItem.getImage(); // This line changed
table.add(clickedImage);
} else {
clickedItem = inventory.getSelected(); // This line changed
clickedImage = clickedItem.getImage(); // This line changed
}
System.out.println(clickedItem.getName());
}
}
如果之前没有图像,这将添加图像,如果之前有图像,则替换图像。希望这可以帮助!
解决了。每次单击屏幕时都不应使用table.add(clickedImage)。 add()创建一个新单元格。相反,在初始布局期间只添加一次占位符Image,并保留对它的引用。在我的ClickListener中,我使用clickedImage.setDrawable()来改变显示的图像。