3

在 Screeps 中,我这段代码不起作用:

var sources = creep.room.find(Game.FIND_SOURCES_ACTIVE);

它是这样说的:

Cannot read property 'find' of undefined

我一直在环顾四周,找不到任何其他方式来寻找资源。

此外,我注意到大多数其他人的代码都不起作用,甚至教程的代码在放入真实游戏时也不再起作用。

4

4 回答 4

1

我不能完全确定您的问题,因为我没有完整的代码可以解决,但一个问题可能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;

希望这可以帮助。

于 2017-02-28T09:26:32.087 回答
1

Screeps 在每个游戏滴答之间共享您的数据时有一些...机制。如果您在全局 Memory 对象中存储任何东西,您的数据将丢失其所有原型。恢复你的原型使用 Object.setPrototypeOf(creep,Creep.prototype) 或者从你的creep id创建新的Creep对象。

于 2020-01-29T07:33:48.547 回答
0

我是新玩家,不确定我的代码是否有效,我认为 find 方法是这样的:

var sources = creep.room.find(FIND_SOURCES_ACTIVE)

蠕变将前往收割机的活动资源。

于 2017-09-21T07:51:06.293 回答
0

我认为您正在寻找的是:

var sources = creep.pos.findClosestByRange(Game.SOURCES);

或者

var sources = creep.pos.findClosestByPath(Game.SOURCES);
于 2017-01-17T21:20:50.890 回答