var game = {
width: 640,
height: 360,
ctx: undefined,
platform: undefined,
sprites: {
background: undefined,
platform: undefined
},
init: function() {
var canvas = document.getElementById('mycanvas');
this.ctx = canvas.getContext('2d');
},
load: function(callback) {
let loaded = 0;
for (var key in this.sprites)
loaded++;
for (var key in this.sprites) {
this.sprites[key] = new Image();
this.sprites[key].src = 'img/' + key + '.png';
var _this = this;
this.sprites[key].onload = function(){
if(--loaded <= 0)
callback.apply(_this,null);
}
}
},
start: function() {
this.init();
this.load(this.run);
},
render: function() {
this.ctx.clearRect(0, 0, this.width, this.height);
this.ctx.drawImage(this.sprites.background, 0, 0);
this.ctx.drawImage(this.sprites.platform, 0, 0, 100, 20);
},
run: function() {
this.render();
requestAnimationFrame(()=>this.run.apply(this,null));
}
};
game.start();
var game = {
width: 640,
height: 360,
ctx: undefined,
platform: undefined,
sprites: {
background: undefined,
platform: undefined
},
init: function() {
var canvas = document.getElementById('mycanvas');
this.ctx = canvas.getContext('2d');
},
load: function(callback) {
let loaded = 0;
for (var key in this.sprites)
loaded++;
for (var key in this.sprites) {
this.sprites[key] = new Image();
this.sprites[key].src = 'img/' + key + '.png';
this.sprites[key].onload = function(){
if(--loaded <= 0)
callback();
}
}
},
start: function() {
this.init();
this.load(this.run);
},
render: function() {
this.ctx.clearRect(0, 0, this.width, this.height);
this.ctx.drawImage(this.sprites.background, 0, 0);
this.ctx.drawImage(this.sprites.platform, 0, 0, 100, 20);
},
run: function() {
game.render();
requestAnimationFrame(game.run);
}
};
game.start();