我正在尝试创建一个多人游戏,如果大厅还没有满员,那么主机将拥有AI飞船,这些飞船会将其动作发送到服务器,服务器将其“动作”广播到客户端游戏中的空白飞船。播放过程中会伴随着“ i”(即AIShips标识符),以便告知正确的飞船在客户端游戏中移动。
以下是使用顺序的代码:
初始发射:(AIShip.java)
JSONObject data = new JSONObject();
try {
data.put("i",identifier); //Identifier is an int set once in the constructor
gameScreen.spaceSoccer.socket.emit("moveAIShipForward",data);
} catch (JSONException e){
e.printStackTrace();
}
我的服务器:(index.js)
socket.on("moveAIShipForward",function(i) {
socket.broadcast.emit("moveAIShipForward",{id: socket.id,i: i})
});
对广播的响应:(SpaceSoccer.java)
.on("moveAIShipForward",new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject data = (JSONObject) args[0];
try {
int i = data.getInt("i"); //error
gameScreen.AIShip[i].moveBodyForward();
} catch (JSONException e) {
e.printStackTrace();
}
});
错误:W / System.err:org.json.JSONException:无法将类型为org.json.JSONObject的i的值{“ i”:0}转换为int W / System.err:位于org.json.JSON.typeMismatch(JSON.java:100)