0

Ive got a problem on line 259 of the whole code which is line 30 in this function, I get an error of Uncaught TypeError: Not enough arguments and I'm unsure how to fix this, I've had a look at opening and closing brackets but I cant find an answer. Let me know if more code is needed

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);
};

FULL CODE

    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("answer").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
4

1 回答 1

1

你说你的问题是

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

MDN文档说参数是

DOMString textToDraw
浮动 x
浮动 y
[可选]浮动 maxWidth

看起来好像您传递了数量合适的参数。问题是否持续存在

ctx.fillText("Hello World", 5, 5);

如果可行,请检查canvas是否按预期定义以及宽度高度是否为整数(不是undefined)。这可以在console.log之前用a来完成;

console.log(canvas, canvas.width, canvas.height);

如果问题出在此处,您将需要针对这种情况的额外逻辑或不同的方法(例如 useoffsetWidth等)

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

于 2013-06-14T23:11:29.717 回答