类型错误:数字不是一个函数
TypeError: number is not a function
我正在开发一个基于文本的游戏,我有一个函数(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
定义为变量:var turn = 1;
。
然后,您稍后尝试将其重新定义为函数:function turn() { ... }
。
"问题"是 JavaScript 将函数定义放在块的开头,在变量定义之后但在变量赋值之前,所以实际上你的 JavaScript 代码将被解释为:
var turn;
function turn() { ... }
turn = 1;
最终结果实际上是turn
是一个数字(1
),这不是一个函数,所以你不能用turn('1')
调用它。
相关文章:
- 我可以在json对象中添加一个函数吗
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在另一个函数中使用变量this
- 在另一个函数成功结束后调用该函数
- mongoose.connect undefined不是一个函数
- 监听器必须是一个函数
- 使用JS函数来使用另一个函数的语法?node.js
- 如何取消object.prototypes javascript的一个函数
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 嵌套到另一个函数中的Fancybox函数;不起作用
- 如何在javascript中使用不止一个函数
- jQuery-在页面加载时执行一个函数
- jquery UI draggable:UI.children不是一个函数
- jQuery Mobile Undefined不是一个函数
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- javascript函数将数据添加到屏幕,但随后被另一个函数覆盖
- JS异常:animate不是一个函数
- 如何将一个函数附加到一个不存在的元素上
- JavaScript/jQuery-添加添加和删除类与下一个函数之间的延迟