简体   繁体   English

使用RequireJS加载PreloadJS

[英]Loading PreloadJS using RequireJS

I am new in using RequireJS in loading modules especially loading libraries like CreateJS. 我是在加载模块时使用RequireJS的新手,尤其是在加载诸如CreateJS之类的库时。 I wanted to use PreloadJS with SoundJS. 我想将PreloadJS与SoundJS一起使用。 I have properly loaded SoundJS via RequireJS and I am not having problem so far. 我已经通过RequireJS正确加载了SoundJS,到目前为止我还没有遇到任何问题。 What I am having problem with is the PreloadJS. 我遇到的问题是PreloadJS。 I load it using this way: 我使用这种方式加载它:

require.config(
  {
    paths :
    {
        soundjs   : 'core/soundjs-0.6.1.min'
      , preloadjs : 'core/preloadjs-0.6.1.min'
    }

    ,shim :
    {
        'preloadjs' : { exports : "createjs" }
      , 'soundjs'   : { exports : "createjs.Sound" }
    }
  });

require(['src/MainGameScene' , 'src/Runner' , 'core/pixi.js' , 'core/tween.min' , 'preloadjs' , 'soundjs' ]
, function(MainGameScene , Runner , PIXI, TWEEN , PreloadJS , SoundJS)
{
  console.log("Endless Runner modules loaded.");

  var screenSize = { width : 960 , height : 500};
  var renderer = PIXI.autoDetectRenderer(screenSize.width , screenSize.height);

  new PIXI.loaders.Loader()
    .add("_assets/textures/p1_walk/Von.json")
    .add("_assets/textures/p2_walk/Don.json")
    .add("_assets/textures/p3_walk/Bon.json")
    .add("_assets/textures/tiles.json")
    .add("_assets/textures/textures.json")
    .once("complete" ,
      function()
      {
        var queue = new PreloadJS();
        SoundJS.alternateExtensions = ["mp3" , "ogg" , "wav" ];
        queue.installPlugin(SoundJS);
        queue.addEventListener("complete" , onFinishedLoading);
        queue.loadManifest(
          [
            {id : "bgm1"  , src : "_assets/bgm/bgm.mp3"}
           ,{id : "jump" , src : "_assets/sfx/jump.wav" }
           ,{id : "pickupcoin" , src : "_assets/sfx/pickupcoin.wav" }
          ]);
      })
    .load();

  function onFinishedLoading()
  {
    new MainGameScene(renderer , screenSize);
  }

  document.body.appendChild(renderer.view);
});

When I run the debugger it shows me that the PreloadJS object has this properties which I believe is not part of PreloadJS. 当我运行调试器时,它告诉我PreloadJS对象具有此属性,我相信这不是PreloadJS的一部分。 I also checked if the prototype is correct but I am having Object as its prototype: 我还检查了原型是否正确,但是我将Object作为其原型:

noConflict: function()
parse : function parse()
runInContext : function a(b, d)
stringify : function stringify()
__proto__ : Object

What else did I miss? 我还想念什么? Also I tried to shim PreloadJS like this: 我也试图像这样填充PreloadJS:

    'preloadjs' : { exports : "createjs.LoadQueue" }
  , 'soundjs'   : { exports : "createjs.Sound" }

But I still get an object with those properties. 但是我仍然得到具有这些属性的对象。 I need RequireJS so I can have my SoundJS with a solid preloader. 我需要RequireJS,因此我的SoundJS可以带有坚固的预加载器。 I cannot find any articles of using PreloadJS with RequireJS so definitely I believe I am doing something wrong, unconventional and undocumented so I will appreciate any help. 我找不到任何有关将PreloadJS与RequireJS一起使用的文章,因此绝对可以相信我做错了,非常规的和未记录的事情,因此,我将感谢您的帮助。

Okay, got it worked out. 好的,解决了。 It's less than ideal but preloadjs is not written with AMD in mind. 它不理想,但是preloadjs并不是为AMD而写的。 If your project absolutely requires you to include your dependencies via bower or something than this might be sub-optimal but in the face of absolutely no other fix I'm pretty content with this. 如果您的项目绝对要求您通过Bower或其他方式包含依赖项,那么这可能不是最佳选择,但是面对绝对没有其他修复方法,我对此非常满意。

Basically, take your version of preloadjs, and wrap the entire file in a define() call: 基本上,使用您的preloadjs版本,并将整个文件包装在define()调用中:

define(function(){
    //Paste the contents of preloadjs here
    //After all the preloadjs code you need to return the reference to createjs:
    return this.createjs
});

Add this version to your dependency list and everything should work as normal. 将此版本添加到您的依赖项列表中,一切都将正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM