[Resolved] TextureCache's addImageAsync behavior question

[Resolved] TextureCache's addImageAsync behavior question
0.0 0



Question about the texture cache please.

As a level initializes, the engine preloads resources using the following:
cc.TextureCache.getInstance().addImageAsync(tx_player, this, this.textureLoadedCallback);

I expected the texture to be available as the callback method was invoked, but its “texture” property is empty until a sprite is created:

textureLoadedCallback:function () {

var tc = cc.TextureCache.getInstance();
console.log(tc.textures); // outputs Object {}

// Debug
var s1 = cc.Sprite.create(tx_player);
console.log(tc.textures); // Object {img/player_idle0.png: img}

Is my understanding wrong? I expect the callback method to be called once the texture is made available in the texture cache.



look at this source code

            texture.addEventListener("load", function () {
                that._addImageAsyncCallBack(target, selector);
            texture.src = path;
            this.textures[path.toString()] = texture;

it invokes your callback routine first, then set the hashmap with texture.
so the texture structure is created after you add it.

sorry above. your code should print out the textures


Finally found what my mistake was:
I used the following:
cc.TextureCache.getInstance().addImageAsync(tx_player, this, this.textureLoadedCallback(tx_player));

-> this exectues the textureLoadedCallback since I make a function call isntead of a function definition… Wrapping it with function(){} solved my issues.

My bad… Thanks for your time!