LibGDX - > LWJGL应用程序中的例外 - 无法转换为

时间:2014-09-16 10:52:57

标签: java libgdx

我希望你能帮助我,我希望我没有忽视任何相关的答案。

当我尝试运行DesktopLauncher.java类时,我得到了这个异常。

    Executing: gradle run

Configuration on demand is an incubating feature.
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:core:compileJava
:core:processResources UP-TO-DATE
:core:classes
:core:jar
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:desktop:compileJava
:desktop:processResources UP-TO-DATE
:desktop:classes
Exception in thread "LWJGL Application" java.lang.ClassCastException: com.bluemoon.game.MainGame cannot be cast to com.badlogic.gdx.InputProcessor
    at com.bluemoon.game.MainGame.create(MainGame.java:30)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
:desktop:run

BUILD SUCCESSFUL

Total time: 4.267 secs

我刚从this教程复制了我的代码。

这是核心文件中的MainGame.java

package com.bluemoon.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class MainGame extends ApplicationAdapter {
 Texture img;
 TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;

@Override
public void create () {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = new OrthographicCamera();
    camera.setToOrtho(false,w,h);
    camera.update();
    tiledMap = new TmxMapLoader().load("basic_map.tmx");
    tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
    Gdx.input.setInputProcessor((InputProcessor) this);
}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    tiledMapRenderer.setView(camera);
    tiledMapRenderer.render();
 }
}

这是我的DesktopLauncher.java

package com.bluemoon.game.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.bluemoon.game.MainGame;

public class DesktopLauncher {
  public static void main (String[] arg) {
    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();

            cfg.fullscreen = true;
            cfg.resizable = false;

    new LwjglApplication(new MainGame(), cfg);
}

}

当我运行DesktopLauncher.java时,程序被构建,然后屏幕变黑(可能是因为它进入全屏模式)。大约1-2秒后程序崩溃。

我正在使用gradle一个数据包管理系统,如果这很重要的话。

1 个答案:

答案 0 :(得分:1)

这是因为MainGame类没有实现InputProcessor类。它应该是:

public class MainGame extends ApplicationAdapter implements InputProcessor {

当然,您必须实现InputProcessor中的所有方法。