Skip to content

CCLoader error when preload resource twice #576

Closed
robrechtclaeys opened this Issue Oct 12, 2012 · 3 comments

2 participants

@robrechtclaeys

If you preload resources at runtime and you happen to preload a resource twice, the loader ends up in an infinite loop.

eg:

cc.TextureCache.getInstance().addImage(filename);
will call
cc.Loader.shareLoader().onResLoaded();
which increments loadedResourceCount in the cc.Loader

when later using cc.Loader.shareLoader().preload(res) , loadedResourceCount will be higher than resourceCount, so the isLoadedComplete never triggers the onLoad

Or am I doing something wrong?

@linshun
linshun commented Oct 22, 2012

The cc.Loader.shareLoader().preload(res) should be called before game runs. It is not allowed to call cc.TextureCache.getInstance().addImage() before preload all resources.

BTW, the preloading will be modified to support multi scene loading before next release. So the resources can be loaded by several times, and it will speed up the first time game play.

@robrechtclaeys

Thanks for the reply.

I use ajax to change my site's content, so I never get a "full page refresh". Because of that I was reusing my application and came in the situation where I had previously preloaded resources and wanting to preload some other resources.

See it as a selection-screen for two games which are both cc.Scenes. I did not want to preload the resources of the scene before the user actually selected that game. I guess that the modification you mentioned will solve this problem.

Now I created a workaround where I "unload" the whole application, removing the canvas, clearing cc, etc. when I leave a game. When selecting a new game everything of cocos is initialized as if new. The "unloading" is still a bit messy, but I did not know how to unload cocos without an actual page refresh. Now I do the following:

try
    {
      // remove all cocos script tags (not-engine)
      if(document.querySelector('#cocos2d-html5') != undefined)
      {
        var appFiles = document.querySelector('#cocos2d-html5').c.appFiles;
          $("script").each(function(index){
          var script = $(this);
            for(var i=0;i<appFiles.length;++i)
            {
              if(script.attr("src") != undefined && script.attr("src") == appFiles[i])
              {
                script.remove();
                break;
              }
            }
        });

        // remove all cocos engine script tags - i'm using the engine dir (which are located assets/js/cocos2d/) instead of the singleEngineFile

        // note: jsloader also refers to main.js, dunno why it must be located on the root, I would rather see it removed from the jsloader. It's project-specific, so I would expect it to be part of the appFiles. But this note is in fact related to issue 524.

        $("script").each(function(index){
          var script = $(this);
          if(script.attr("src") != undefined && (script.attr("src").indexOf("assets/js/cocos2d/") != -1 || script.attr("src") == ("main.js")))
          {
            script.remove();
          }
        });
      }

      // destroy window stuff ("general memory")  --> bit messy, this give an error in debugger, but not fatal, so i'm ignoring it atm
      cc = null;
      g_ressources = null;
    }
    catch(error){alert("error destroying game")}

@linshun
linshun commented Feb 25, 2013

It has been fixed. Moreover, it supports multi scenes resources preloading.

@linshun linshun closed this Feb 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.