-1
以下のコードがあります。 関数がtrueを返す場合は、特別なコマンドを実行します。 "if"の下で関数を呼び出すときはいつでも、特別なコマンドは常に彼の仕事をしています。関数がtrueを返した場合は、条件の場合にコマンドを実行してください。
// You are trapped. Don't move, it'll be painful.
// Attack ogres only when they're within reach.
// This function checks if the enemy is in your attack range.
// The function returns a boolean value: true or false
function inAttackRange(enemy) {
var distance = hero.distanceTo(enemy);
// Almost all swords have attack range of 3.
if (distance <= 3) {
return true;
} else {
return false;
}
}
while (true) {
// Find the nearest enemy and store it in a variable.
var enemy = hero.findNearestEnemy();
// Call inAttackRange(enemy), with the enemy as the argument
// and save the result in the variable canAttack.
var canAttack = inAttackRange(enemy);
// If the result stored in canAttack is true, then attack!
if (canAttack) {
hero.attack(enemy);
}
}
}
は**ですinsert * "特別なコマンド" *? –
特殊コマンドはhero.attack(敵)です。関数がtrueを返すときにのみ実行されます。 –
どこ? if条件で –