Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript Boolean issue
    primarykey
    data
    text
    <p>So I'm creating a game in javascript and I'm trying to create a debugging mode but when I create the variable debugVariable and set it to a boolean </p> <pre><code>var debugVariable = new Boolean(); </code></pre> <p>It somehow reads as true, no matter what I do. Firstly here is the full block of code</p> <pre><code> var debugVariable = new Boolean(); function debuggCheckBox() { if(debugVariable){ debugVariable = false;} else {debugVariable = true;} } function testNumber1() { var num1 = Math.floor(Math.random() * 11); var num2 = Math.floor(Math.random() * 3); var link = "GAME 2"; var answer = prompt('LEVEL 1: What is ' + num1 + " * " + num2 +"?"); if (!answer || answer == ""){ testNumber1(); } else if (num1 * num2 == answer) { alert('Correct'); } else if (num1 * num2 != answer) { alert('Incorrect'); testNumber1(); } } testNumber1(); function testNumber2() { var num1 = Math.floor(Math.random() * 21); var num2 = Math.floor(Math.random() * 13); var answer = prompt('LEVEL 2: What is ' + num1 + " * " + num2 +"?"); if (num1 * num2 == answer) { alert('Correct'); ++monstersCaught; reset(); ++level; } else if (num1 * num2 != answer) { alert('Incorrect'); testNumber2(); } } function testNumber3() { var num1 = Math.floor(Math.random() * 31); var num2 = Math.floor(Math.random() * 23); var answer = prompt('LEVEL 3: What is ' + num1 + " * " + num2 +"?"); if (num1 * num2 == answer) { alert('Correct'); ++monstersCaught; ++level; reset(); } else if (num1 * num2 != answer) { alert('Incorrect'); testNumber3(); } } //checking if canvas is supported function checkCanvasSupported(){ var element = document.createElement('canvas'); return !!(element.getContext &amp;&amp; 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.createElement("canvas"); 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"; // 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 monster = {}; var monster1 = {}; var monster2 = {}; var monstersCaught = 0; var level = 1; // Handle keyboard controls var keysDown = {}; 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) { if (38 in keysDown) { // Player holding up hero.y -= hero.speed * modifier; if(hero.y &lt; 0){ reset(); } } if (40 in keysDown) { // Player holding down hero.y += hero.speed * modifier; if(hero.y &gt; canvas.height){ reset(); } } if (37 in keysDown) { // Player holding left hero.x -= hero.speed * modifier; if(hero.x &lt; 0){ reset(); } } if (39 in keysDown) { // Player holding right hero.x += hero.speed * modifier; if(hero.x &gt; canvas.width){ reset(); } } // Are they touching? if ( hero.x &lt;= (monster.x + 32) &amp;&amp; monster.x &lt;= (hero.x + 32) &amp;&amp; hero.y &lt;= (monster.y + 32) &amp;&amp; monster.y &lt;= (hero.y + 32) ) { ++monstersCaught; reset0(); } else if ( hero.x &lt;= (monster1.x + 32) &amp;&amp; monster1.x &lt;= (hero.x + 32) &amp;&amp; hero.y &lt;= (monster1.y + 32) &amp;&amp; monster1.y &lt;= (hero.y + 32) ) { ++monstersCaught; reset1(); } else if ( hero.x &lt;= (monster2.x + 32) &amp;&amp; monster2.x &lt;= (hero.x + 32) &amp;&amp; hero.y &lt;= (monster2.y + 32) &amp;&amp; monster2.y &lt;= (hero.y + 32) ) { ++monstersCaught; reset2(); } if (monstersCaught == 5){ testNumber2(); } else if (monstersCaught == 10){ testNumber3(); } }; // Draw everything var render = function () { 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 + "," + hero.y + ')'; } else { coords = ""; } ctx.fillText(coords , 100, 200); ctx.fillStyle = "rgb(250, 250, 250)"; ctx.font = "24px Verdana"; ctx.textAlign = "center"; ctx.textBaseline = "top"; ctx.fillText("Debug: " + debugVariable, 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 </code></pre> <p>And here are the main areas of trouble (lines 1-6)</p> <pre><code> var debugVariable = new Boolean(); function debuggCheckBox() { if(debugVariable){ debugVariable = false;} else {debugVariable = true;} } </code></pre> <p>(lines 233-270)</p> <pre><code>var render = function () { 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 + "," + hero.y + ')'; } else { coords = ""; } ctx.fillText(coords , 100, 200); ctx.fillStyle = "rgb(250, 250, 250)"; ctx.font = "24px Verdana"; ctx.textAlign = "center"; ctx.textBaseline = "top"; ctx.fillText("Debug: " + debugVariable, 100, 100); }; </code></pre> <p>And the HTML code for the checkbox, although it does nothing at all</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Simple Canvas Game&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="style/style.css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;script src="js/game.js"&gt;&lt;/script&gt; &lt;input type="checkbox" name="debugOnOff" onclick="debugCheckBox()"&gt; &lt;a href="game2.html"&gt; GAME 2&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload