【问题标题】:HTML canvas game not displayingHTML画布游戏不显示
【发布时间】:2016-04-23 17:10:07
【问题描述】:

我正在尝试构建一个简单的游戏。我使用谷歌浏览器作为我的浏览器。当我检查元素时,画布存在,如果我使用开发工具并在脚本的开头放置一个断点,我可以按照它的编写方式跟踪代码,并且我的所有图像都存在。为什么不显示?

<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>

我正在学习本教程。 http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/

【问题讨论】:

  • 把你的脚本放在正文中。否则,您的脚本会在创建正文之前尝试添加到正文。

标签: javascript html canvas


【解决方案1】:

检查您的错误控制台始终是调试的第一步,并准确解释发生了什么:

document.body.appendChild(canvas);

document.body此时不存在。

您的选择包括:

  • 将脚本移动到文档的最底部。
  • 将脚本封装在 onload 事件中。

【讨论】:

  • 我将所有内容都移到了一个函数中,并将其命名为 onload,一切正常。谢谢。现在,当我将该脚本移动到一个单独的 js 文件中,并将脚本标记放在 html 正文中时,我遇到了同样的问题。我需要将脚本标签包装在另一个标签中吗?
  • 没关系,我只是粗暴地处理了脚本标签。谢谢
【解决方案2】:

就像 Jeremy J Starcher 所说,document.body 还没有退出。此外,您的变量 heroReady 至少没有定义...... 查看this 获取该教程的完整代码。

<!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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多