I'm creating a simple game using the HTML5 canvas and the jCanvaScript library.
//JavaScript
function start() {
jc.start("drawingCanvas", 380);
text = jc.text('score: ' + score, 10, 10);
clock = jc.text(round / 2, 285, 145);
var millisecondsToWait = 500;
inter = setInterval(nextRound, millisecondsToWait);
}
function nextRound() {
round--;
if(round % 2 == 0){
clock.string(round / 2);
}
if (round == 0) {
endGame();
} else {
draw();
}
}
function draw() {
var shape = jc.circle(random() * 300, random() * 100, random() * 10, randomRgba(), 1);
shape.click(destroy);
shape.animate({
x : random() * 300,
y : random() * 100,
radius : random() * 100
}, random() * 2000);
}
function destroy() {
score += Math.round(100 - this._radius);
text.string('score: ' + score);
this.color('#ff0000');
this.animate({
radius : 0
}, 1000);
}
the html is too easy:
<!DOCTYPE HTML>
<html>
<head>
<script src="http://jcscript.com/data/js/jCanvaScript.1.5.18.min.js"></script>
<script src="js/canvasWithLibraries.js"></script>
<link rel="stylesheet" href="css/canvaslib.css" />
<title>Canvas with libraries demo</title>
</head>
<body>
<canvas id="drawingCanvas">your browser does not support the canvas element</canvas>
</body>
</html>
QUESTION:
As you can see from the code, everytime I click on a shape, the shape should turn red and grow smaller until it vanishes. This seems to be working, sometimes. When you test this code, you'll notice that the click seems to respond on different shapes or not at all or just random. Does anyone have an idea what the problem is?
I'm guessing this is all on a single thread and the loop doens't get interrupted properly.