javascript - 类型错误:数字不是函数

标签 javascript function typeerror

我正在开发一个基于文本的游戏,我有一个函数 (turn()),可以根据用户按下的按钮确定用户的操作。

但是每次您按下 3 个按钮之一时,控制台都会显示 TypeError: number is not a function

我尝试了很多操作(例如将所有脚本移到一个文件中、全局定义游戏变量以及更改函数的顺序),但仍然不起作用!

有什么帮助吗?

<小时/>

JavaScript 代码:

console.log("Resetting resources...");
var turn = 1;
var player = {
  damage: Math.floor(Math.random() * 4 + 2), //Deals 2 to 5 damage
  healing: Math.floor(Math.random() * 4 + 3), //Heals 3 to 6 health
  health: null,
  maxHP: 25, //Maximum health (not recommended to change)
  special: true,
  alive: true
};
var dragon = {
  damage: Math.floor(Math.random() * 6 + 1), //Deals 1 to 6 damage
  health: null,
  maxHP: 28, //Maximum health (not recommended to change)
  alive: true
};
player.health = player.maxHP;
dragon.health = dragon.maxHP;
console.log("Resources ready!");

function turn() {
  if (player.alive) {
    /*Player turn*/
    console.log("---------------TURN " + turn + " : PLAYER---------------");
    alert("The dragon is still alive!");
    console.log("Player HP: " + player.health + "/" + player.maxHP);

    switch (action) {
      case '1':
        console.log("Dealt " + player.damage + " damage!");
        dragon.health -= player.damage;
        if (dragon.health <= 0) {
          alert("The dragon has been slain!");
          console.log("---------------DRAGON SLAIN---------------");
          dragon.alive = false;
          player.alive = false;
        }
        break;
      case '2':
        console.log("Recovered " + player.healing + " health!");
        player.health += player.healing;
        break;
      case '3':
        alert("Scared of dying, you ditch the scene before you become human toast.");
        console.log("---------------PLAYER SURRENDERS---------------");
        player.alive = false;
        break;
      default:
        console.error("Random error occured.");
        break;
    }

    /*Reset RNG*/
    player.damage = Math.floor(Math.random() * 4 + 2);
    player.healing = Math.floor(Math.random() * 4 + 3);

    /*Dragon turn*/
    console.log("---------------TURN " + turn + " : DRAGON---------------");
    console.log("Dragon HP: " + dragon.health + "/" + dragon.maxHP);
    console.log("Dealt " + dragon.damage + " damage!");
    player.health -= dragon.damage;
    if (player.health <= 0) {
      alert("You have died!");
      console.log("---------------PLAYER DIES---------------");
      player.alive = false;
    }
    /*Reset RNG*/
    dragon.damage = Math.floor(Math.random() * 6 + 1);
    turn++;
  } else if (!player.alive && dragon.alive) {
    alert("You have died!\nGAME OVER\nReset the game to play again.");
  } else if (!dragon.alive) {
    alert("You have slain the dragon!\nGAME OVER\nReset the game to play again.");
  }
}

function attack() {
  turn('1'); //Error occurs here
}

function heal() {
  turn('2'); //and here
}

function flee() {
  turn('3'); // and here
}
<button onclick="attack()">Attack</button>
<button onclick="heal()">Heal</button>
<button onclick="flee()">Flee!</button>

最佳答案

您在脚本开头将 turn 定义为变量:varturn = 1;

然后您稍后尝试将其重新定义为函数:functionturn() { ... }

“问题”是 JavaScript 将函数定义放在 block 的开头,在变量定义之后但在变量赋值之前,因此实际上您的 JavaScript 代码将被解释为:

var turn;

function turn() { ... }

turn = 1;

最终结果实际上是 turn 是一个数字 (1) 并且这不是一个函数,所以你不能用 turn 调用它('1')

关于javascript - 类型错误:数字不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019079/

相关文章:

javascript - 引用错误: display is not defined

javascript - 如何将固定宽度的 div 居中,以便它水平滚动并在 div 比浏览器宽时响应地保持居中?

JavaScript 图像 OnLoad 事件

C++ 传递函数的问题(数学不加起来)

python - 将不存在的路径分配给字符串时出现类型错误

javascript - 通过 Object.keys 访问 Javascript/Google Apps 脚本属性 TypeError

javascript - gulpjs - 将多个文件合并为一个 gulpfile

C++:如何在循环中为函数指针生成数组

json - 从 PostgreSQL 中的函数返回中删除双引号

javascript - 未捕获的类型错误 : Cannot call method 'toLowerCase' of undefined