2016-12-18 19 views
-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); 
 
     } 
 
    } 
 
}

+0

は**ですinsert * "特別なコマンド" *? –

+0

特殊コマンドはhero.attack(敵)です。関数がtrueを返すときにのみ実行されます。 –

+0

どこ? if条件で –

答えて

0

あなたのコードは、構文エラーが発生しました。 **あなたがしたいのはここの作業スクリプト(「o.findNearestEnemy()」または「英雄」のようないくつかの関数および変数が存在しないため、動作しない場合があります)

// 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); 
 
    } 
 
}

関連する問題