[英]Sending a keypress over a client / server
I am creating an Android game and have come across a problem. 我正在创建一个Android游戏,遇到了一个问题。 It is a simple Pong style game where a bat is moved left or right to block a ball.
这是一种简单的Pong风格的游戏,在其中左右移动蝙蝠来阻挡球。 However I am trying to control the bat via a UDP connection.
但是,我试图通过UDP连接来控制蝙蝠。 I have my server set up on my Android code and then a separate Java class for the client.
我在我的Android代码上设置了服务器,然后为客户端设置了单独的Java类。 At the minute I can only send strings or longs from the client to server.
目前,我只能从客户端向服务器发送字符串或long。 What I am trying to achieve is, on the client while it is waiting for an input if I press the left or right arrows then it will get this data, convert it to bytes and then send it to the server.
我要达到的目的是,在客户端等待输入时,如果我按向左或向右箭头,它将获取此数据,将其转换为字节,然后将其发送到服务器。 The server should then receive the data as a key press so that the bat image of the game will move in the appropriate direction.
然后,服务器应通过按键接收数据,以便游戏的蝙蝠图像将向适当的方向移动。 If anyone has any idea how to do this it would be greatly appreciated.
如果有人对如何执行此操作有任何想法,将不胜感激。 Thanks
谢谢
I recently created a game myself where I also needed to do some client-server synchronisation. 我最近自己创建了一个游戏,我还需要进行一些客户端-服务器同步。 I came across the KryoNet library which also works for Android.
我遇到了KryoNet库,该库也适用于Android。
http://code.google.com/p/kryonet/ http://code.google.com/p/kryonet/
It might be worth looking into it since it's not that hard to grasp 可能值得研究,因为它并不难掌握
Good luck on your game! 祝您比赛顺利!
You have to make your own "server codes." 您必须制作自己的“服务器代码”。 What I mean by that is you should send certain key words to the server to let it know that a specific action should be taken.
我的意思是,您应该向服务器发送某些关键字,以使其知道应采取特定的措施。 In Sun's tutorials on creating sockets, you're able to see that the connection does not close until a keyword has been sent to the server.
在Sun有关创建套接字的教程中,您可以看到直到将关键字发送到服务器后,连接才会关闭。 You are able to do the same with this.
您可以这样做。
The Server and Client Example, Telling When The Server Should Close 服务器和客户端示例,告诉服务器何时应关闭
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.