简体   繁体   English

如何使用Kryonet客户端处理游戏对象更新

[英]How to handle game object updates with kryonet client

I working on a simple multiplayer game in java2d that handles 4 players at a time. 我正在用java2d开发一个简单的多人游戏,一次可同时处理4个玩家。 Here is a code sample that receives data from the server. 这是一个从服务器接收数据的代码示例。

server.addListener(new Listener() {
   public void received (Connection connection, Object object) {

   }
});

My question is: when data it received such as the location of another player, how should I update it? 我的问题是:当收到数据时(例如另一个播放器的位置),我应该如何更新它? Do I statically reference the game class an access it's player data, or do I pass the player object to the client class at the start? 我是静态引用游戏类对其玩家数据的访问,还是一开始将玩家对象传递给客户端类? Is there a good way of doing this? 有这样做的好方法吗?

Thanks! 谢谢!

Well if you have a balloon for example that needs to show up on every ones screen then maybe you should have a static list that holds these objects and send that whole list or loop through it all and send each separately to the client and then on the client side it can have a static list to that it renders from every game loop. 好吧,例如,如果您有一个气球需要在每个屏幕上显示,那么也许您应该有一个包含这些对象的静态列表,然后发送整个列表或遍历所有列表,然后将其分别发送到客户端,然后在客户端在客户端,它可以具有一个静态列表,该列表可以在每个游戏循环中呈现。

Or you can pass the current game object that you need to update. 或者,您可以传递需要更新的当前游戏对象。 For example you can extract the listener to external class instead of nesting it in the serever.addListener method. 例如,您可以将侦听器提取到外部类,而不是将其嵌套在serever.addListener方法中。 LIke this 像这样

package BTDOnlineToolKit;

import com.esotericsoftware.kryonet.*;

import packets.GeneralPackets.Ping;

public class NetworkHandler extends Listener {

    private Client clientObject;

    public NetworkHandler(Client clientObject) {
        this.clientObject = clientObject;
    }

    @Override
    public void connected(Connection connection) {
        clientObject.updateReturnTripTime();
        connection.updateReturnTripTime();
    }

    @Override
    public void disconnected(Connection connection) {
        super.disconnected(connection);
    }

    @Override
    public void idle(Connection connection) {
        super.idle(connection);
    }

    @Override
    public void received(Connection con, Object packet) {
        if (packet instanceof Ping) {
            Ping ping = (Ping) packet;
            if (ping.returned) {

            }
        }
    }

}

NetworkHandler netHandle = new NetworkHandler(Anyobjects you need it to parse);
Server.addListener(netHandle);

Dont forget you can also update the objects you need to update manully by making a method for that in the network handler. 不要忘记,您还可以通过在网络处理程序中为该对象创建方法来更新需要手动更新的对象。

And construct it before you start the server with all the things you need the connection to update for example you can pass a label object that shows ping or a enemy player object which you need to update atrributes of. 并在启动服务器之前用所有需要连接的事物来构造它,例如,您可以传递一个显示ping的标签对象或一个您需要更新属性的敌人玩家对象。 But like i said You can have a static list to store those values but this is more scalable. 但是就像我说的那样,您可以有一个静态列表来存储这些值,但这更具可伸缩性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM