我正在制作一个小型 2 人游戏,它将通过网络与演员一起工作。每个客户端都会向服务器发送一条消息以加入,我想在那时保留对发件人的引用,但是当第二个玩家加入时,它会覆盖我对第一个玩家的引用:
case class JoinMsg
class Server(port: Int) extends Actor {
var client1: OutputChannel[Any] = null
var client2: OutputChannel[Any] = null
def act() {
alive(port)
register('Server, self)
while (true) {
receive {
case JoinMsg =>
if (client1 == null) {
Console.println("got player 1")
client1 = sender
client1 ! new Msg("Waiting for player 2")
} else if (client2 == null) {
Console.println("got player 2")
client2 = sender
Console.println("blatted client1?: "+(client1 == client2))//true
client1 ! new Msg("hi")
client2 ! new Msg("hi")
}
}
}
}
}
解决这个问题的正确方法是什么?谢谢。