我正在尝试编写一个rpg游戏,但是当我第一次开始编写代码时,它说fillRect
不是一个函数。现在,我已经使用fillRect
了很多次,但是从未遇到过此错误。如果您能帮助我,那就太好了!
我的代码:
<!DOCTYPE html>
<html><head>
<script type="text/javascript">
var ctx = null;
var tileW = 40,
tileH = 40;
var mapW = 10,
mapH = 10;
var currentSecond = 0,
frameCount = 0,
frameLastSecond = 0;
var gameMap =
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 0, 1, 1, 1, 1, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 1, 1, 0, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
window.onload = function ()
{
ctx = document.getElementById('game');
requestAnimationFrame(drawGame);
ctx.font = "bold 18pt sans-serif";
};
function drawGame ()
{
if(ctx == null){return;}
var sec = Math.floor(Date.now()/ 1000);
if(sec!= currentSecond)
{
currentSecond = sec;
frameLastSecond = frameCount;
frameCount = 1;
}
else { frameCount++;}
for(var y = 0; y < mapH; y++)
{
for(var x = 0; x < mapW; x++)
{
switch(gameMap[((y*mapW)+x)])
{
case 0:
ctx.fillStyle = "#999999";
break;
default:
ctx.fillStyle = "#eeeeee";
}
ctx.fill(x*tileW, y*tileH, tileW, tileH);
}
}
ctx.fillStyle = "#ff0000";
ctx.fillText("FPS: " + frameLastSecond, 10, 20);
requestAnimationFrame(drawGame);
}
</script>
</head><body>
<canvas id="game" width="400" height="400"></canvas>
</body></html>
我还没有找到解决方法。因此,如果这个社区中的任何人都可以帮助我–那将很棒。
答案 0 :(得分:1)
您的代码中有两个错误:
ctx = document.getElementById('game');
或var canvas = document.getElementById('game'); ctx = canvas.getContext('2d');
来代替ctx = document.getElementById('game').getContext('2d');
ctx.fill(x * tileW, y * tileH, tileW, tileH);
来代替ctx.fillRect(x * tileW, y * tileH, tileW, tileH);
我为您更正了
<!DOCTYPE html>
<html><head>
<script type="text/javascript">
var ctx = null,
tileW = 40,
tileH = 40,
mapW = 10,
mapH = 10,
currentSecond = 0,
frameCount = 0,
frameLastSecond = 0;
var gameMap =
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 0, 1, 1, 1, 1, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
0, 1, 0, 1, 0, 0, 0, 1, 1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 1, 1, 0, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
window.onload = function()
{
// here was the first mistake:
ctx = document.getElementById('game').getContext('2d');
requestAnimationFrame(drawGame);
ctx.font = "bold 18pt sans-serif";
};
function drawGame()
{
if (ctx == null) {return;}
var sec = Math.floor(Date.now()/ 1000);
if(sec!= currentSecond)
{
currentSecond = sec;
frameLastSecond = frameCount;
frameCount = 1;
}
else
{
frameCount++;
}
for(var y = 0; y < mapH; y++)
{
for(var x = 0; x < mapW; x++)
{
switch(gameMap[((y*mapW)+x)])
{
case 0:
ctx.fillStyle = "#999999";
break;
default:
ctx.fillStyle = "#eeeeee";
}
// here was the second mistake:
ctx.fillRect(x * tileW, y * tileH, tileW, tileH);
}
}
ctx.fillStyle = "#ff0000"
ctx.fillText("FPS: " + frameLastSecond, 10, 20);
requestAnimationFrame(drawGame);
}
</script>
</head><body>
<canvas id="game" width="400" height="400"></canvas>
</body></html>