azdfg1
azdfg1

Reputation: 109

HTML canvas game not displaying

I am trying to build a simple game. I am using google chrome as my browser. When I inspect the element the canvas exists, and if I use the Dev tools and put a breakpoint at the beginning of my script I can follow through the code the way it is written, and all of my images exist. Why will this not display?

<html>
<head>
<title>canvasGame</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
// 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";
// Game objects
var hero = {
speed: 256, // movement in pixels per second
x: 0,
y: 0
};
var monster = {
x: 0,
y: 0
};
var monstersCaught = 0;
// 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;

// Throw the monster somewhere on the screen randomly
monster.x = 32 + (Math.random() * (canvas.width - 64));
monster.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 (40 in keysDown) { // Player holding down
hero.y += hero.speed * modifier;
}
if (37 in keysDown) { // Player holding left
hero.x -= hero.speed * modifier;
}
if (39 in keysDown) { // Player holding right
hero.x += hero.speed * modifier;
}

// 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;
reset();
}
};
// 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);
}

// Score
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Monsterrs caught: " + monstersCaught, 32, 32);
};
// The main game loop
var main = function () {
var now = Date.now();
var delta = now - then;

update(delta / 1000);
render();

then = now;

// Request to do this again ASAP
requestAnimationFrame(main);
};
// Cross-browser support for requestAnimationFrame
var w = window;
requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;
// Let's play this game!
var then = Date.now();
reset();
main();

</script>
</head>
<body>
<div></div>
</body>
</html>

I was following this tutorial. http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/

Upvotes: 0

Views: 397

Answers (2)

user1626594
user1626594

Reputation:

Like Jeremy J Starcher said document.body does not exit yet. Also your variable heroReady at least is not defined... Check this for the full code of that tutorial.

<!DOCTYPE html>
    <html>
    <head>
    <title>canvasGame</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
    function rungame(){
    // 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 = "https://raw.githubusercontent.com/lostdecade/simple_canvas_game/master/images/background.png";
    
    // Hero image
    var heroReady = false;
    var heroImage = new Image();
    heroImage.onload = function () {
    	heroReady = true;
    };
    heroImage.src = "https://raw.githubusercontent.com/lostdecade/simple_canvas_game/master/images/hero.png";
    
    // Monster image
    var monsterReady = false;
    var monsterImage = new Image();
    monsterImage.onload = function () {
    	monsterReady = true;
    };
    monsterImage.src = "https://raw.githubusercontent.com/lostdecade/simple_canvas_game/master/images/monster.png";
    
    // Game objects
    var hero = {
    	speed: 256 // movement in pixels per second
    };
    var monster = {};
    var monstersCaught = 0;
    
    // 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;
    
    	// Throw the monster somewhere on the screen randomly
    	monster.x = 32 + (Math.random() * (canvas.width - 64));
    	monster.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 (40 in keysDown) { // Player holding down
    		hero.y += hero.speed * modifier;
    	}
    	if (37 in keysDown) { // Player holding left
    		hero.x -= hero.speed * modifier;
    	}
    	if (39 in keysDown) { // Player holding right
    		hero.x += hero.speed * modifier;
    	}
    
    	// 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;
    		reset();
    	}
    };
    
    // 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);
    	}
    
    	// Score
    	ctx.fillStyle = "rgb(250, 250, 250)";
    	ctx.font = "24px Helvetica";
    	ctx.textAlign = "left";
    	ctx.textBaseline = "top";
    	ctx.fillText("Goblins caught: " + monstersCaught, 32, 32);
    };
    
    // The main game loop
    var main = function () {
    	var now = Date.now();
    	var delta = now - then;
    
    	update(delta / 1000);
    	render();
    
    	then = now;
    
    	// Request to do this again ASAP
    	requestAnimationFrame(main);
    };
    
    // Cross-browser support for requestAnimationFrame
    var w = window;
    requestAnimationFrame = w.requestAnimationFrame || w.webkitRequestAnimationFrame || w.msRequestAnimationFrame || w.mozRequestAnimationFrame;
    
    // Let's play this game!
    var then = Date.now();
    reset();
    main();
    }
    window.onload = rungame;
    </script>
    </head>
    <body>
    <div></div>
    </body>

</html>

Upvotes: 1

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

Checking your error console is always the first step of debugging and explains exactly what is going on:

document.body.appendChild(canvas);

document.body does not exist at this point in time.

Your options include:

  • Moving the script to the very bottom of the document.
  • Wrap the script in an onload event.

Upvotes: 3

Related Questions