为什么Libgdx中addActor后纹理仍然没有显示在屏幕上?

问题描述 投票:0回答:1

我正在尝试创建一个三消游戏,我创建了一个 Tile 类来表示各个游戏图块,它包含有关图块的行和列位置的图像和数据。此外,我有一个 Board 类,其中包含 Tile 对象数组,特别是 8x8 网格。这是游戏网格的核心结构。在 GameScreen 类中,我尝试将每个图块添加为参与者,这应该将它们显示在屏幕上。 但是,该设置未按预期运行。游戏无法正确渲染图块,我不确定问题是否在于如何添加参与者,或者问题是否与渲染或游戏逻辑的其他方面有关。下一步是调试图块和棋盘之间的交互,确保图块在游戏舞台上正确定位和渲染。请帮助我。

这里是代码:

public class Tile extends Image
{
    public int row;
    public int col;
    public int type = -1;

    public Tile(int col, int row){
        this.col = col;
        this.row = row;
    }

    public void init(TextureRegion sprite, int index)
    {
        setSize(getWidth(), getHeight());
        setBounds(getX(), getY(), getWidth(), getHeight());
        setDrawable(new TextureRegionDrawable(sprite));
        debug();
        this.type = index;
    }

    public void setRowCol(int row, int col){
        this.row = row;
        this.col = col;
    }
}

public class Board extends Group implements Disposable
{
    Match3Game game;
    Array<TextureAtlas.AtlasRegion> entities;
    Tile[][] tiles = new Tile[8][8];
    //
    public Board(Match3Game game, Array<TextureAtlas.AtlasRegion> sprites)
    {
        this.game = game;
        this.entities = sprites;

        initialize();
    }
    //
    private void initialize()
    {
        setBounds(0,0,640,640);

        for (int i = 0; i < tiles.length; i++)
        {
            for (int j = 0; j < tiles[i].length; j++)
            {
                Tile tile = new Tile(i ,j);
                tile.addListener(clickListener);
                // random number in atlas
                int num = MathUtils.random(1,5);
                tile.init(this.entities.get(num),num);
                tile.setPosition(j * tile.getWidth(), i * tile.getHeight());
                tiles[i][j] = tile;
                //add tile in addActor
                this.addActor(tile);

                if(tiles[i][j] == null)
                    Gdx.app.log("Board", "Could not found Board Texture");
            }
        }
    }
    ...
}

public class GameScreen extends ParentScreen
{
    Match3Game game;
    Board board;
    //Constructor
    public GameScreen(Match3Game game)
    {
        super(game);
        this.game = game;

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void show()
    {
        super.show();
        //
        setUpBoard();
    }
    
    private void setUpBoard()
    {
        //todo
        board = new Board(game, Assets.getTexture().findRegions("color"));
            //set position
////        board.setPosition(getWidth()/2, getHeight()/5, Align.center);
////        board.setPosition(0,0);
        board.debugAll();
        this.addActor(board);
    }
    ...
}
java libgdx
1个回答
0
投票

在游戏屏幕中看不到任何摄像机、视图屏幕和舞台。如果您有的话,请不要忘记将董事会演员添加到舞台上。 我在主课上有我的:

    @Override
    public void create() {
        guiCamera = new OrthographicCamera(SCREEN_WIDTH, SCREEN_HEIGHT);
        guiCamera.setToOrtho(false, SCREEN_WIDTH, SCREEN_HEIGHT);
        if (isMobile()) {
            guiViewport = new ExtendViewport(SCREEN_WIDTH, SCREEN_HEIGHT, guiCamera);
        } else {
            guiViewport = new FitViewport(SCREEN_WIDTH, SCREEN_HEIGHT, guiCamera);
        }
...

然后(我有很多屏幕),在屏幕 i 中:

public ScreenLoader(Main main) {
    this.main = main;
    batch = new SpriteBatch();
    stage = new Stage();
    stage.setViewport(guiViewport);

..在这里我初始化并添加演员。并且不要忘记

@Override
public void render(float delta) {
    stage.act(delta);
    stage.draw();
© www.soinside.com 2019 - 2024. All rights reserved.