There is a stackoverflow post by a guy called Gustavo Carvalho that shows how camera/viewport works in a HTML5 game.
All's good except that setInterval is used instead of requestAnimationFrame.
I tried converting to requestAnimationFrame without success :-(
Can somebody pls help? Here's the post:
Simple HTML5 game camera/viewport
Thanks very much!
EDIT: After reviewing the answers below, I came up with this solution:
REPLACE THE FOLLOWING CODE...
// Game Loop
var gameLoop = function(){
update();
draw();
}
// <-- configure play/pause capabilities:
var runningId = -1;
Game.play = function(){
if(runningId == -1){
//Use setInterval instead of requestAnimationFrame for compatibility reason
runningId = setInterval(function(){
gameLoop();
}, INTERVAL);
console.log("play");
}
}
Game.togglePause = function(){
if(runningId == -1){
Game.play();
}
else
{
clearInterval(runningId);
runningId = -1;
console.log("paused");
}
}
// -->
REPLACE WITH THIS ONE...
// Game Loop
var gameLoop = function(){
if(gameStatus == 'play'){
update();
draw();
}
window.requestAnimationFrame(gameLoop);
}
var gameStatus = 'play';
Game.play = function() {
gameLoop();
}
Game.togglePause = function() {
if(gameStatus == 'play'){
gameStatus = 'pause';
console.log(gameStatus);
}
else if(gameStatus == 'pause'){
gameStatus = 'play';
console.log(gameStatus);
}
}