Allora il mio problema è che ho creato la classe fixed_object (che eredita da object_2d):
codice:
//simula l'eredità
if (typeof Function.prototype.inherits !== 'function')
{
Function.prototype.inherits = function(superclass){
this.prototype = superclass.prototype;
superclass.call(this.prototype);
};
}
var object_2d = function(){
this.x = 0;
this.y = 0;
this.width = 0;
this.height = 0;
};
var fixed_image = function(source){
this.img = new Image;
this.img.src = source;
if(navigator.appName == "Microsoft Internet Explorer")
{
this.img.attachEvent("onload", getdimensionIE.call(this));
}
else
{
this.width = this.img.width;
this.height = this.img.height;
}
};
fixed_image.inherits(object_2d);
var getdimensionIE = function()
{
this.width = this.img.width;
this.height = this.img.height;
};
Questo codice è in un file a parte (classi.js). Poi nella pagina vera e propria (prova.html) dichiaro:
codice:
var bar = new fixed_image("rep_bar.png");
e vorrei che impostasse sempre le dimensioni (che mi servono per gestire delle collisioni su canvas) in maniera autonoma.
Solo che mi serve che il codice sia espandibile. Per cui devo necessariamente utilizzare attachEvent() e addEventListener() per far sì che poi da altri script possa comunque aggiungere qualche altra cosa, in questo caso:
codice:
if(navigator.appName == "Microsoft Internet Explorer")
bar.img.attachEvent("onload", function(){
context.drawImage(bar.img,bar.x,bar.y);
});
else
{
bar.img.onload = function(){
bg.drawImage(bar.img,bar.x,bar.y);
};
}
Tralasciando il fatto che ho utilizzato il vecchio sistema per browser diversi da IE (poi anche lì farò assegnare width e height nell'evento onload).
Forse il problema è l'assegnazione del link dell'immagine prima di aver assegnato gli eventi onload, però per come ho strutturato il programma non posso metterlo dopo aver dichiarato tutti gli eventi. Comunque sia ho fatto delle prove e vengono richiamati tutti, è per questo che penso che il problema abbia a che fare con IE.
Ripeto il programma funziona perfettamente su Chrome e Firefox e anche su IE se non fosse per width e height.