我试图编写一个代码,以防止2条小兵(收割者)到达相同的目的地,并将它们绑定到该目的地,直到到达目的地为止。

在第55行,我得到一个可以理解的错误

55:creep.memory.targetID = target [checkTarget] .id;
由于我没有看到target [checkTarget]为null的原因

谁能告诉我我在做什么错?

var roleHarvester = {

    /** @param {Creep} creep **/
    run: function(creep) {
        if (!creep.memory.targetID) creep.memory.targetID = "not given";
        console.log(creep.memory.targetID);
        if (creep.carry.energy < creep.carryCapacity) {
            var nearestSource = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
            if (creep.harvest(nearestSource) == ERR_NOT_IN_RANGE) {
                creep.moveTo(nearestSource, {
                    visualizePathStyle: {
                        stroke: '#ffaa00'
                    }
                });
            }
        } else {
            if (creep.memory.targetID != "not given") {
                console.log("should not be not given");
                if (Game.getObjectById(creep.memory.targetID).hits == Game.getObjectById(creep.memory.targetID).hitsMax) {
                    creep.memory.targetID = "not given";

                    var targets = creep.room.find(FIND_STRUCTURES, {
                        filter: (structure) => {
                            return (structure.structureType == STRUCTURE_SPAWN ||
                                structure.structureType == STRUCTURE_EXTENSION ||
                                structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
                        }
                    });
                } else {
                    if (creep.transfer(Game.getObjectById(creep.memory.targetID), RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(Game.getObjectById(creep.memory.targetID), {
                            visualizePathStyle: {
                                stroke: '#ffffff'
                            }
                        });
                    }
                }
            } else {
                var targets = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return (structure.structureType == STRUCTURE_SPAWN ||
                            structure.structureType == STRUCTURE_EXTENSION ||
                            structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
                    }
                });
                console.log(targets);
                for (var checkTarget in targets) {
                    console.log(checkTarget);
                    console.log(targets[checkTarget]);
                    for (var name in Game.creeps) {
                        if (targets[checkTarget] = Game.getObjectById(Game.creeps[name].memory.targetID)) {
                            var fail = true;
                            break;
                        }
                        var fail = false;
                    }
                    if (fail == true) continue;

                    console.log(Game.getObjectById(targets[checkTarget]));
                    creep.memory.targetID = targets[checkTarget].id;
                    break;

                }
            }
        }
    }
};

module.exports = roleHarvester;

最佳答案

在以下if条件下出了点问题:

for(var name in Game.creeps) {
   if(targets[checkTarget] = Game.getObjectById(Game.creeps[name].memory.targetID)){


您可能是说==吗?

关于javascript - 爬行:获取对象的ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48349879/

10-09 20:14