エラーがでてしまい、原因がわかりません。
snakeBite 風のゲームだぞうですが、エラーメッセージが英語でわかりません。
エラーメッセージは
Uncaught TypeError: Failed to execute 'fillText' on
'CanvasRenderingContext2D' : 3 arguments required, buf only 2 preset.
です。109行目が悪いようですが、タイプミスはしてないし、わかりません。
以下がソースコード(長文)になります。
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>SnakeBite</title>
<meta heep-equiv="X-UA-Compatilbe" content="IE=edge" />
<script type="text/javascript">
var W, H, S = 20;
var snake = [], foods = [];
var keyCode = 0;
var point = 0;
var timer = NaN;
var ctx;
// Pointオブジェクト
function Point(x, y) {
this.x = x;
this.y = y;
}
// 初期化関数
function init() {
var canvas = document.getElementById('field');
W = canvas.width / 8;
H = canvas.height / 8;
ctx = canvas.getContext("2d");
ctx.font = "20px sans-serif";
// 蛇の初期化
snake.push(new Point(W / 2, H / 2));
// えさの初期化
for (var i = 0; i < 10; i++) {
addFood();
}
timer = setInterval("tick()", 200);
window.onkeydown = keydown;
}
// 餌の追加
function addFood() {
while( true ) {
var x = Math.floor(Math.random() * W );
var y = Math.floor(Math.random() * H );
if( isHit(foods, x, y ) || isHit(snake, y, y ) ) {
continue;
}
foods.push( new Point(x,y) );
break;
}
}
// 衝突判定
function isHit( data, x, y ) {
for( var i = 0; i<data.length; i++ ) {
if( data[i].x == x && data[i].y == y ) {
return true;
}
}
return false;
}
function moveFood( x, y ) {
foods = foods.filter( function(p) {
return ( p.x != x || p.y != y );
} );
addFood();
}
function tick() {
var x = snake[0].x;
var y = snake[0].y;
switch( keyCode ) {
case 37: x--; break; // 左
case 38: y--; break; // 上
case 39: x++; break; // 右
case 40: y++; break; // 下
default: paint(); return;
}
// 自分 or 壁に衝突?
if( isHit(snake,x,y) || x < 0 || x>= W || y < 0 || y >= H ) {
clearInterval(timer);
paint();
return;
}
// 頭を先頭に追加
snake.unshift( new Point(x,y) );
if( isHit(foods, y, y)) {
point += 10; // 餌を食べた
moveFood(x,y);
} else {
snake.pop(); // 食べていないー>しっぽを削除
}
paint();
}
function paint() {
ctx.clearRect(0, 0, W * S, H * S);
ctx.fillStyle = "rgb(256,0,0)";
ctx.fillText( point. S, S * 2 );
ctx.fillStyle = "rgb(0,0,255)";
foods.forEach(function(p) {
ctx.fillText("+", p.x * S, (p.y + 1 ) * S );
} );
snake.forEach(function(p) {
ctx.fillText("*", p.x * S, (p.y + 1 ) * S );
} );
}
function keydown(event) {
keyCode= event.ekyCode;
}
</script>
</head>
<body onload="init()">
<canvas id="field" width="400" height="400" style="background:#cccccc">
</canvas>
</body>
</html>