在Screeps,我這個代碼不工作:如何使screep查找來源?
var sources = creep.room.find(Game.FIND_SOURCES_ACTIVE);
它這樣說:
Cannot read property 'find' of undefined
我一直在四處尋找,並不能找到任何其他方式尋找貨源。
另外我注意到,大多數其他人的代碼不起作用,甚至教程的代碼不再適用於真正的遊戲。
在Screeps,我這個代碼不工作:如何使screep查找來源?
var sources = creep.room.find(Game.FIND_SOURCES_ACTIVE);
它這樣說:
Cannot read property 'find' of undefined
我一直在四處尋找,並不能找到任何其他方式尋找貨源。
另外我注意到,大多數其他人的代碼不起作用,甚至教程的代碼不再適用於真正的遊戲。
我知道你在尋找的是:
var sources = creep.pos.findClosestByRange(Game.SOURCES);
或
var sources = creep.pos.findClosestByPath(Game.SOURCES);
我剛剛得到: TypeError:無法讀取undefined屬性'findClosestByPath'undefined –
,因爲我沒有你完整的代碼走下車,我不能完全確定您的問題只有一個問題可能是creep
未定義。
你需要在你的代碼中的某個地方定義creep
,比如for循環來遍歷遊戲或房間中的每一個小兵。
var roleMiner = require('role.miner') // role.miner being the module name for miner actions
for(var name in Game.creeps) {
var creep = Game.creeps[name];
//
// do whatever you wish with the current selected creep.
//
// most of the time you will call a module similar to what the tutorials suggest and put your actions for it in there
//
if(creep.memory.role == 'miner'){
roleMiner.run(creep); // passes the current selected creep to the run function in the module
}
}
因此,在您的roleMiner模塊中,您將擁有定義礦工操作的內容。
var roleMiner = {
run: function(creep) {
// this one returns an array of the sources that are in the room with the creep
var sourcesRoom = creep.room.find(FIND_SOURCES);
// this one returns the source object which is closest to the creeps positon
var sourcesClose = creep.pos.findClosestByRange(FIND_SOURCES);
}
}
module.exports = roleMiner;
希望這會有所幫助。
IM一個新的球員,不知道我的代碼是有效的,我想找到方法將是這樣的:
var sources = creep.room.find(FIND_SOURCES_ACTIVE)
蠕變會去主動資源來收割機。
你能提供更多的上下文嗎?有可能'creep'甚至可能不是'Creep'的一個實例,這可能是'creep.room'未定義的原因。 –