4

我有教程中的以下代码,除了在模拟模式下它不起作用..

if(creep.memory.role == 'builder') {

    if(creep.energy === 0) {
        creep.moveTo(Game.spawns.Spawn1);
        Game.spawns.Spawn1.transferEnergy(creep);
    }
    else {
        var targets = creep.room.find(Game.CONSTRUCTION_SITES);
        if(targets.length) {
            creep.moveTo(targets[0]);
            creep.build(targets[0]);
        }
    }
}

在控制台中打印“目标”的结果,响应未定义。谁能解释“CONSTRUCTION_SITES”常量是如何工作的以及如何在房间内找到它们?

4

3 回答 3

4

The code looks right to me, it seems that you don't actually have any constructions set for the builders to build. Did you use the "Construct" menu on the left to build a road, for example?

于 2014-11-21T19:01:08.407 回答
2

我不知道这个问题是否已经得到解答,或者只是游戏的语法发生了变化,这就是你最初以这种方式编写它的原因,但这段代码适用于你正在做的事情:

var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
于 2015-09-16T06:57:13.923 回答
-1

在 Screeps 中console.log()总是打印 undefined。在 undefined 之前的行中,您可以找到打印值:

> console.log(Game)
[hh:mm:ss] [object Object]
< undefined
于 2014-11-22T00:18:29.217 回答