1 /**************************************************************************** 2 Copyright (c) 2011-2012 cocos2d-x.org 3 Copyright (c) 2013-2014 Chukong Technologies Inc. 4 5 http://www.cocos2d-x.org 6 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 ****************************************************************************/ 25 26 /** 27 * Base singleton object for ccs.ActionManager. 28 * @class 29 * @name ccs.actionManager 30 */ 31 ccs.actionManager = /** @lends ccs.actionManager# */{ 32 _actionDic: {}, 33 34 /** 35 * Init properties with json dictionary 36 * @param {String} jsonName 37 * @param {Object} dic 38 * @param {Object} root 39 */ 40 initWithDictionary: function (jsonName, dic, root) { 41 var path = jsonName; 42 var pos = path.lastIndexOf("/"); 43 var fileName = path.substr(pos + 1, path.length); 44 var actionList = dic["actionlist"]; 45 var locActionList = []; 46 for (var i = 0; i < actionList.length; i++) { 47 var locAction = new ccs.ActionObject(); 48 var locActionDic = actionList[i]; 49 locAction.initWithDictionary(locActionDic, root); 50 locActionList.push(locAction); 51 } 52 this._actionDic[fileName] = locActionList; 53 }, 54 55 /** 56 * Gets an actionObject with a name. 57 * @param {String} jsonName 58 * @param {String} actionName 59 * @returns {ccs.ActionObject} 60 */ 61 getActionByName: function (jsonName, actionName) { 62 var path = jsonName; 63 var pos = path.lastIndexOf("/"); 64 var fileName = path.substr(pos + 1, path.length); 65 var actionList = this._actionDic[fileName]; 66 if (!actionList) 67 return null; 68 for (var i = 0; i < actionList.length; i++) { 69 var locAction = actionList[i]; 70 if (actionName === locAction.getName()) 71 return locAction; 72 } 73 return null; 74 }, 75 76 /** 77 * Play an Action with a name. 78 * @param {String} jsonName 79 * @param {String} actionName 80 * @param {cc.CallFunc} fun 81 */ 82 playActionByName: function (jsonName, actionName, fun) { 83 var action = this.getActionByName(jsonName, actionName); 84 if (action) 85 action.play(fun); 86 }, 87 88 /** 89 * Stop an Action with a name. 90 * @param {String} jsonName 91 * @param {String} actionName 92 */ 93 stopActionByName: function (jsonName, actionName) { 94 var action = this.getActionByName(jsonName, actionName); 95 if (action) 96 action.stop(); 97 }, 98 99 /** 100 * Release all actions. 101 */ 102 releaseActions: function () { 103 this._actionDic = {}; 104 }, 105 106 /** 107 * Clear data: Release all actions. 108 */ 109 clear: function() { 110 this._actionDic = {}; 111 } 112 }; 113