javascript - ctx.fillRect 不是函数
问题描述
我正在尝试编写一个 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>
同样,我还没有找到解决方案。因此,如果这个社区中的任何人都可以帮助我,那就太好了。
解决方案
您的代码中有两个错误:
- 而不是
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>
推荐阅读
- ios - 如何处理从另一个应用程序共享到我自己的 iOS 应用程序的文件?
- javascript - 如何使用 Javascript 滚动到页面底部但使用 selenium c# 有一些延迟
- java - 如何将一个数组的元素插入第二个数组?
- javascript - 从反应项目中分离firebase实时数据库
- next.js - 在带有 ISR 和 SWR 的 nextjs 应用程序中使用反冲设置初始状态
- javascript - 直接在 JSX 中样式,当值改变时改变
- python - 检查时间戳列值是否在每个组的数据范围内
- wix - WiX 工具集 - 在自定义操作中运行 cmd 的问题
- php - 如何使用 PHP 用一个 mysqli_query 查询更改多个表?
- elasticsearch - 通过 elasticsearch 搜索字符串关键字