LibGDX ScrollPane显示黑屏

时间:2014-12-18 04:12:04

标签: java android libgdx scrollpane

情况:
首先,我是LibGDX中的新手。

我制作的游戏中玩家可以在屏幕上看到所有成就,因此我使用 ScrollPane 进行此操作。

成就以弹出形式显示(见下图)。成就列表将在这些红线内。

截图: enter image description here

问题:
问题是:当我向ScrollPane添加小部件时,屏幕仅显示黑屏

奇怪的是:
- 当我拖动 ScrollPane时,所有内容都正确呈现
- 但是当我放开时,屏幕再次变黑

我做了什么:
- 我发现任何演员(不仅仅是表格)我放入 ScrollPane参数会导致黑屏
- 如果我将其设置为 null ,则可以正常工作

代码:

public void show() {
    stage = new Stage();

    roomScreenUI = new RoomScreenUI();
    roomScreenUI.setName("RoomScreenUI");
    stage.addActor(roomScreenUI);

    roomScreenButton = new RoomScreenButton[5];
    for(int i=0; i<roomScreenButton.length; i++){
        roomScreenButton[i] = new RoomScreenButton(i+1, roomScreenUI.getScaleFactor());
        roomScreenButton[i].setName("RoomScreenButton");
        stage.addActor(roomScreenButton[i]);
    }

    roomScreenAchievementUI = new RoomScreenAchievementUI(roomScreenUI.getScaleFactor());
    roomScreenAchievementUI.setName("RoomScreenAchievementUI");
    stage.addActor(roomScreenAchievementUI);


    //----------------THE PROBLEM LIES HERE----------------//

    achievementContainer = new Table();

    scrollPane = new ScrollPane(achievementContainer);
    // scrollPane = new ScrollPane(null); <-- If I replace it with this line, it works fine

    //----------------THE PROBLEM LIES HERE----------------//

    achievementTable = new Table();
    achievementTable.setSize(roomScreenAchievementUI.getWidth() * 0.9f, roomScreenAchievementUI.getHeight() * 0.8f);
    achievementTable.setPosition(roomScreenAchievementUI.getX() + roomScreenAchievementUI.getWidth() / 2 - achievementTable.getWidth() / 2, roomScreenAchievementUI.getY() + roomScreenAchievementUI.getHeight() * 0.48f - achievementTable.getHeight() / 2);
    achievementTable.debug();
    achievementTable.add(scrollPane).expand().fill();
    achievementTable.setName("AchievementTable");
    stage.addActor(achievementTable);

    Gdx.input.setInputProcessor(stage);
}

public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // ... code omitted ... //

    stage.act(delta);
    stage.draw();
}

问题:
任何人对于发生了什么都有任何想法? 我该如何解决呢? Thx提前......

============================================ ======================================  的更新 =============================================== ===================================
经过几个小时的实验,我创建了一个更简单的项目,只有一个屏幕一个演员从头开始:

TestActor.java (这是演员)

Texture texture;
Sprite sprite;

public TestActor(){
    // I used the default generated image badlogic.jpg
    texture = new Texture(Gdx.files.internal("badlogic.jpg"));
    sprite = new Sprite(texture);
}

@Override
public void draw(Batch batch, float alpha){
    batch.draw(sprite, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

TestClass.java (这是屏幕)

Stage stage;
ScrollPane scrollPane;
Table outerTable, innerTable;
TestActor testActor;

@Override
public void show() {
    stage = new Stage();

    testActor = new TestActor();
    stage.addActor(testActor);

    //---------THE PROBLEM IS STILL HERE---------//

    innerTable = new Table();
    scrollPane = new ScrollPane(innerTable); // change it to null, it works

    //---------THE PROBLEM IS STILL HERE---------//

    outerTable = new Table();
    outerTable.setPosition(0, 0);
    outerTable.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    outerTable.debug();
    outerTable.add(scrollPane).fill().expand();
    stage.addActor(outerTable);
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act(delta);
    stage.draw();
}
  

在我调试时,我想出了一些可能是线索的东西:
   - 在第一帧,一切都正确呈现    - 从第二帧开始,屏幕变黑。

这是一个错误吗? 还是我误解了什么? 任何帮助将不胜感激......

2 个答案:

答案 0 :(得分:3)

以下是仍然遇到此错误的解决方法:try scrollpane.setFadeScrollBars(false);

答案 1 :(得分:2)

修改托盘此代码

示例:

public class TestActor extends Widget {
Texture texture;
Sprite sprite;

public TestActor(){
    // I used the default generated image badlogic.jpg
    texture = new Texture(Gdx.files.internal("badlogic.jpg"));
    sprite = new Sprite(texture);
}

@Override
public void draw(Batch batch, float parentAlpha){

    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);

    batch.draw(sprite, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

}