我目前正在开发一个小的 dungon 爬虫。今天我尝试实现本地多人游戏模式......当我的duengon创建时,它还会在每个房间中创建一些敌人,大多数时候总共有大约100个敌人。首先我做了播放器部分,看起来像这样:
gameScreen.player.update();
//Update position
if(gameScreen.player.networkPosition.x != gameScreen.player.position.x){
//Send the player's X value
PacketUpdateX packet = new PacketUpdateX();
packet.x = gameScreen.player.position.x;
network.client.sendUDP(packet);
gameScreen.player.networkPosition.x = gameScreen.player.position.x;
}
if(gameScreen.player.networkPosition.y != gameScreen.player.position.y){
//Send the player's Y value
PacketUpdateY packet = new PacketUpdateY();
packet.y = gameScreen.player.position.y;
network.client.sendUDP(packet);
gameScreen.player.networkPosition.y = gameScreen.player.position.y;
}
if(gameScreen.player.netState != gameScreen.player.state){
PacketUpdateState packet = new PacketUpdateState();
packet.state = gameScreen.player.state;
network.client.sendUDP(packet);
gameScreen.player.netState = gameScreen.player.state;
}
所以它应该只在玩家改变他的位置时发送数据包更新。在那之后,我实现了一个可加入的播放器。连接正常,一切都很好,直到我移动播放器对角线。比加入的玩家滞后很严重。首先我认为那是因为我的播放器每秒移动 200f 像素。但是当我每秒做 5 个像素时,它也会滞后:/ 我不知道为什么。这是我的玩家移动代码:
if(Gdx.input.isKeyPressed(Keys.A) && moveDirections[0]){
position.x -= Gdx.graphics.getDeltaTime() * 200f;
state = 1;
}
if(Gdx.input.isKeyPressed(Keys.D) && moveDirections[1]){
position.x += Gdx.graphics.getDeltaTime() * 200f;
state = 2;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.W) && moveDirections[2]){
position.y += Gdx.graphics.getDeltaTime() * 200f;
state = 3;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.S) && moveDirections[3]){
position.y -= Gdx.graphics.getDeltaTime() * 200f;
state = 4;
}
有谁知道,为什么它会滞后?或者一个想法如何找到问题?那是我第一次使用 kryonet :/ 感谢您的关注 :)