未捕获类型错误:没有足够的参数Javascript游戏

Uncaught TypeError: Not enough arguments Javascript Game

本文关键字:参数 游戏 Javascript 类型 错误      更新时间:2023-09-26

我在整个代码的第259行遇到了一个问题,这是这个函数的第30行,我得到了一个未捕获的TypeError错误:没有足够的参数,我不确定如何解决这个问题,我已经看了一下开括号和闭括号,但我找不到答案。如果需要更多代码,请告诉我

var render = function () {
if(displayQuestion == 1 && hideGame == 1)
{
    if(gameOver == true)
    {
        ctx.fillStyle = "rgb(250, 250, 250)";
        ctx.font = "24px Verdana";
        ctx.textAlign = "right";
        ctx.textBaseline = "top";
        ctx.fillText("Game Over Dude");
    }
    else
    {
        if (bgReady) 
        {
            ctx.drawImage(bgImage, 0, 0);
        }
        ctx.fillStyle = "rgb(250, 250, 250)";
        ctx.font = "24px Verdana";
        ctx.textAlign = "right";
        ctx.textBaseline = "top";
        ctx.fillText("Level: " + level, 400, 32);
        ctx.fillStyle = "rgb(250, 250, 250)";
        ctx.font = "40px Verdana";
        ctx.textAlign = "center";
        ctx.textBaseline = "top";
        if(level == 1)
        {
            var text = "What is " + num1 + " x " + num2 + "?";
        }
        else if(level == 2)
        {
            var text = "What is " + num3 + " x " + num4 + "?";
        }
        else if(level >= 3)
        {
            var text = "What is " + num5 + " x " + num6 + "?";
        }
        **ctx.fillText(text, canvas.width / 2, canvas.height / 2);**
    }
}
else if(displayQuestion == 0 && hideGame == 0)
{
    if (bgReady) {
        ctx.drawImage(bgImage, 0, 0);
    }
    if (heroReady) {
        ctx.drawImage(heroImage, hero.x, hero.y);
    }
    if (monsterReady) {
        ctx.drawImage(monsterImage, monster.x, monster.y);
        ctx.drawImage(monsterImage, monster1.x, monster1.y);
        ctx.drawImage(monsterImage, monster2.x, monster2.y);
    }
    // Score
    ctx.fillStyle = "rgb(250, 250, 250)";
    ctx.font = "24px Verdana";
    ctx.textAlign = "left";
    ctx.textBaseline = "top";
    ctx.fillText("Score: " + monstersCaught, 32, 32);
    ctx.fillStyle = "rgb(250, 250, 250)";
    ctx.font = "24px Verdana";
    ctx.textAlign = "right";
    ctx.textBaseline = "top";
    ctx.fillText("Level: " + level, 400, 32);
    ctx.fillStyle = "rgb(250, 250, 250)";
    ctx.font = "24px Verdana";
    ctx.textAlign = "center";
    ctx.textBaseline = "top";
    if(debugVariable == true)
    {
        var coords = 'Co-ords(' + hero.x.toFixed(2) + "," + hero.y.toFixed(2) + ')';
    } 
    else {
        coords = "";
    }
    ctx.fillText(coords , 200, 450);
}
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "18px Verdana";
ctx.textAlign = "center";
ctx.textBaseline = "top";
if(debugVariable == true)
{
    deText = "Degbug: on";
}
else
{
    deText = "";
}
ctx.fillText(deText, 100, 100);
};

完整代码
    var debugVariable = false;
function dBugVariableFunction()
{
    debugVariable = !debugVariable;
}
var changeNumber = false;
function getAnswer(){
   var answer = document.getElementById('answer');
   var userAnswer = answer.value;
    if (!userAnswer || userAnswer == ""){
        alert('You must enter a number for it to be marked!');
    }
    else if (num1 * num2 == userAnswer || debugVariable == true || num3 * num4 == userAnswer || num5 * num6 == userAnswer)
    {
        alert('Correct');
            ++level;
            hideGame = 0;
            displayQuestion = 0;
            document.getElementById("answerswer").value="";
    }
    else if (num1 * num2 != userAnswer || num3 * num4 != userAnswer || num5 * num6 != userAnswer)
    {
        alert('Incorrect');
        if(level == 1)
        {
        }
        else
        {
            --monstersCaught;
        }
    }
  }
//checking if canvas is supported
function checkCanvasSupported(){
  var element = document.createElement('canvas');
  return !!(element.getContext && element.getContext('2d'));
}
//if canvas not supported then alert user
if (!checkCanvasSupported()){
    alert('Sorry cavas isn''t supported by your internet browser!');
}
// Create the canvas
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);
// Background image
var bgReady = false;
var bgImage = new Image();
bgImage.onload = function () {
    bgReady = true;
};
bgImage.src = "images/background.png";
// Hero image
var heroReady = false;
var heroImage = new Image();
heroImage.onload = function () {
    heroReady = true;
};
heroImage.src = "images/hero.png";
// Hero image
var enemyReady = false;
var enemyImage = new Image();
enemyImage.onload = function () {
    enemyReady = true;
};
enemyReady.src = "images/hero.png";
// Monster image
var monsterReady = false;
var monsterImage = new Image();
monsterImage.onload = function () {
    monsterReady = true;
};
monsterImage.src = "images/monster.png";
// Game objects
var hero = {
    speed: 256 // movement in pixels per second
};
var enemy = {
    speed: 128 // movement in pixels per second
};
var monster = {};
var monster1 = {};
var monster2 = {};
var monstersCaught = 0;
var level = 1;
var displayQuestion = 1;
var gameOver = false;
var hideGame = 1;
var keysDown = {}; // Handle keyboard controls
addEventListener("keydown", function (e) {
    keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function (e) {
    delete keysDown[e.keyCode];
}, false);
// Reset the game when the player catches a monster
var reset = function () {
    hero.x = canvas.width / 2;
    hero.y = canvas.height / 2;
};
var reset0 = function () {
    hero.x = canvas.width / 2;
    hero.y = canvas.height / 2;
    // Throw the monster somewhere on the screen randomly
    monster.x = 32 + (Math.random() * (canvas.width - 64));
    monster.y = 32 + (Math.random() * (canvas.height - 64));
}
var reset1 = function () {
    hero.x = canvas.width / 2;
    hero.y = canvas.height / 2;
    // Throw the monster somewhere on the screen randomly
    monster1.x = 32 + (Math.random() * (canvas.width - 64));
    monster1.y = 32 + (Math.random() * (canvas.height - 64));
};
var reset2 = function () {
    hero.x = canvas.width / 2;
    hero.y = canvas.height / 2;
    // Throw the monster somewhere on the screen randomly
    monster2.x = 32 + (Math.random() * (canvas.width - 64));
    monster2.y = 32 + (Math.random() * (canvas.height - 64));
};
// Update game objects
var update = function (modifier) {
  //  enemy.position = Vector3.MoveTowards(enemy.position, hero.transform.position, .03);
    if (38 in keysDown) { // Player holding up
        hero.y -= hero.speed * modifier;
        if(hero.y < 0){
            reset();
        }
    }
    if (40 in keysDown) { // Player holding down
        hero.y += hero.speed * modifier;
        if(hero.y > canvas.height){
        reset();
        }
    }
    if (37 in keysDown) { // Player holding left
        hero.x -= hero.speed * modifier;
        if(hero.x < 0){
            reset();
        }
    }
    if (39 in keysDown) { // Player holding right
        hero.x += hero.speed * modifier;
        if(hero.x > canvas.width){
            reset();
        }
    }
    // Are they touching?
    if (
        hero.x <= (monster.x + 32)
        && monster.x <= (hero.x + 32)
        && hero.y <= (monster.y + 32)
        && monster.y <= (hero.y + 32)
    ) {
        ++monstersCaught;
        reset0();
    }
    else if (
        hero.x <= (monster1.x + 32)
        && monster1.x <= (hero.x + 32)
        && hero.y <= (monster1.y + 32)
        && monster1.y <= (hero.y + 32)
    ) {
        ++monstersCaught;
        reset1();
    }
    else if (
        hero.x <= (monster2.x + 32)
        && monster2.x <= (hero.x + 32)
        && hero.y <= (monster2.y + 32)
        && monster2.y <= (hero.y + 32)
    ) {
        ++monstersCaught;
        reset2();
    }
    if (monstersCaught == 5 && level != 3){
        hideGame = 1;
        displayQuestion = 1;
    }
    else if (monstersCaught == 10 && level != 4){
        hideGame = 1;
        displayQuestion = 1;
    }
    else if (monstersCaught == 0 && level != 1){
        hideGame = 1;
        displayQuestion = 1;
        gameOver = true;
    }

};  
    var num1 = Math.floor(Math.random() * 5);
    var num2 = Math.floor(Math.random() * 4);
    var num3 = Math.floor(Math.random() * 11);
    var num4 = Math.floor(Math.random() * 9);
    var num5 = Math.floor(Math.random() * 21);
    var num6 = Math.floor(Math.random() * 15);  
// Draw everything
var render = function () {
    if(displayQuestion == 1 && hideGame == 1)
    {
        if(gameOver == true)
        {
            ctx.fillStyle = "rgb(250, 250, 250)";
            ctx.font = "24px Verdana";
            ctx.textAlign = "right";
            ctx.textBaseline = "top";
            ctx.fillText("Game Over Dude");
        }
        else
        {
            if (bgReady) 
            {
                ctx.drawImage(bgImage, 0, 0);
            }
            ctx.fillStyle = "rgb(250, 250, 250)";
            ctx.font = "24px Verdana";
            ctx.textAlign = "right";
            ctx.textBaseline = "top";
            ctx.fillText("Level: " + level, 400, 32);
            ctx.fillStyle = "rgb(250, 250, 250)";
            ctx.font = "40px Verdana";
            ctx.textAlign = "center";
            ctx.textBaseline = "top";
            if(level == 1)
            {
                var text = "What is " + num1 + " x " + num2 + "?";
            }
            else if(level == 2)
            {
                var text = "What is " + num3 + " x " + num4 + "?";
            }
            else if(level >= 3)
            {
                var text = "What is " + num5 + " x " + num6 + "?";
            }
            ctx.fillText(text, canvas.width / 2, canvas.height / 2);
        }
    }
    else if(displayQuestion == 0 && hideGame == 0)
    {
        if (bgReady) {
            ctx.drawImage(bgImage, 0, 0);
        }
        if (heroReady) {
            ctx.drawImage(heroImage, hero.x, hero.y);
        }
        if (monsterReady) {
            ctx.drawImage(monsterImage, monster.x, monster.y);
            ctx.drawImage(monsterImage, monster1.x, monster1.y);
            ctx.drawImage(monsterImage, monster2.x, monster2.y);
        }
        // Score
        ctx.fillStyle = "rgb(250, 250, 250)";
        ctx.font = "24px Verdana";
        ctx.textAlign = "left";
        ctx.textBaseline = "top";
        ctx.fillText("Score: " + monstersCaught, 32, 32);
        ctx.fillStyle = "rgb(250, 250, 250)";
        ctx.font = "24px Verdana";
        ctx.textAlign = "right";
        ctx.textBaseline = "top";
        ctx.fillText("Level: " + level, 400, 32);
        ctx.fillStyle = "rgb(250, 250, 250)";
        ctx.font = "24px Verdana";
        ctx.textAlign = "center";
        ctx.textBaseline = "top";
        if(debugVariable == true)
        {
            var coords = 'Co-ords(' + hero.x.toFixed(2) + "," + hero.y.toFixed(2) + ')';
        } 
        else {
            coords = "";
        }
        ctx.fillText(coords , 200, 450);
    }
    ctx.fillStyle = "rgb(250, 250, 250)";
    ctx.font = "18px Verdana";
    ctx.textAlign = "center";
    ctx.textBaseline = "top";
    if(debugVariable == true)
    {
        deText = "Degbug: on";
    }
    else
    {
        deText = "";
    }
    ctx.fillText(deText, 100, 100);
};
// The main game loop
var main = function () {
    var now = Date.now();
    var delta = now - then;
    update(delta / 1000);
    render();
    then = now;
};
// Let's play this game!
reset();
reset0();
reset1();
reset2();
var then = Date.now();
setInterval(main, 1); // Execute as fast as possible

你说你的问题出在

ctx.fillText(text, canvas.width / 2, canvas.height / 2);
Uncaught TypeError: Not enough arguments

MDN文档说参数是

DOMString textToDraw
float x
float y
[可选]float maxWidth

看起来你传递的参数数量还可以。

的问题是否仍然存在?
ctx.fillText("Hello World", 5, 5);

如果可以,检查canvas是否按预期定义,并且宽度高度是整数(而不是undefined)。这可以通过在;

前一行添加console.log来实现。
console.log(canvas, canvas.width, canvas.height);

如果问题在这里,你需要额外的逻辑,或者一个不同的方法(例如使用offsetWidth等)

如果这不起作用,浏览器可能在没有最后一个可选参数的情况下实现了它,请键入一个足够大的数字,例如1280