下面是我的builder creeps的腳本。這些小兵能夠很好地構建,但是我的腳本在我寫的修復部分上拋出了一個錯誤。整個建築腳本粘貼下面:努力在我的screeps代碼中找到修復道路的語法錯誤(其他意外的令牌)
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的來試圖解決我的語法錯誤,但是這一直未果。我幾乎可以肯定,這部分錯誤是因爲刪除構建代碼修復了錯誤。
引發的錯誤下面是:
語法錯誤:意外標記別的 在Object.h:5:31901 在主:3:19 在EVAL: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;
你有一個目標之後沒有if if – juvian
最後'else'沒有'if'。 – dhilt
變量目標被多次使用,您的意思是代碼中的確切位置?我嘗試過轉移else語句,但無法找到應該去的地方。預期的行爲是,如果蠕變記憶正在建設中,施工人員應該搜索建築工地。如果沒有發現,爬行者應該試圖修復任何道路,其命中率低於最大值的25%。最後,如果沒有嚴重損壞的道路或建築工地,建築商應該恢復收割。希望這是有幫助的。 – universal123