对于简单的 2D 游戏,我需要我的客户端程序始终将坐标发送到服务器。
因此,我创建了一个测试,看看我是否可以通过从服务器发送和检索值来使两个玩家具有相同的速度。
package main;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataSender implements Runnable{
private DataInputStream fromServer;
private DataOutputStream toServer;
Player player;
Player opponent;
public DataSender(DataInputStream fromServer, DataOutputStream toServer, Player player, Player opponent){
this.fromServer = fromServer;
this.toServer = toServer;
this.player = player;
this.opponent = opponent;
}
@Override
public void run() {
while(true){
try {
toServer.writeInt(player.velX);
} catch (IOException e) {
e.printStackTrace();
}
try {
opponent.velX = fromServer.readInt();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
循环仅在while(true)
线程创建时执行一次。如何建立恒定的数据流?