无头/CLI LibGDX [英] Headless/CLI LibGDX

查看:90
本文介绍了无头/CLI LibGDX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为小型LibGDX驱动的游戏在服务器端进行编码,但偶然发现了一个问题.每次尝试使用任何Gdx.files.*方法时,都会遇到NullPointerException.

I'm coding the server-side for a small LibGDX powered game, and have stumbled across an issue. Every time I try and use any Gdx.files.* methods, I'm met with a NullPointerException.

显然,这是因为我没有实现ApplicationListener,所以LibGDX尚未初始化.

Apparently this is because I'm not implementing the ApplicationListener, so LibGDX has not been initialized.

有没有办法以无头/CLI方式初始化LibGDX?我需要能够在服务器端加载TiledMap对象.

Is there any way of initializing LibGDX in a headless/CLI way? I need to be able to load TiledMap objects on the server-side.

MapLoader(Request request)
{
    TiledMap tmp = new TmxMapLoader().load("maps/" + request.name + ".tmx");
}

线程服务器"中的异常java.lang.NullPointerException在 com.tester.Example.server.ExampleServer $ 2.received(MapLoader.java:83) 在com.esotericsoftware.kryonet.Server $ 1.received(Server.java:60)在 com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246) 在com.esotericsoftware.kryonet.Server.update(Server.java:202)处 com.esotericsoftware.kryonet.Server.run(Server.java:350)在 java.lang.Thread.run(Thread.java:722)

Exception in thread "Server" java.lang.NullPointerException at com.tester.Example.server.ExampleServer$2.received(MapLoader.java:83) at com.esotericsoftware.kryonet.Server$1.received(Server.java:60) at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246) at com.esotericsoftware.kryonet.Server.update(Server.java:202) at com.esotericsoftware.kryonet.Server.run(Server.java:350) at java.lang.Thread.run(Thread.java:722)

推荐答案

我不建议在无头环境中使用libGDX,它不是设计用于这种方式的,将来您可能会遇到一些问题libGDX团队更改了框架.但是,正如Rod指出的那样,完全有可能这样做,下面是您如何实现的摘要.要初始化Gdx.files全局变量,您将需要在后端程序包中创建一个类并自行设置全局变量:

I would not recommend using libGDX for a headless environment, it was simply not designed to be used that way and you may encounter issues in the future as the libGDX team changes the framework. However, as Rod pointed out, it is entirely possible to do so and below is a snippet of how you would go about it. To initialize the Gdx.files global you will need to create a class in the backend package and setup the globals yourself:

package com.badlogic.gdx.backends.lwjgl;

import com.badlogic.gdx.Gdx;

public class Headless {
    public static void loadHeadless() {
        LwjglNativesLoader.load();
        Gdx.files = new LwjglFiles();
    }
}

其余的应该很简单.只需调用Headless.loadHeadless();首先,您应该能够使用所需框架的一部分.

The rest should be fairly simple. Just call Headless.loadHeadless(); at the start and then you should be able to use parts of the framework you require.

正如我之前所说,我不建议这样做,但是我还没有找到将libgdx与客户端/服务器体系结构一起使用的任何好的解决方案.

As I stated before, I would not suggest doing this, but I haven't found any nice solutions for using libgdx with a client/server architecture.

修改:

前不久(我最初写了这个答案之后),libgdx添加了

A little while back (after I wrote this answer originally) libgdx added a headless backend which is designed for this kind of purpose. It is the correct and proper way to use libgdx in a headless environment and works very well for creating a server with libgdx.

这篇关于无头/CLI LibGDX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆