更新表中的映像

我目前正在创建一个清单系统,用户可以在其中从其清单列表集合中选择一个项目,右侧的图标将更新。


截至目前,我已经实现了一个 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();

    }


萧十郎
浏览 94回答 2
2回答

Helenr

解决。您不应该在每次单击屏幕时使用 table.add(单击图像)。add() 创建一个新单元格。相反,请在初始布局期间仅添加一次占位符图像,并保留对它的引用。在我的ClickListener中,我使用clickedImage.setDrawable()来更改显示的图像。

慕的地8271018

我发现了问题,查看LibGDX Wiki表 - 添加单元格,似乎您正在使用的方法不会替换单元格中的上一个actor,它只会在行中添加另一个actor。相反,您应该将显示的图像保存在add()Listpublic class PlayerInventory {&nbsp; &nbsp; [..........................]&nbsp; &nbsp; Item clickedItem; // This will save your clicked item&nbsp; &nbsp; Image clickedImage; // This will save your clicked image;&nbsp; &nbsp; [..........................]&nbsp; &nbsp; inventory.addListener(new ClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void clicked(InputEvent event, float x, float y) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (clickedItem == null && clickedImage == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickedItem = inventory.getSelected(); // This line changed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickedImage = clickedItem.getImage(); // This line changed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table.add(clickedImage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickedItem = inventory.getSelected(); // This line changed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickedImage = clickedItem.getImage(); // This line changed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(clickedItem.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}如果之前没有图像,这将添加图像,如果以前有图像,则替换图像。希望这有帮助!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java