2017-06-10 3 views
-2
<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
<style> 
canvas { 
    border:1px solid #d3d3d3; 
    background-color: #f1f1f1; 
} 
</style> 
</head> 
<body> 
<canvas id="GameArea"></canvas> 
<script> 

var myGamePiece; 

myGamePiece = new component(30, 30, "red", 10, 120); 




function component(width, height, color, x, y) { 
    console.log("component called"); 
    this.width = width; 
    this.height = height; 
    this.x = x; 
    this.y = y; 
    var myGameArea=document.getElementById("startGame"); 
    console.log("myGameArea"); 
    var ctx = myGameArea.getContext("2d"); 
    console.log("crossed"); 
    ctx.fillStyle = color; 
    ctx.fillRect(this.x, this.y, this.width, this.height); 
} 

</script> 

<p>We have added a component to our game, a red square!</p> 

</body> 
</html> 

//ではない私は、コンポーネントの名前、いくつかの関数を呼び出すに四角形を作成しようとした上記のコードでは、私の誤り のgetoutすることのproperty'getコンテキスト」を読み取ることができないのエラーを取得しています。 コンテキストで矩形を作成しましたが、エラーが表示されます私はヌル

+2

あなた自身でコードをデバッグすることを忘れないでください。エラーメッセージを読んで理解してください。 'startGame'は' GameArea'と同じではありません。 – Xufox

答えて

0

キャンバスのIDは「GameArea」ですが、getElementById( "startGame")を実行しようとしています。

0

あなたのキャンバスの要素IDは「GameArea」ですが、コードでは「startGame」と呼ばれます。だからこそnullです

関連する問題