首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让Screep找到资源?

如何让Screep找到资源?
EN

Stack Overflow用户
提问于 2017-01-18 05:16:02
回答 4查看 1.9K关注 0票数 3

在Screep中,我这段代码不能工作:

代码语言:javascript
复制
var sources = creep.room.find(Game.FIND_SOURCES_ACTIVE);

它是这样写的:

代码语言:javascript
复制
Cannot read property 'find' of undefined

我一直在四处寻找,但找不到任何其他的方法来寻找来源。

此外,我注意到大多数人的代码都不能工作,甚至教程的代码在放入真正的游戏中时也不再工作。

EN

回答 4

Stack Overflow用户

发布于 2017-02-28 17:26:32

我不能完全确定你的问题,因为我没有你的完整代码,但一个问题可能是creep没有定义。

您需要在代码中的某处定义creep,例如循环遍历游戏或房间中的每个爬虫。

代码语言:javascript
复制
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模块中,你应该有一些东西来定义你的挖掘器操作。

代码语言:javascript
复制
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;

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2020-01-29 15:33:49

爬虫有一些..。机制,当你在每个游戏节拍之间共享你的数据。如果您在全局内存对象中存储任何内容,您的数据将丢失其所有原型。若要恢复原型,请使用Object.setPrototypeOf(爬行,Creep.prototype)或根据爬行id创建新的爬行对象。

票数 1
EN

Stack Overflow用户

发布于 2017-01-18 05:20:51

我想你要找的是:

代码语言:javascript
复制
var sources = creep.pos.findClosestByRange(Game.SOURCES);

代码语言:javascript
复制
var sources = creep.pos.findClosestByPath(Game.SOURCES);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41707132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档