stage.draw();
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();
}
}
},
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++;
this.sprites[key] = new Image();
this.sprites[key].src = 'img/' + key + '.png';
this.sprites[key].onload = function() {
if (--loaded <= 0)
callback();
}
}
},
// load: function() {
//
// for (var key in this.sprites) {
// this.sprites[key] = new Image();
// this.sprites[key].src = 'img/'+ key + '.png';
// console.log(this.sprites[key]);
// }
//
// // this.sprites.background = new Image();
// // this.sprites.background.src = 'img/bg-main.png';
// // this.sprites.platform = new Image();
// // this.sprites.platform.src = 'img/pl.png';
// },
start: function() {
this.init();
this.load(this.run);
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, this.platform.x, this.platform.y, 100, 20);
},
run: function() {
this.render();
window.requestAnimationFrame(function() {
game.run();
});
}
};
game.platform = {
x: 280,
y: 320,
}
window.addEventListener('load', function() {
game.start();
});