I am trying to create a game with javascript but it doesn't works. In the developers tools the error is the following: "Uncaught TypeError: Cannot read property 'x' of undefined". So I suppose the error involve the nested array "circles".
This is the code:
<!DOCTYPE html>
<html>
<head>
<title>Canvas Tutorial</title>
<script type="text/javascript">
window.onload = draw;
var circles = [{x:200,y:150,r:40,direction:1,speedX:1,speedY:2},{x:200,y:150,r:70,direction:1,speedX:2,speedY:1}];
for (var i=0; i<circles.length; i++) {
function bottomRight() {
circles[i].x += circles[i].speedX;
circles[i].y += circles[i].speedY;
}
function upLeft() {
circles[i].x -= circles[i].speedX;
circles[i].y -= circles[i].speedY;
}
function upRight() {
circles[i].x += circles[i].speedX;
circles[i].y -= circles[i].speedY;
}
function bottomLeft() {
circles[i].x -= circles[i].speedX;
circles[i].y += circles[i].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(circles[i].x,circles[i].y,circles[i].r,0,Math.PI*2,false);
ctx.fill();
if((circles[i].y > 300 - circles[i].r) && circles[i].direction ===1){
circles[i].direction = 2;
} else if((circles[i].x > 400 - circles[i].r) && (circles[i].direction===2)) {
circles[i].direction = 3;
} else if ((circles[i].y > 300 - circles[i].r) && (circles[i].direction===4)) {
circles[i].direction = 3;
} else if ((circles[i].y <= circles[i].r) && circles[i].direction === 3) {
circles[i].direction = 4;
} else if ((circles[i].x < circles[i].r) && circles[i].direction === 4){
circles[i].direction = 1;
} else if ((circles[i].y < circles[i].r) && circles[i].direction === 2) {
circles[i].direction = 1;
}
if (circles[i].direction === 1) {
bottomRight();
} else if (circles[i].direction === 2) {
upRight();
} else if (circles[i].direction === 3) {
upLeft();
} else {
bottomLeft();
}
}
setTimeout(draw, 10);
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
</body>
</html>
How can I fix the code?