Here is my small attempt at working with Game Loop. Here is the Demo. (keep clicking in gray area to start it).
The issue is that the game animations get choppy after clicking for a while, and reach to a point where it's like a stop motion thing, and after that I need to kill the browser tab.
Surely there is something I'm missing here. May be something like a delay that needs to be inserted when the game is idle.
Here is the code that i've been working on:
HTML
<div class="container">
<div class="player"></div>
</div>
JavaScript
var player = {
height: 0,
width: 0,
image: "",
score: 0,
highestScore: 0
};
var newColor=null;
/*Game loop starts here*/
var gameLoop = function () {
$(".container").on("click", function () {
$(".player").stop().animate({
bottom: "+=100px"
}, 300, function () {
$(".player").animate({
bottom: "0"
}, 800);
});
});
/* some more calls to updating player properties etc etc will come here*/
};
/*Game loop ends here*/
/*Run the Game Loop*/
setInterval(gameLoop, 16);
To state my question:
How do I prevent the lag that I'm experiencing as the game progresses?