我在让存储在cree内存中的位置工作时遇到了一些麻烦。例如,这段代码:
var sources = creep.room.find(FIND_SOURCES);
if(creep.memory.sourceLocationIndex == null) {
creep.memory.sourceLocationIndex = Math.floor(Math.random() * sources.length);
}
return sources[creep.memory.sourceLocationIndex];
完美运行,但这段代码:
if(creep.memory.sourceLocation == null) {
var sources = creep.room.find(FIND_SOURCES);
var sourceLocationIndex = Math.floor(Math.random() * sources.length);
creep.memory.sourceLocation = sources[sourceLocationIndex];
}
return creep.memory.sourceLocation;
一旦小兵移动,似乎就失败了。发生这种情况有什么原因吗?我应该做些什么不同的事情?