是否可以从Node.js中的事件发射器“退订”?
我正在创建一个小型游戏,其中有多个地图,玩家可以进出。 每个地图都扩展了EventEmitter类,并发出游戏事件,例如聊天,玩家移动,其他玩家进入和退出等。
看起来像...
class Map extends EventEmitter {
//other code..
enter(player) {
this.addListener('chat', handlePlayerChat.bind(player))
this.addListener('move', handlePlayerMove.bind(player))
this.addListener('attack', handlePlayerAttack.bind(player))
//and so on..
}
exit(player) {
//player is going to another map
//here i want to remove player's all subscriptions to 'this' map so
//it can listen to events from the new map
this.removeListener('chat', handlePlayerChat.bind(player)); //this doesn't work..
}
}
不确定这是否是正确的方法。
我还有一个“世界”类,所有“地图”都属于该类。我应该改为在世界一流的赛事中发光吗?