Skip to content

Commit 8c2f8e2

Browse files
committed
Merge pull request #2726 from dingpinglv/Iss2723_defineGetterSetter
Loading spriteFrames from jsonObject has been supported.
2 parents 58bcde5 + 625ffe2 commit 8c2f8e2

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

cocos2d/core/sprites/CCSpriteFrameCache.js

+49-31
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
7171
this._frameConfigCache[url] = dict;
7272
return dict;
7373
}
74+
this._frameConfigCache[url] = this._parseFrameConfig(dict);
75+
return this._frameConfigCache[url];
76+
},
77+
78+
_getFrameConfigByJsonObject: function(url, jsonObject) {
79+
cc.assert(jsonObject, cc._LogInfos.spriteFrameCache__getFrameConfig_2, url);
80+
this._frameConfigCache[url] = this._parseFrameConfig(jsonObject);
81+
return this._frameConfigCache[url];
82+
},
83+
84+
_parseFrameConfig: function(dict) {
7485
var tempFrames = dict["frames"], tempMeta = dict["metadata"] || dict["meta"];
7586
var frames = {}, meta = {};
7687
var format = 0;
@@ -125,38 +136,21 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
125136
}
126137
frames[key] = tempFrame;
127138
}
128-
var cfg = this._frameConfigCache[url] = {
129-
_inited : true,
130-
frames : frames,
131-
meta : meta
132-
};
133-
return cfg;
139+
return {_inited: true, frames: frames, meta: meta};
134140
},
135141

136-
/**
137-
* <p>
138-
* Adds multiple Sprite Frames from a plist or json file.<br/>
139-
* A texture will be loaded automatically. The texture name will composed by replacing the .plist or .json suffix with .png<br/>
140-
* If you want to use another texture, you should use the addSpriteFrames:texture method.<br/>
141-
* </p>
142-
* @param {String} url file path
143-
* @param {HTMLImageElement|cc.Texture2D|string} texture
144-
* @example
145-
* // add SpriteFrames to SpriteFrameCache With File
146-
* cc.spriteFrameCache.addSpriteFrames(s_grossiniPlist);
147-
* cc.spriteFrameCache.addSpriteFrames(s_grossiniJson);
148-
*/
149-
addSpriteFrames: function (url, texture) {
142+
// Adds multiple Sprite Frames from a json object. it uses for local web view app.
143+
_addSpriteFramesByObject: function(url, jsonObject, texture) {
150144
cc.assert(url, cc._LogInfos.spriteFrameCache_addSpriteFrames_2);
151-
152-
//Is it a SpriteFrame plist?
153-
var dict = this._frameConfigCache[url] || cc.loader.getRes(url);
154-
if(!dict || !dict["frames"])
145+
if(!jsonObject || !jsonObject["frames"])
155146
return;
156147

157-
var self = this;
158-
var frameConfig = self._frameConfigCache[url] || self._getFrameConfig(url);
159-
//self._checkConflict(frameConfig); //TODO
148+
var frameConfig = this._frameConfigCache[url] || this._getFrameConfigByJsonObject(url, jsonObject);
149+
//this._checkConflict(frameConfig); //TODO
150+
this._createSpriteFrames(frameConfig, texture);
151+
},
152+
153+
_createSpriteFrames: function(frameConfig, texture) {
160154
var frames = frameConfig.frames, meta = frameConfig.meta;
161155
if(!texture){
162156
var texturePath = cc.path.changeBasename(url, meta.image || ".png");
@@ -170,7 +164,7 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
170164
}
171165

172166
//create sprite frames
173-
var spAliases = self._spriteFramesAliases, spriteFrames = self._spriteFrames;
167+
var spAliases = this._spriteFramesAliases, spriteFrames = this._spriteFrames;
174168
for (var key in frames) {
175169
var frame = frames[key];
176170
var spriteFrame = spriteFrames[key];
@@ -180,9 +174,8 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
180174
if(aliases){//set aliases
181175
for(var i = 0, li = aliases.length; i < li; i++){
182176
var alias = aliases[i];
183-
if (spAliases[alias]) {
177+
if (spAliases[alias])
184178
cc.log(cc._LogInfos.spriteFrameCache_addSpriteFrames, alias);
185-
}
186179
spAliases[alias] = key;
187180
}
188181
}
@@ -202,12 +195,37 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
202195
spriteFrame.setRect(cc.rect(0, 0, rect.width, rect.height));
203196
}
204197
}
205-
206198
spriteFrames[key] = spriteFrame;
207199
}
208200
}
209201
},
210202

203+
/**
204+
* <p>
205+
* Adds multiple Sprite Frames from a plist or json file.<br/>
206+
* A texture will be loaded automatically. The texture name will composed by replacing the .plist or .json suffix with .png<br/>
207+
* If you want to use another texture, you should use the addSpriteFrames:texture method.<br/>
208+
* </p>
209+
* @param {String} url file path
210+
* @param {HTMLImageElement|cc.Texture2D|string} texture
211+
* @example
212+
* // add SpriteFrames to SpriteFrameCache With File
213+
* cc.spriteFrameCache.addSpriteFrames(s_grossiniPlist);
214+
* cc.spriteFrameCache.addSpriteFrames(s_grossiniJson);
215+
*/
216+
addSpriteFrames: function (url, texture) {
217+
cc.assert(url, cc._LogInfos.spriteFrameCache_addSpriteFrames_2);
218+
219+
//Is it a SpriteFrame plist?
220+
var dict = this._frameConfigCache[url] || cc.loader.getRes(url);
221+
if(!dict || !dict["frames"])
222+
return;
223+
224+
var frameConfig = this._frameConfigCache[url] || this._getFrameConfig(url);
225+
//this._checkConflict(frameConfig); //TODO
226+
this._createSpriteFrames(frameConfig, texture);
227+
},
228+
211229
// Function to check if frames to add exists already, if so there may be name conflit that must be solved
212230
_checkConflict: function (dictionary) {
213231
var framesDict = dictionary["frames"];

0 commit comments

Comments
 (0)