我正在开发一个小duengon爬行器。今天我试图实现一个本地的多人模式...当我的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合作:/感谢您的关注:))