cocos2d / cocos2d-js

cocos2d-x for JS
http://www.cocos2d-x.org
MIT License
1.86k stars 490 forks source link

Resources being removed from cache #1328

Open adamlwgriffiths opened 9 years ago

adamlwgriffiths commented 9 years ago

Loading an animation using cc.spriteFrameCache causes the resource to be removed from the loader cache. This makes work arounds for dynamically loading frame names from plists (an unbelievable oversight in the API) a hit-and-miss affair as resources disappear.

 64     _getFrameConfig : function(url){
 65         var dict = cc.loader.getRes(url);
 66 
 67         cc.assert(dict, cc._LogInfos.spriteFrameCache__getFrameConfig_2, url);
 68 
 69         cc.loader.release(url);//release it in loader

http://www.cocos2d-x.org/reference/html5-js/V3.2/symbols/src/_Users_panda_StudyWork_Cocos_cocos2d-js_frameworks_cocos2d-html5_cocos2d_core_sprites_CCSpriteFrameCache.js.html

dingpinglv commented 9 years ago

Hi @adamlwgriffiths ,

I'm sorry, I didn't understand what your problem is.

Yes, _getFrameConfig released the resource from cc.loader(for reduce memory consumption), it also save the result of plist into _frameConfigCache.

        var cfg = this._frameConfigCache[url] = {
            _inited : true,
            frames : frames,
            meta : meta
        };

If you call addSpriteFrames again, it will return the result from _frameConfigCache directly. var frameConfig = self._frameConfigCache[url] || self._getFrameConfig(url);

Best regards David

adamlwgriffiths commented 9 years ago

Because the API doesn't let me get frame data from the animation, I need that plist file so I can read the frame names and construct an animation.

I'm not going to hard code in for x in y: add 'hard_coded_frame_' + x to animation for every animation I create (as your examples do), thats insane.

But the plist has been removed from the cache, and trying to get it results in errors.

I've explicitly asked for it to be cached by adding it to the resources.js file, I doesn't make sense to remove it unless I explicitly ask for it to be.

adamlwgriffiths commented 9 years ago

The private _frameConfigCache variable was my previous "work around" for this deficiency. This obviously doesn't exist in Cocos2d-X and is therefore non-portable and not feasible.