1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * @class 29 * @extends cc.Class 30 */ 31 cc.ActionTweenDelegate = cc.Class.extend(/** @lends cc.ActionTweenDelegate */{ 32 updateTweenAction:function(value, key){ 33 34 } 35 }); 36 37 /** 38 * cc.ActionTween 39 * cc.ActionTween is an action that lets you update any property of an object. 40 * 41 * @class 42 * @extends cc.ActionInterval 43 * @example 44 * //For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then: 45 * var modifyWidth = cc.ActionTween.create(2,"width",200,300) 46 * target.runAction(modifyWidth); 47 * 48 * //Another example: cc.ScaleTo action could be rewriten using cc.PropertyAction: 49 * // scaleA and scaleB are equivalents 50 * var scaleA = cc.ScaleTo.create(2,3); 51 * var scaleB = cc.ActionTween.create(2,"scale",1,3); 52 */ 53 cc.ActionTween = cc.ActionInterval.extend(/** @lends cc.ActionTween */{ 54 key:"", 55 from:0, 56 to:0, 57 delta:0, 58 59 ctor:function(){ 60 cc.ActionInterval.prototype.ctor.call(this); 61 this.key = ""; 62 this.from = 0; 63 this.to = 0; 64 this.delta = 0; 65 }, 66 67 /** 68 * initializes the action with the property name (key), and the from and to parameters. 69 * @param {Number} duration 70 * @param {String} key 71 * @param {Number} from 72 * @param {Number} to 73 * @return {Boolean} 74 */ 75 initWithDuration:function (duration, key, from, to) { 76 if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) { 77 this.key = key; 78 this.to = to; 79 this.from = from; 80 return true; 81 } 82 return false; 83 }, 84 /** 85 * @param {cc.Node} target 86 */ 87 startWithTarget:function (target) { 88 if(!target || !target.updateTweenAction) 89 throw "cc.ActionTween.startWithTarget(): target must be non-null, and target must implement updateTweenAction function"; 90 cc.ActionInterval.prototype.startWithTarget.call(this, target); 91 this.delta = this.to - this.from; 92 }, 93 /** 94 * @param {Number} dt 95 */ 96 update:function (dt) { 97 this._target.updateTweenAction(this.to - this.delta * (1 - dt), this.key); 98 }, 99 /** 100 * @return {cc.ActionTween} 101 */ 102 reverse:function () { 103 return cc.ActionTween.create(this.duration, this.key, this.to, this.from); 104 }, 105 106 clone:function(){ 107 var action = new cc.ActionTween(); 108 action.initWithDuration(this._duration, this.key, this.from, this.to); 109 return action; 110 } 111 }); 112 113 /** 114 * Creates an initializes the action with the property name (key), and the from and to parameters. 115 * @param {Number} duration 116 * @param {String} key 117 * @param {Number} from 118 * @param {Number} to 119 * @return {cc.ActionTween} 120 */ 121 cc.ActionTween.create = function (duration, key, from, to) { 122 var ret = new cc.ActionTween(); 123 if (ret.initWithDuration(duration, key, from, to)) 124 return ret; 125 return null; 126 }; 127