代码没有控制台输出

时间:2015-08-09 20:29:14

标签: javascript

我一直在研究一个可用于JavaScript / HTML RPG游戏的简单脚本,但在构建战斗脚本时,我没有得到任何控制台输出。我已经尝试了所有我能想到但仍然没有运气的东西,我开始怀疑自己是否在某个地方犯了编码错误。

var player = {hp:100, attack:10, defence:10, speed:20};
var enemy = {hp:100, attack:10, defence:10, speed:20};

function playeratk(){
    console.log("enemy " + enemy.hp);
    var pAtk = Math.floor(Math.random() * player.attack) + 1;
    console.log(pAtk);
    enemy.hp = enemy.hp - pAtk;
    console.log("enemy " + enemy.hp);
    console.log("-----");
}

function enemyatk(){
    console.log("player " + player.hp);
    var eAtk = Math.floor(Math.random() * enemy.attack) + 1;
    console.log(eAtk);
    player.hp = player.hp - eAtk;
    console.log("player " + player.hp);
    console.log("-----");
}


function battle() {
    if (player.hp < 1) {
        console.log("Enemy wins");
    }
    else if (enemy.hp < 1){
        console.log("Player Wins");
    }
    else {
        var pSpeed = Math.floor(Math.random() * player.speed) + 1;
        var eSpeed = Math.floor(Math.random() * enemy.speed) + 1;
        if (player.speed > enemy.speed){
            playeratk();
            enemyatk();
            battle();
        }
        else if(enemy.speed > player.speed){
            enemyatk();
            playeratk();
            battle();
        }
        else {
            playeratk();
            enemyatk();
            battle();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须至少在其外部调用battle()函数一次。也许在剧本的最后。