如何在 Swing 应用程序中正确放置 libgdx 应用程序?
我正在为我的游戏创建关卡编辑器,但我在使用带有 JFrame 的 LwjglCanvas 时遇到了问题.我使用 JFrame(不是 LwjglFrame)来保持引擎和关卡编辑器尽可能独立.我有两个 JAR:WorldEditor.jar 和 GameEngine.jar.在 WorldEditor 中,我有一个名为test"的按钮,假设加载 GameEngine.jar(如果尚未加载)并启动(如果已加载则重新启动)到应用程序主框架中.实际上,我所做的是将 WorldEditor 游戏容器(例如 JFrame 内的 JPanel)注入游戏应用程序,并使用 Gdx.app.postRunnable
将 lwjglcanvas 添加到注入的游戏容器中:
I'm creating a level editor for my game, and I ave a problem using LwjglCanvas with a JFrame. I use a JFrame (not a LwjglFrame) to keep engine and level editor as independent as possible.
I have two JARs: WorldEditor.jar, and GameEngine.jar. Inside WorldEditor, I have a button called "test", that is suppose to load GameEngine.jar (if not already loaded) and launch (resart it if already loaded) it into the application main frame.
Actually, what I do is injecting the WorldEditor game container (a JPanel inside the JFrame for example) to the game app, and use Gdx.app.postRunnable
to add the lwjglcanvas to the injected game container :
世界编辑端:
JPanel _gameContainer = new JPanel(); // is inside a JFrame
MyGame game = loadGame(_gameContainer); // load the GameEngine JAR, and retrive the game
GameEngine 方面:
GameEngine side:
// container is the _gamecontainer of above
public void createGame(final Container gameContainer)
{
LwjglCanvas canvas = new LwjglCanvas(myapp, myconfig);
Gdx.app.postRunnable(new Runnable()
{
public void run()
{
gameContainer.add(canvas.getCanvas());
}
});
}
事实是 postRunnable
从未被调用(由于应用程序在可见之前不可见,我错了吗?)我已经尝试了很长时间,但没有结果......
The fact is that the postRunnable
is never called (due to the fact that the app doesn't before being visible, am I wrong ?)
I have been trying for a long time but no result ...
有人知道我可以做些什么来解决这个问题吗?或者至少是另一种(让我们说更简单)的方法来做到这一点?
Does someone have an idea of what I could do to fix this problem ? Or a least another (let's say easier) method to do that ?
推荐答案
你必须使用 SwingUtilites.invokelater 因为 postRunnable 发布到没有运行的游戏循环.我会尝试从 MyGame 获取一个组件并添加它.如果你返回一个组件,你就没有一个 dep.到 LwjglCanvas.这不是很好,因为现在 MyGame 界面有一个 dep.挥杆,但值得一试,看看它是否能解决你的问题.
You have to use SwingUtilites.invokelater because postRunnable posts to the game loop which is not running. I would try to get a Component from MyGame and add this. If you return a Component you don't have a dep. to the LwjglCanvas. It's not that nice because now the MyGame Interface has a dep. to swing but it's worth a shot to see if its solves your problem.
相关文章