私は、SublimeでHTMLとJavascriptを使用してゲームを作成したいと思います。 しかし、Javascriptでネストされた配列を作成しようとしていますが、ブラウザ上で動作しないコードを実行します。配列 "円"には "ボール"が含まれている必要があります(この行を削除すると、ゲームには1つのボールだけが含まれ、動作します)。JavaScriptでネストされた配列を作成する
これは、ゲームのコードです:
<!DOCTYPE html>
<html>
<head>
<title>Canvas Tutorial</title>
<script type="text/javascript">
window.onload = draw;
x = 200;
y = 150;
r = 40;
direction = 1;
speedX = 1;
speedY = 2;
var circles = [{x:200,y:150,r:40,d:1,speedX=1,speedY=2},{x:200,y:150,r:40,d:1,speedX=1,speedY=2}];
function bottomRight() {
x += speedX;
y += speedY;
}
function upLeft() {
x -= speedX;
y -= speedY;
}
function upRight() {
x += speedX;
y -= speedY;
}
function bottomLeft() {
x -= speedX;
y += speedY;
}
function draw() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.fillRect(0,0,400,300);
ctx.fillStyle = "yellow";
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2,false);
ctx.fill();
if((y > 300 - r) && direction ===1){
direction = 2;
} else if((x > 400 - r) && (direction===2)) {
direction = 3;
} else if ((y > 300 - r) && (direction===4)) {
direction = 3;
} else if ((y <= r) && direction === 3) {
direction = 4;
} else if ((x < r) && direction === 4){
direction = 1;
} else if ((y < r) && direction === 2) {
direction = 1;
}
if (direction === 1) {
bottomRight();
} else if (direction === 2) {
upRight();
} else if (direction === 3) {
upLeft();
} else {
bottomLeft();
}
}
setInterval(draw, 10);
</script>
</head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
</body>
</html>
は、どのように私は、コードを修正することができますか?
リスナーは少し生産的ではありません。 'window.addEventListener(" load "、function(); draw(); setInterval(draw、10);});または' setTimeout(draw、10);を 'draw'の中に置き、間隔を削除します。 – Xufox
あなたのコードで 'circle 'をどのように使うのか分かりません。明確にすることはできますか? –