2
我想運行一個代碼塊只有當變量不是有效的字符串(長度> 0,而不是未定義)。基於this SO post我認爲我做對了,但每次都運行。我在這裏做錯了什麼?只運行,如果字符串值沒有設置,每次都在運行
if (creep.memory.sourceid ||creep.memory.depositLoc||creep.memory.sourceType)
{
creep.memory.sourceid = getSourceMinWorkers(creep);
creep.memory.sourceType='energy';
creep.memory.depositLoc=getClosestDepositLoc(creep.memory.sourceid,creep.memory.sourceType);
console.log(creep.name," harvesting ",creep.memory.sourceType," at: ",creep.memory.sourceid," depositing at: ",creep.memory.depositLoc);
}
輸出的console.log的:
H1_1 harvesting energy at: 81a61f68f5eb4057223b05b2 depositing at: a7633d25d9058f616ab8a0f3
H1_1 harvesting energy at: 1649baad43f736c9fc13d2ad depositing at: a7633d25d9058f616ab8a0f3
您是否嘗試在輸入'if'塊後立即註銷'creep.memory.sourceid'等?這些值中至少有一個必須是truthy(不是'false','undefined','null'或一個空字符串)。你可以[自己輕鬆驗證這一點。](https://jsfiddle.net/ohnyfyxq/) –
你可以創建一個更簡單的例子來了解它的底部。你的具體遊戲代碼對於這項任務並不重要。 – sheriffderek
@sheriffderek他們的代碼對我來說似乎很簡單。他們缺少的是有關這三個值真正屬於什麼的信息。 –