javascript - Load Images from within objects with array -
i have class i've created, called sprite:
var sprite = function sprite() { = this; that.xpos = 0; that.ypos = 0; … that.image = null; this.render =function() { … } this.setimage(filename) { that.image = new image(); that.image.src = filename; } }
and create array of objects:
var names=[ {filename:"a1.png"}, {filename:"a2.png"}, {filename:"a3.png"}, {filename:"a4.png"} ]; var objs = []; for(var l=0;l<names.length;l++) { objs.push({}); objs[l] = new sprite(); … setimage(names[l]); }
every object in array point same image (with same image file.)
can tell me i'm doing wrong here?
is there better way can this?
thanks.
your setimage(names[l]); in loop seems overwriting hence same image, try doing like:
var sprite = function sprite() { = this; that.xpos = 0; that.ypos = 0; that.image = null; this.setimage = function(filename) { that.image = new image(); that.image.src = filename; }; } var names=[ {'filename':"a1.png"}, {'filename':"a2.png"}, {'filename':"a3.png"}, {'filename':"a4.png"} ]; var objs = []; for(var l=0;l<names.length;l++) { var sp = new sprite(); //set image new sprite object sp.setimage(names[l].filename); objs.push(sp); //push sp object objs array } console.log( objs );
Comments
Post a Comment