I have the charge method defined for MainCharacter class and inside it, another onload function for the image.
var MainCharacter = function(canvas, X, Y) {
this.canvas = canvas;
this.X = (typeof X === "undefined") ? 10 : X;
this.Y = (typeof Y === "undefined") ? 10 : Y;
this.frameX = 0;
this.frameY = 0;
this.frameXWidth = this.frameX + 32;
this.frameYHeight = this.frameY + 33;
this.img = document.createElement('img');
this.img.src = 'assets/img/maincharacter.png';
};
MainCharacter.prototype.charge = function() {
this.img.onload = function() {
self.canvas.drawImage(self.img, 0, 0, 55, 55, 12, 12, 80, 80);
};
}
My problem is that this closure is not taking the object, and I am doing the next to solve the problem:
MainCharacter.prototype.charge = function () {
var self = this;
var img = this.img;
img.onload = function () {
self.canvas.drawImage(self.img, 0, 0, 55, 55, 12, 12, 80, 80);
};
}
How I can solve this properly?