2014-03-02 111 views
0

我试图为我的GUI元素使用LibGDX舞台,但我很难得到它正确呈现。现在,我试图在屏幕左下角呈现聊天窗口。Libgdx:阶段只渲染在(0,0)

这里是GUI对象的构造:

stage = new Stage(); 
stage.setCamera(controller.getCamera()); 

uiSkin = new Skin(Gdx.files.internal("res/gui/skin/uiskin.json")); 

Table table = new Table(uiSkin); 
stage.addActor(table); 

table.setSize(300, 260); 
table.setPosition(0, 0); 
table.setFillParent(false); 
table.bottom(); 
table.left(); 
table.pad(10); 

lblChatLabel = new Label("", uiSkin); 
lblChatLabel.setWrap(true); 

final ScrollPane scroll = new ScrollPane(lblChatLabel, uiSkin); 

txtChatBar = new TextField("do a chat", uiSkin); 
txtChatBar.setName("txtChatBar"); 

table.add(txtChatBar).width(300f); 
table.row(); 
table.add(scroll).expand().fill().colspan(4); 

txtChatBar.addListener(new InputListener() { 
    public boolean keyDown(InputEvent event, int keycode) { 
     if (keycode == Input.Keys.ENTER) { 
      sendMessage(); 
      // Close the chat bar 
      showChat = false; 
      return true; 
     } 

     return false; 
    } 
}); 

,这里是我的render()方法:

Log.debug("void", "x: " + stage.getCamera().position.x + ", y: " + stage.getCamera().position.y); 
stage.act(); 
for (Actor a : stage.getActors()) { 
    a.draw(spriteBatch, 1); 
} 

在另一段代码的其他地方,游戏中的镜头对象被翻译成以球员为中心:

camera.translate(targetPosition.x, targetPosition.y); 
camera.update(); 

这样说,聊天窗口呈现pr operly,但它只在0,0时这样做。我也改变了名为stage.draw(),而不是单独遍历Actors,但这会导致相同的问题。下面是说明这一问题的截图:

http://i.imgur.com/8uz5lV6.jpg

最后,我试图通过设置视手动平移阶段,但导致一个更离奇的问题。

float cx = controller.getCamera().getX(); 
float cy = controller.getCamera().getY(); 
float width = controller.getCamera().viewportWidth; 
float height = controller.getCamera().viewportHeight; 
stage.act(); 
stage.setViewport(width, height, true, cx, cy, width, height); 
stage.draw(); 

的形象在这里:

http://i.imgur.com/JpSLq1s.jpg

当然,我做错了什么,但我在这一点上没有任何想法。我会假设该阶段是在照相机翻译之后的,但似乎并非如此。欢迎任何建议!谢谢!

回答

1

我相信这个问题是这行代码:

stage.setCamera(controller.getCamera()); 

如果我读正确,你想聊天窗口总是从(0,0)呈现,无论身在何处的摄像头是屏幕。如果是这样的话,那么舞台就不应该和摄像机有任何关系,而摄像机会左右移动,这会让舞台在正确的位置被正确渲染变得更加复杂。

没有该行的代码,你应该能够只是调用

stage.act(); 
stage.draw(); 

,它应该工作的罚款。

+0

感谢您的回答。实际上,对于游戏中的坐标系统,它总是呈现为0,0,并且我希望它随着相机移动(上面的第一个图像显示了在0,0的框渲染,而玩家已经移动到别处)。也许这就是问题所在,因为GUI阶段需要根据窗口而不是游戏坐标进行渲染。 – Magicked

+0

啊,是的,这就是我的意思。上面的代码应该始终在窗口坐标中呈现为0,0。 – kabb