我对LibGDX有疑问。这是空白项目女巫只使用套接字连接到其他程序。当我尝试启动它时,它只是挂起程序没有响应。套接字连接已建立并正常工作。谢谢你的帮助~BeefEX
package com.beefcodes.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
BitmapFont font;
String lastMsg = "test";
Socket socket;
PrintWriter output;
BufferedReader input;
@Override
public void create () {
batch = new SpriteBatch();
font = new BitmapFont();
try {
socket = new Socket("localhost", 540);
} catch (IOException e) {
e.printStackTrace();
}
}
public void render () {
if (input == null && output == null) {
try {
output = new PrintWriter(socket.getOutputStream(), true);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
String s;
try {
if ((s = input.readLine()) != null)
lastMsg = s;
} catch (IOException e) {
e.printStackTrace();
}
font.draw(batch, lastMsg, 100, 100);
batch.end();
}
}
答案 0 :(得分:0)
从渲染方法
中删除它 if (input == null && output == null) {
try {
output = new PrintWriter(socket.getOutputStream(), true);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
您不希望每秒创建60个PrintWriters和60个BufferedReaders。此外,我甚至没有看到这些被使用的地方。在任何情况下,也许这可以在create方法中完成一次。