mercredi 29 avril 2015

Javascript if statement, function is not called

I found a very nice code here to preload images into application. Which works great.

// PRELOAD IMAGES
function loadImages(sources, callback) {

  var loadedImages = 0;
  var numImages = 0;
  // get num of sources
  for(var src in sources) {
    numImages++;
  }
  for(var src in sources) {
    images[src] = new Image();
    images[src].onload = function() {
      if(++loadedImages >= numImages) {
        callback(images);
      }
    };
    images[src].src = sources[src];
  }
}


var sources;
sources = {

  img1: "....../img1.png",
  img2: "....../img2.png",

};

But when i try to call loadImages() from if statement inside for loop, for whatever reasons the function is not called. Everything else works as intented i.e. iteration, conditions, etc. Code is below:

      if (data[j].FlImg === 'img2') {

    alert (j) // just to be sure it gets here, it does.

    loadImages(sources, function (images) {

      alert(j +'-this line is not executing')

      ctxGameBoard.drawImage(images.img2, 0, 0, 100, 100);

    });

Any suggestions greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire