努力在我的screeps代码中找到修复道路的语法错误(其他意外的令牌)

时间:2017-10-17 21:47:49

标签: javascript screeps

以下是我的生成器creeps的脚本。 creeps能够很好地构建,但是我的脚本在我写的修复部分引发了错误。整个建筑脚本粘贴在下面:

var roleBuilder = {

    /** @param {Creep} creep **/
    run: function(creep) {

        if(creep.memory.building && creep.carry.energy == 0) {
            creep.memory.building = false;
            creep.say(' harvest');
        }
        if(!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
            creep.memory.building = true;
            creep.say(' build');
        }

        if(creep.memory.building) {
            var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            if(targets.length) {
                if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
                }
            }
            else {
                targets = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return (structure.structureType == STRUCTURE_ROAD && structure.hits < (structure.hitsMax * 0.25));
                        if(targets.length) {
                            if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                                creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});

                            }

                        }

                    }

                });
                else {
                    var sources = creep.room.find(FIND_SOURCES);
                    if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
                    }
                }
            }    
        };
    }
}

module.exports = roleBuilder;

我已经尝试过像JSLint这样的在线工具来尝试解决我的语法错误,但这是不成功的。我几乎可以肯定错误在本节中,因为删除构建代码可以修复错误。

抛出的错误如下:

SyntaxError:其他意外的令牌     在Object.h:5:31901     在主要:3:19     在评论:71:4     在Object.h:5:31901     at Object.c.runCode:6:16531

我很感激您提供的任何帮助,因为我对Javascript相对较新。

编辑:问题现在解决了。对于发现这个有用的人来说,这是我的固定代码:

var roleBuilder = {

    /** @param {Creep} creep **/
    run: function(creep) {

        if(creep.memory.building && creep.carry.energy == 0) {
            creep.memory.building = false;
            creep.say(' harvest');
        }
        if(!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
            creep.memory.building = true;
            creep.say(' build');
        }
        if(creep.memory.building) {
            var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            if(targets.length) {
                if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
                }
            }
            else {
                targets = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return (structure.structureType == STRUCTURE_ROAD && structure.hits < (structure.hitsMax * 0.25));
                        if(targets.length) {
                            if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                                creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
                            }
                    }
                        }         
                        });
                   }
                }
                else {
                    var sources = creep.room.find(FIND_SOURCES);
                    if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); 
            }    
        };
    }
}

module.exports = roleBuilder;

1 个答案:

答案 0 :(得分:-1)

我设法解决了这个问题,并且已将固定代码复制到我原来的帖子中。