如果满足语句条件但代码不运行

时间:2013-12-19 07:47:46

标签: javascript if-statement

由于某种原因,第224行的if语句中的代码不会运行。当函数loose()在if语句之外调用时,它可以工作。我不确定我的代码是什么问题。

<!DOCTYPE html>
<html>
<head>
    <title>Jordan's Space Explorer </title>
</head>
<body>

<script>
// Tells user what they are about to play.
confirm ("You are about to play Jordan's Space Explorer?")

// Prompt user for their name.
userName="Capt. "+prompt("What is your name?");
shipName=prompt("What is your ship's name?")

// Declare start locations, default star system locations, and check for encounters.
var min = -10;
var max = 10;
var locationY = Math.floor(Math.random() * (max - min + 1)) + min;
var locationX = Math.floor(Math.random() * (max - min + 1)) + min;
var locationZ = Math.floor(Math.random() * (max - min + 1)) + min;
var locationYEast;
var locationYWest;
var locationXNorth;
var locationXSouth;
var locationZAbove;
var locationZBelow;
var solLocationX = Math.floor(Math.random() * (max - min + 1)) + min;
var solLocationY = Math.floor(Math.random() * (max - min + 1)) + min;
var solLocationZ = Math.floor(Math.random() * (max - min + 1)) + min;
var spacePortLocationX = Math.floor(Math.random() * (max - min + 1)) + min;
var spacePortLocationY = Math.floor(Math.random() * (max - min + 1)) + min;
var spacePortLocationZ = Math.floor(Math.random() * (max - min + 1)) + min;
var sensors = false;
var locationName = false;
var credits = 1000
var health = 100
var shipSpeed = 1
var lasers = 10
encounters();
locationQuery();



//Go directions
function goNorth(){
    locationY = locationY +shipSpeed;
    console.log("You head North.");
    encounters();
    console.log("Y = "+locationY);
}
function goSouth(){
    locationY = locationY- shipSpeed;
    console.log("You head South.");
    encounters();
    console.log("Y = "+locationY);
}
function goEast(){
    locationX = locationX+shipSpeed;
    console.log("You head East");
    encounters();
    console.log("X = "+ locationX);
}
function goWest(){
    locationX = locationX-shipSpeed;
    console.log("You head West");
    encounters();
    console.log("X = "+locationX);
}
function goUp(){
    locationZ = locationZ +shipSpeed;
    console.log("You go up.");
    encounters();
    console.log("Z = "+locationZ);
}
function goDown(){
    locationZ = locationZ-shipSpeed;
    console.log("You go down.");
    encounters();
    console.log("Z = "+locationZ);
}






// Encounters
 function encounters()
 {
// Sol
    if(locationX === solLocationX && locationY === solLocationY && locationZ === solLocationZ){
     locationName = "Sol";
     console.log(shipName + " is at " + locationName)
     alert("Arrived at " + locationName);
    }
// Center of Galaxy
    if(locationX===0  && locationY===0  && locationZ===0)
    {
        locationName = "Center of Galaxy";
        console.log(shipName + " is at " + locationName)
       alert("Arrived at " + locationName);
    }
// SpacePort
 if(locationX === spacePortLocationX && locationY === spacePortLocationY && locationZ === spacePortLocationZ)
{
  locationName = "SpacePort"
  console.log(shipName + " is at " + locationName)
  alert("Arrived at " + locationName);
   }
         else{locationName = "empty space."}
      }






// Purchase
function makePurchase()
{
    if (locationName !== "SpacePort"){alert("You are not at the SpacePort.")}
    if (locationName === "SpacePort")
    {
        var buy = prompt(userName + ", "+"what do you purchase?","Lasers, Sensors, or Repair Ship")
        var purchase = buy.toUpperCase();{
            if(purchase === "LASERS" && credits>=5000){lasers=lasers+10, credits= credits-5000, alert(userName+ "'s ship," + shipName +", has had it's lasers upgraded.")}
            if(purchase === "SENSORS"){alert("These are not ready at this time.")}
            if(purchase === "REPAIR SHIP"){shipDamage=0, alert(shipName+" has been repaired.")}
        }

    }
}

// Display Credits.
function creditsRemaining(){
    console.log("Credits remaining: "+ credits);
}
// Display Laser Strength.
 function laserStrength(){
    console.log("Laser Strength: "+ lasers);
}
// Display Health.
function healthDisplay(){
    console.log(health)
}

// Find things.
// Find current location.
function locationQuery(){
    console.log(userName + " your ship, "+ shipName + ", is at " +locationName);
    console.log("X = " +locationX);
    console.log("Y = " +locationY);
    console.log("Z = " +locationZ);

}
// Find Sol.
function solQuery(){
    console.log("Sol is at:")
    console.log("X = " + solLocationX);
    console.log("Y = " + solLocationY);
    console.log("Z = " + solLocationZ);
}
// Find SpacePort.
function spacePortQuery(){
    console.log("The SpacePort is at:")
    console.log("X = " + spacePortLocationX);
    console.log("Y = " + spacePortLocationY);
    console.log("Z = " + spacePortLocationZ);
}

// GoTo
function goToSpacePort(){
 locationX = spacePortLocationX
 locationY = spacePortLocationY
 locationZ = spacePortLocationZ
 locationName = "SpacePort"
    }

// Quit
function quit()
{
var quitWindow = window.open("http://192.168.7.2:3000/workspace/myScripts/jordansSpaceExplorer/game/quitPage.html","_self");
}
    //Loose
   function loose()
    {
        var looseWindow = window.open("http://192.168.7.2:3000/workspace/myScripts/jordansSpaceExplorer/game/loose.html","_self");
   }
// Suicide
function suicide()
{
    health = health-100;
}

// Loose Conditions.
if(health <= 0){
    console.log("You have lost the game.")
    loose()
}
</script>

   Controls: <br>
    <button onclick="goEast()">Go East</button>
    <button onclick="goWest()">Go West</button>
    <br>
    <button onclick="goNorth()">Go North</button>
    <button onclick="goSouth()">Go South</button>
    <br>
    <button onclick="goUp()">Go Up</button>
    <button onclick="goDown()">Go Down</button>
    <br>
    <button onclick="creditsRemaining()">Credits</button>
    <button onclick="healthDisplay()">Heath</button>
    <button onclick="laserStrength()">Lasers</button>
    <br>
    <button onclick="locationQuery()">Where am I?</button>
    <button onclick="quit()">Quit</button>
    <br>
    SpacePort:
    <br>
    <button onclick="makePurchase()">Purchase</button>
<br>
  Debug: <br>
     <button onclick="spacePortQuery()">Where is SpacePort?</button><br>
     <button onclick="solQuery()">Where is Sol?</button><br>
     <button onclick="engageSensors()">Engage Sensors</button><br>
     <button onclick="goToSpacePort()">SpacePort</button><br>
     <button onclick="suicide()">Suicide!</button>
     <button onclick="loose()">Loose</button>
     </body>
</html>

任何帮助将不胜感激。我是编程新手,所以请考虑到这一点。

2 个答案:

答案 0 :(得分:0)

if语句不在任何函数中,因此它将在脚本加载时执行。 此时,变量 health 的值为100,所以通常不应该调用函数 loose()

也许你应该有一个计时器来检查健康状况,或者你将在每个修改健康状况的函数中调用的函数。

你可以这样做:

function suicide()
{
    health = health-100;
    checkHealth();
}

function checkHealth() {
   // Loose Conditions.
   if(health <= 0){
       console.log("You have lost the game.")
       loose()
   }
}

答案 1 :(得分:0)

您可以将if语句更改为

function checkLost() {
    // Loose Conditions.
    if(health <= 0){
        console.log("You have lost the game.")
        loose()
    }

    setTimeout(checkLost, 100);
}
setTimeout(checkLost, 100);

另一种方法是在这种情况下将逻辑添加到降低“船”寿命的方法中。就像在你的自杀方法一样。