Skip to content

Loading spriteFrames from jsonObject has been supported. #2726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 49 additions & 31 deletions cocos2d/core/sprites/CCSpriteFrameCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
this._frameConfigCache[url] = dict;
return dict;
}
this._frameConfigCache[url] = this._parseFrameConfig(dict);
return this._frameConfigCache[url];
},

_getFrameConfigByJsonObject: function(url, jsonObject) {
cc.assert(jsonObject, cc._LogInfos.spriteFrameCache__getFrameConfig_2, url);
this._frameConfigCache[url] = this._parseFrameConfig(jsonObject);
return this._frameConfigCache[url];
},

_parseFrameConfig: function(dict) {
var tempFrames = dict["frames"], tempMeta = dict["metadata"] || dict["meta"];
var frames = {}, meta = {};
var format = 0;
Expand Down Expand Up @@ -125,38 +136,21 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
}
frames[key] = tempFrame;
}
var cfg = this._frameConfigCache[url] = {
_inited : true,
frames : frames,
meta : meta
};
return cfg;
return {_inited: true, frames: frames, meta: meta};
},

/**
* <p>
* Adds multiple Sprite Frames from a plist or json file.<br/>
* A texture will be loaded automatically. The texture name will composed by replacing the .plist or .json suffix with .png<br/>
* If you want to use another texture, you should use the addSpriteFrames:texture method.<br/>
* </p>
* @param {String} url file path
* @param {HTMLImageElement|cc.Texture2D|string} texture
* @example
* // add SpriteFrames to SpriteFrameCache With File
* cc.spriteFrameCache.addSpriteFrames(s_grossiniPlist);
* cc.spriteFrameCache.addSpriteFrames(s_grossiniJson);
*/
addSpriteFrames: function (url, texture) {
// Adds multiple Sprite Frames from a json object. it uses for local web view app.
_addSpriteFramesByObject: function(url, jsonObject, texture) {
cc.assert(url, cc._LogInfos.spriteFrameCache_addSpriteFrames_2);

//Is it a SpriteFrame plist?
var dict = this._frameConfigCache[url] || cc.loader.getRes(url);
if(!dict || !dict["frames"])
if(!jsonObject || !jsonObject["frames"])
return;

var self = this;
var frameConfig = self._frameConfigCache[url] || self._getFrameConfig(url);
//self._checkConflict(frameConfig); //TODO
var frameConfig = this._frameConfigCache[url] || this._getFrameConfigByJsonObject(url, jsonObject);
//this._checkConflict(frameConfig); //TODO
this._createSpriteFrames(frameConfig, texture);
},

_createSpriteFrames: function(frameConfig, texture) {
var frames = frameConfig.frames, meta = frameConfig.meta;
if(!texture){
var texturePath = cc.path.changeBasename(url, meta.image || ".png");
Expand All @@ -170,7 +164,7 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
}

//create sprite frames
var spAliases = self._spriteFramesAliases, spriteFrames = self._spriteFrames;
var spAliases = this._spriteFramesAliases, spriteFrames = this._spriteFrames;
for (var key in frames) {
var frame = frames[key];
var spriteFrame = spriteFrames[key];
Expand All @@ -180,9 +174,8 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
if(aliases){//set aliases
for(var i = 0, li = aliases.length; i < li; i++){
var alias = aliases[i];
if (spAliases[alias]) {
if (spAliases[alias])
cc.log(cc._LogInfos.spriteFrameCache_addSpriteFrames, alias);
}
spAliases[alias] = key;
}
}
Expand All @@ -202,12 +195,37 @@ cc.spriteFrameCache = /** @lends cc.spriteFrameCache# */{
spriteFrame.setRect(cc.rect(0, 0, rect.width, rect.height));
}
}

spriteFrames[key] = spriteFrame;
}
}
},

/**
* <p>
* Adds multiple Sprite Frames from a plist or json file.<br/>
* A texture will be loaded automatically. The texture name will composed by replacing the .plist or .json suffix with .png<br/>
* If you want to use another texture, you should use the addSpriteFrames:texture method.<br/>
* </p>
* @param {String} url file path
* @param {HTMLImageElement|cc.Texture2D|string} texture
* @example
* // add SpriteFrames to SpriteFrameCache With File
* cc.spriteFrameCache.addSpriteFrames(s_grossiniPlist);
* cc.spriteFrameCache.addSpriteFrames(s_grossiniJson);
*/
addSpriteFrames: function (url, texture) {
cc.assert(url, cc._LogInfos.spriteFrameCache_addSpriteFrames_2);

//Is it a SpriteFrame plist?
var dict = this._frameConfigCache[url] || cc.loader.getRes(url);
if(!dict || !dict["frames"])
return;

var frameConfig = this._frameConfigCache[url] || this._getFrameConfig(url);
//this._checkConflict(frameConfig); //TODO
this._createSpriteFrames(frameConfig, texture);
},

// Function to check if frames to add exists already, if so there may be name conflit that must be solved
_checkConflict: function (dictionary) {
var framesDict = dictionary["frames"];
Expand Down