为什么我不能从交换机内的变量中减去?

时间:2016-07-12 02:43:28

标签: javascript

在我开始之前我想说对不起我不会写得非常整洁我对java很新(这是我的第5天左右)。我试图从文本(在控制台开发人员工具中使用)制作一个小型的网页浏览器游戏,它受到了长寿的启发,我的问题是,当我从交换机内部的敌人健康中减去它什么都不做。这是这一点的代码:

var health = (20)
var weapon = ('copper sword(5 DMG)')
var damage = (5)

//for npc's: name = [health, damage]
var playerstats = (name + ": health = " + health + ", weapon = " + weapon)
var skeleton1stats = [5, 5]

var name = prompt("Hi, what's your name?")
alert("Welcome " + name + " Are you ready to play Monster Fighter?")

alert("HINT: there will be messages in the log that you also should read. when a message like this appears click ok to continue")
alert("HINT: read log messages before dialog boxes, as the log messages are made before the dialog boxes are.")

console.log("Lets begin!")

console.log("You are walking through a forest when you trip and fall into a dark pit...")
console.log("As you are walking around a cave the pit lead you to, you are approached by a skeleton")
console.log("The skeleton tells you that he can lead you to safety")

alert("HINT: when you come across a character such as this one you will have multiple options, 3 of which are usually: fight, flee, and stats")
alert("HINT: I would recommend before fighting any character you view their stats.")

var skeleton1 = prompt("Do you choose to: TRUST, FIGHT, FLEE, STATS").toUpperCase()

switch (skeleton1) {

case 'TRUST':
console.log("The skeleton brings you to a dead end and stabs you doing 5 damage to you, and you hit him back doing 5 damage to him.");
skeleton1stats[0] - 5;
health - 5
break;

case 'FIGHT':
console.log("You thrust your sword through the skeleton doing 5 damage to the skeleton.");
skeleton1stats[0] - 5;
break;

case 'FLEE':
console.log("You make a run for it, but he catches up to you and stabs you dealing 5 damage to you. laying next to you is a iron sword(7 DMG), you picked it up and hit him with it doing 7 damage to him.");
health - 5;
skeleton1stats[0] - 7;
weapon = 'iron sword(7 DMG)'
damage = 7
break;

case 'STATS':
    console.log(playerstats + ", skeleton: health = " + skeleton1stats[0] + ", damage = " + skeleton1stats[1])
    var skeleton1 = prompt("Do you choose to: TRUST, FIGHT, FLEE").toUpperCase()
    switch (skeleton1) {
        case 'TRUST':
            console.log("The skeleton brings you to a dead end and stabs you doing 5 damage to you, and you hit him back doing 5 damage to him.");
            skeleton1stats[0] - 5;
            health - 5
        break;
        case 'FIGHT':
            console.log("You thrust your sword through the skeleton doing 5 damage to the skeleton.");
            skeleton1stats[0] - 5;
        break;
        case 'FLEE':
            console.log("You make a run for it, but he catches up to you and stabs you dealing 5 damage to you. laying next to you is a iron sword(7 DMG), you picked it up and hit him with it doing 7 damage to him.");
            health - 5;
            skeleton1stats[0] - 7;                
            weapon = 'iron sword(7 DMG)'
            damage = 7
        break;
        default:
            alert("ALERT: you must restart the game.")       
        break;
}
break;

default:
    alert("ALERT: if you do not enter one of the choices this time you will have to restart the game.")
    var skeleton1 = prompt("Do you choose to: TRUST, FIGHT, FLEE, STATS").toUpperCase()
    switch (skeleton1) {
        case 'TRUST':
            console.log("The skeleton brings you to a dead end and stabs you doing 5 damage to you, and you hit him back doing 5 damage to him.");
            skeleton1stats[0] - 5;
            health - 5
        break;
        case 'FIGHT':
            console.log("You thrust your sword through the skeleton doing 5 damage to the skeleton.");
            skeleton1stats[0] - 5;
        break;
        case 'FLEE':
            console.log("You make a run for it, but he catches up to you and stabs you dealing 5 damage to you. laying next to you is a iron sword(7 DMG), you picked it up and hit him with it doing 7 damage to him.");
            health - 5;
            skeleton1stats[0] - 7;              
            weapon = 'iron sword(7 DMG)'
            damage = 7
        break;
        case 'STATS':
            console.log(playerstats + ", skeleton: health = " + skeleton1stats[0] + ", damage = " + skeleton1stats[1])
        var skeleton1 = prompt("Do you choose to: TRUST, FIGHT, FLEE").toUpperCase()
        switch (skeleton1) {
            case 'TRUST':
                console.log("The skeleton brings you to a dead end and stabs you doing 5 damage to you, and you hit him back doing 5 damage to him.");
                skeleton1stats[0] - 5;
                health - 5
            break;
            case 'FIGHT':
                console.log("You thrust your sword through the skeleton doing 5 damage to the skeleton.");
                skeleton1stats[0] - 5;
            break;
            case 'FLEE':
                console.log("You make a run for it, but he catches up to you and stabs you dealing 5 damage to you. laying next to you is a iron sword(7 DMG), you picked it up and hit him with it doing 7 damage to him.");
                health - 5;
                skeleton1stats[0] - 7;
                weapon = 'iron sword(7 DMG)'
                damage = 7
            break;
            default:
                alert("ALERT: you must restart the game.")       
            break;
}
            break;
            default:
                alert("ALERT: you must restart the game.")       
            break;
}
            break;
}

if(skeleton1stats[0] < 1) {
console.log("You defeated the skeleton!") }

console.log("You continue down the cave further...")
console.log("far into the cave you see light, but coming from it are 3 zombies!")
console.log("Fairies then appear and kill the zombies and tell you to follow them to the 'hallow'")
console.log("in the hallow there are many fairies and the biggest one of them, the queen says she needs your help slaying these monsters that have begun appearing near the hallow")

var helpsfairies = confirm("will you help the fairies?")

if(helpsfairies) {
console.log("The fairies thank you and show you the way to a cave that they have seen many zombies in lately")
} else {
    console.log("The queen puts you under a spell and you help them anyway...") }

1 个答案:

答案 0 :(得分:0)

您需要将值分配回变量:

health = health - 5;

更多信息: http://www.w3schools.com/js/js_variables.asp