2

鉴于我在房间里有一个小兵,除了“Game.creeps.creep.room.find(Game.)”之外还有其他方法可以获取房间中的对象吗?

.room.sources 之类的东西?

因为我目前正在使用这个:

var creep;
var target;
creep = Game.creeps.Creep1;
if(!creep.memory.target) {
  target = creep.pos.findNearest(Game.SOURCES,{filter:function(s) { return s.energy > 0; }});
  creep.memory.target = target.id;
}
else{
  target = creep.room.find(Game.SOURCES,{filter:function(s) { return s.id == creep.memory.target; }});
}

由于过滤器,这似乎可能有点慢。

4

2 回答 2

2

Room.find()是目前遍历房间对象的唯一方法。

顺便说一句,energy > 0您可以简单地使用Game.SOURCES_ACTIVE常量而不是检查:

于 2014-11-24T07:40:51.410 回答
0

万一有人读到这篇文章并感到困惑,自从提出/回答了这个问题以来,添加了一种新方法。Game.getObjectById()

在上面的代码中,您可以将 else 块中的语句替换为:

target = Game.getObjectById(creep.memory.target);
于 2014-12-10T01:33:34.197 回答