【发布时间】:2019-12-19 17:41:12
【问题描述】:
我正在 libgdx 中制作在线游戏。我正在使用 socket.io 作为服务器,我正在使用房间。但是我在屏幕更改方面遇到了问题。我有两个屏幕类,我只连接到服务器一次来自城镇等级(从城镇开始的玩家)。当玩家去武器店时,我正在向玩家发送 getPlayers 方法。但是 getPlayers 方法在城里和武器店调用。当玩家在武器店类中时,我不想在城镇类中调用 getPlayers 方法。
我在 Multiplayer 类中定义的套接字变量,用于随处访问:
public class Multiplayer extends Game {
public static Socket socket;
...
@Override
public void create () {
game = this;
try {
socket = IO.socket("http://localhost:8080");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
@Override
public void render () {
super.render();
}
}
镇级:
public Town() {
...
connectToRoom();
configSocketEvents();
}
private void connectToRoom() {
try {
if (!socket.connected())
socket.connect();
JSONObject data = new JSONObject();
try {
data.put("newroom", "town");
socket.emit("joinRoom", data);
} catch (JSONException e) {
Gdx.app.log("SOCKET.IO", "Error sending update data!");
}
} catch(Exception e) {
System.out.println(e);
}
}
private void configSocketEvents() {
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Gdx.app.log("SocketIO", "Connected");
}
}).on("getPlayers", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("gs getPlayers");
JSONArray objects = (JSONArray) args[0];
try {
for (int i = 0; i < objects.length(); i++) {
FriendlyPlayer coopPlayer = new FriendlyPlayer(playerTex, world, game, 1220, 1100, 24, 32);
Vector2 position = new Vector2();
Float vx, vy;
Float bx, by;
position.x = ((Double) objects.getJSONObject(i).getDouble("x")).floatValue();
position.y = ((Double) objects.getJSONObject(i).getDouble("y")).floatValue();
vx = ((Double) objects.getJSONObject(i).getDouble("vx")).floatValue();
vy = ((Double) objects.getJSONObject(i).getDouble("vy")).floatValue();
bx = ((Double) objects.getJSONObject(i).getDouble("bx")).floatValue();
by = ((Double) objects.getJSONObject(i).getDouble("by")).floatValue();
String username = objects.getJSONObject(i).getString("username");
Integer level = objects.getJSONObject(i).getInt("level");
coopPlayer.setPosition(position.x, position.y);
coopPlayer.vx = vx;
coopPlayer.vy = vy;
coopPlayer.bx = bx;
coopPlayer.by = by;
coopPlayer.username = username;
coopPlayer.level = level;
friendlyPlayers.put(objects.getJSONObject(i).getString("id"), coopPlayer);
coopPlayer.update(Gdx.graphics.getDeltaTime());
}
} catch (JSONException e) {
}
}
});
}
武器商店类:
public WeaponShop() {
...
connectToRoom();
configSocketEvents();
}
private void connectToRoom() {
try {
JSONObject data = new JSONObject();
try {
data.put("newroom", "weaponshop");
socket.emit("joinRoom", data);
} catch (JSONException e) {
Gdx.app.log("SOCKET.IO", "Error sending update data!");
}
} catch(Exception e){
System.out.println(e);
}
}
private void configSocketEvents() {
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Gdx.app.log("SocketIO", "Connected");
}
}).on("getPlayers", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("ws getPlayers");
JSONArray objects = (JSONArray) args[0];
try {
for (int i = 0; i < objects.length(); i++) {
FriendlyPlayer coopPlayer = new FriendlyPlayer(playerTex, world, game, 56, 195, 24, 32);
Vector2 position = new Vector2();
Float vx, vy;
Float bx, by;
String username;
position.x = ((Double) objects.getJSONObject(i).getDouble("x")).floatValue();
position.y = ((Double) objects.getJSONObject(i).getDouble("y")).floatValue();
vx = ((Double) objects.getJSONObject(i).getDouble("vx")).floatValue();
vy = ((Double) objects.getJSONObject(i).getDouble("vy")).floatValue();
bx = ((Double) objects.getJSONObject(i).getDouble("bx")).floatValue();
by = ((Double) objects.getJSONObject(i).getDouble("by")).floatValue();
username = objects.getJSONObject(i).getString("username");
coopPlayer.setPosition(position.x, position.y);
coopPlayer.vx = vx;
coopPlayer.vy = vy;
coopPlayer.bx = bx;
coopPlayer.by = by;
coopPlayer.username = username;
friendlyPlayers.put(objects.getJSONObject(i).getString("id"), coopPlayer);
coopPlayer.update(Gdx.graphics.getDeltaTime());
}
} catch (JSONException e) {
}
}
});
}
还有我在服务器中的 joinRoom 方法:
socket.on('joinRoom', function(data) {
oldRoom = socket.room;
socket.room = data.newroom;
socket.leave(oldRoom);
socket.join(socket.room);
socket.emit("createPlayer");
socket.to(oldRoom).emit('playerDisconnected', { id: socket.id });//socket.broadcast.to(oldRoom).emit('playerDisconnected', { id: socket.id });
socket.to(socket.room).emit('newPlayer', { id: socket.id });//socket.broadcast.to(socket.room).emit('newPlayer', { id: socket.id });
if(oldRoom == 'town') {
for(var i = 0; i < players.length; i++) {
if(players[i].id == socket.id) {
players.splice(i, 1);
}
}
}
if(oldRoom == 'weaponshop') {
for(var i = 0; i < weaponshopPlayers.length; i++) {
if(weaponshopPlayers[i].id == socket.id) {
weaponshopPlayers.splice(i, 1);
}
}
}
if(oldRoom == 'potionshop') {
for(var i = 0; i < potionshopPlayers.length; i++) {
if(potionshopPlayers[i].id == socket.id) {
potionshopPlayers.splice(i, 1);
}
}
}
if(socket.room == 'town') {
socket.emit('getPlayers', players);
socket.emit('getMonsters', monsters);
players.push(new player(socket.id, "", 0, "town", 1220 / 100, 1100 / 100, 0, 0, 0, 0));
}
if(socket.room == 'weaponshop') {
socket.emit('getPlayers', weaponshopPlayers);
weaponshopPlayers.push(new player(socket.id, "", 0, "weaponshop", 56 / 100, 195 / 100, 0, 0, 0, 0));
}
if(socket.room == 'potionshop') {
socket.emit('getPlayers', potionshopPlayers);
potionshopPlayers.push(new player(socket.id, "", 0, "potionshop", 56 / 100, 195 / 100, 0, 0, 0, 0));
}
console.log("Player (" + socket.id + ") switched room");
});
【问题讨论】:
标签: java node.js socket.io libgdx