• Jump To … +
    CONTRIBUTING.md MIT-LICENSE.md README.js.md audio.js audio/null_sound.js audio/sound.js audio/sound_instance.js core.js graphics.js graphics/animation.js graphics/canvas.js graphics/image.js graphics/sprite.js graphics/sprite_sheet.js keyboard.js media.js mouse.js timer.js timer/after.js timer/every.js timer/tween.js touch.js
  • ¶

    media.js

    (function() {
  • ¶

    Luv.Media

    Luv.Media = Luv.Class('Luv.Media', {
  • ¶

    This module creates the media object when you create a luv game. It's usually instantiated by the Luv function.

      var luv = Luv();
      luv.media // this is the media object

    The media object is an "asset manager". It keeps track of those assets (i.e. images) that load asynchronously, or can fail to load.

      init: function() {
        this.pending = 0;
        this.loaded = true;
      },
  • ¶

    isLoaded returns true if all the assets have been loaded, false if there are assets still being loaded. Useful to wait actively until all assets are finished loading:

      var luv = Luv();
      var dogImage;
      luv.load = function() {
        dogImage = luv.graphics.Image('dog.png');
      }
      luv.draw = function() {
        // wait until all images are loaded before drawing anything
        if(!luv.media.isLoaded()) { return; }
        luv.graphics.drawImage(dogImage, 100, 100);
      }
      isLoaded     : function() { return this.loaded; },
  • ¶

    Returns the numbers of assets that are loading, but not yet ready

      getPending   : function() { return this.pending; },
  • ¶

    onAssetLoaded is an overridable callback. It will be called once for each asset (Image, Sound, etc) that is loaded. You may use it for things like displaing a "loaded percentage"

      luv.media.onAssetLoaded = function(asset) {
        assetsLoaded += 1;
      };
      onAssetLoaded: function(asset) {},
  • ¶

    onAssetError is an overridable callback that will be called when an asset can not be loaded (for example, the path to an image does not exist) By default, it throws an error

      onAssetError  : function(asset) { throw new Error("Could not load " + asset); },
  • ¶

    onLoaded is an overridable callback that will be called when the last pending asset is finished loading you can use it instead of isLoaded to control the game flow

      onLoaded     : function() {},
  • ¶

    Pseudo-Internal function. Registers the asset in the media object.

      newAsset  : function(asset) {
        this.pending++;
        this.loaded = false;
        clearTimeout(this.onLoadedTimeout);
        asset.status = "pending";
      },
  • ¶

    Pseudo-internal function. Assets that have been loaded successfully should call this function (this will trigger onAssetLoaded, etc)

      registerLoad : function(asset) {
        this.pending--;
    
        asset.status = "loaded";
    
        this.onAssetLoaded(asset);
        if(this.pending === 0) {
          var self = this;
          clearTimeout(this.onLoadedTimeout);
          this.onLoadedTimeout = setTimeout(function() {
            self.loaded = true;
            self.onLoaded();
          }, Luv.Timer.ONLOAD_TIMEOUT);
        }
      },
  • ¶

    Pseudo-internal function. Assets that can't be loaded must invoke this method

      registerError: function(asset) {
        this.pending--;
    
        asset.status = "error";
    
        this.onAssetError(asset);
      }
    });
    
    Luv.Timer.ONLOAD_TIMEOUT = 200;
  • ¶

    This just a method holding object, to be extended by specialized assets like Image or Sound. Usage:

      MyAwesomeClass.include(Luv.Media.Asset)

    See Luv.Graphics.Image for an example.

    Luv.Media.Asset = {
      isPending: function() { return this.status == "pending"; },
      isLoaded:  function() { return this.status == "loaded"; },
      isError:   function() { return this.status == "error"; }
    };
    
    }());