【发布时间】:2020-07-30 02:27:34
【问题描述】:
所以开始我对使用 libGDX 有点陌生。我有本书 Java Game Development with LibGDX,我正在尝试运行第 2 章中的示例程序。我正在使用 BlueJ。代码如下:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.Game;
public class StarfishCollectorAlpha extends Game
{
private SpriteBatch batch;
private Texture turtleTexture;
private float turtleX;
private float turtleY;
private Rectangle turtleRectangle;
private Texture starfishTexture;
private float starfishX;
private float starfishY;
private Rectangle starfishRectangle;
private Texture oceanTexture;
private Texture winMessageTexture;
private boolean win;
public void create()
{
batch = new SpriteBatch();
turtleTexture = new Texture( Gdx.files.internal("assets/turtle-1.png") );
turtleX = 20;
turtleY = 20;
turtleRectangle = new Rectangle( turtleX, turtleY,
turtleTexture.getWidth(), turtleTexture.getHeight() );
starfishTexture = new Texture( Gdx.files.internal("assets/starfish.png") );
starfishX = 380;
starfishY = 380;
starfishRectangle = new Rectangle( starfishX, starfishY,
starfishTexture.getWidth(), starfishTexture.getHeight() );
oceanTexture = new Texture( Gdx.files.internal("assets/water.jpg") );
winMessageTexture = new Texture( Gdx.files.internal("assets/you-win.png") );
win = false;
}
public void render()
{
// check user input
if (Gdx.input.isKeyPressed(Keys.LEFT))
turtleX--;
if (Gdx.input.isKeyPressed(Keys.RIGHT))
turtleX++;
if (Gdx.input.isKeyPressed(Keys.UP))
turtleY++;
if (Gdx.input.isKeyPressed(Keys.DOWN))
turtleY--;
// update turtle rectangle location
turtleRectangle.setPosition(turtleX, turtleY);
// check win condition: turtle must be overlapping starfish
if (turtleRectangle.overlaps(starfishRectangle))
win = true;
// clear screen
Gdx.gl.glClearColor(0,0,0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// draw graphics
batch.begin();
batch.draw( oceanTexture, 0, 0 );
if (!win)
batch.draw( starfishTexture, starfishX, starfishY );
batch.draw( turtleTexture, turtleX, turtleY );
if (win)
batch.draw( winMessageTexture, 180, 180 );
batch.end();
}
}
和
import com.badlogic.gdx.Game;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
public class LauncherAlpha
{
public static void main (String[] args)
{
Game myGame = new StarfishCollectorAlpha();
LwjglApplication launcher = new LwjglApplication( myGame, "Starfish Collector", 800, 600 );
}
}
我收到错误:
Exception in thread "LWJGL Application" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glGetError(GL11.java:1299)
at org.lwjgl.opengl.Util.checkGLError(Util.java:57)
at org.lwjgl.opengl.WindowsContextImplementation.setSwapInterval(WindowsContextImplementation.java:113)
at org.lwjgl.opengl.ContextGL.setSwapInterval(ContextGL.java:232)
at org.lwjgl.opengl.DrawableGL.setSwapInterval(DrawableGL.java:86)
at org.lwjgl.opengl.Display.setSwapInterval(Display.java:1129)
at org.lwjgl.opengl.Display.setVSyncEnabled(Display.java:1142)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setVSync(LwjglGraphics.java:558)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
以前有没有人遇到过这个错误,有没有人知道解决方法?我知道这与多线程有关,但是我没有打开任何新线程。就像我说的,我对使用 libGDX 还很陌生,所以我不是 100% 了解它是如何工作的。
谢谢
【问题讨论】:
-
这是整个错误吗?应该有更多的错误信息对我们有帮助,但我怀疑线程存在问题,以及您如何在主线程中启动游戏,或者 OpenGL 上下文尚未初始化。
-
是的,这就是整个错误消息。抱歉,我没有提供更多信息,我只是不知道有什么帮助
-
不使用 BlueJ 会怎样?
-
首先,尝试在 Eclipse 或 NetBeans IDE 中运行它,其次 - 检查其他示例,尝试从另一个线程访问视频内存时出现常见的上下文问题,是的,但有时它无法创建上下文,因为问题在驱动程序中