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