問題描述
我正在為我的游戲創(chuàng)建關(guān)卡編輯器,但我在使用帶有 JFrame 的 LwjglCanvas 時遇到了問題.我使用 JFrame(不是 LwjglFrame)來保持引擎和關(guān)卡編輯器盡可能獨立.我有兩個 JAR:WorldEditor.jar 和 GameEngine.jar.在 WorldEditor 中,我有一個名為test"的按鈕,假設(shè)加載 GameEngine.jar(如果尚未加載)并啟動(如果已加載則重新啟動)到應(yīng)用程序主框架中.實際上,我所做的是將 WorldEditor 游戲容器(例如 JFrame 內(nèi)的 JPanel)注入游戲應(yīng)用程序,并使用 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
從未被調(diào)用(由于應(yīng)用程序在可見之前不可見,我錯了嗎?)我已經(jīng)嘗試了很長時間,但沒有結(jié)果......
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 發(fā)布到?jīng)]有運行的游戲循環(huán).我會嘗試從 MyGame 獲取一個組件并添加它.如果你返回一個組件,你就沒有一個 dep.到 LwjglCanvas.這不是很好,因為現(xiàn)在 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.
這篇關(guān)于如何在 Swing 應(yīng)用程序中正確放置 libgdx 應(yīng)用程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!