【发布时间】:2014-09-13 10:41:57
【问题描述】:
我是 html5 画布游戏开发新手,我可能遇到了新手问题。
我正在制作一个基于图块的地图,该地图应该将二维数组转换为带有墙壁和开放空间的地图,但每当我打开游戏时它就不会显示... 我什至没有收到错误!?
请帮助我(我正在使用 chrome BTW)
粘贴码:http://pastebin.com/5GcQwCVa#
// Declares global variables
var canvas = document.createElement("canvas");
c = canvas.getContext("2d"),
make = {},
maps = {},
width = 800,
height = 600;
// Creates the requestAnimationFrame variable
(function () {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
}) ();
// Modifies the canvas' properties
canvas.width = width,
canvas.height = height;
// 2D arrays that make up maps
maps = {
one: [
["w","w","w","w","w","w","w","w"],
["w","o","o","o","o","o","o","w"],
["w","o","w","w","w","w","o","w"],
["w","o","w","o","o","o","o","w"],
["w","o","w","o","w","o","o","w"],
["w","o","w","o","o","w","o","w"],
["w","o","o","o","o","o","o","w"],
["w","w","w","w","w","w","w","w"]
],
two: [
["w","w","w","w","w","w","w","w"],
["w","o","o","o","o","o","o","w"],
["w","o","o","o","o","o","o","w"],
["w","o","o","o","o","o","o","w"],
["w","o","o","o","o","o","o","w"],
["w","o","o","o","o","o","o","w"],
["w","o","o","o","o","o","o","w"],
["w","w","w","w","w","w","w","w"]
]
};
// Maps drawing functions
make = {
map: function ( map2d ) {
var i,
j,
tile,
tilesX = 8,
tilesY = 8;
for (i = 0; i < tilesX; i++) {
for(j = 0; j < tilesY; j++) {
if (map2d[i][j] === "w") {
this.tile(i * 64, j * 64);
}
}
}
},
tile: function (x, y, TD) {
switch (TD) {
case "w":
c.rect(x, y, 64, 64);
c.fillStyle = wallColor;
c.fill();
c.lineWidth = 8;
c.strokeStyle = "black";
c.stroke();
break;
case "o":
c.rect(x, y, 64, 64);
c.fillStyle = "white";
c.fill();
c.lineWidth = 8;
c.strokeStyle = "white";
c.stroke();
break;
}
}
}
// Updates constantly
function update () {
c.clearRect(0, 0, width, height);
make.map(maps.two);
requestAnimationFrame(update);
}
// Begins updating when window is ready
window.addEventListener("load", function () {
update();
});
【问题讨论】:
标签: javascript html canvas tiles