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 * initializes the action with the property name (key), and the from and to parameters. 60 * @param {Number} duration 61 * @param {String} key 62 * @param {Number} from 63 * @param {Number} to 64 * @return {Boolean} 65 */ 66 initWithDuration:function (duration, key, from, to) { 67 if (this._super(duration)) { 68 this.key = key; 69 this.to = to; 70 this.from = from; 71 return true; 72 } 73 return false; 74 }, 75 /** 76 * @param {cc.Node} target 77 */ 78 startWithTarget:function (target) { 79 cc.Assert(target, "target must implement cc.ActionTweenDelegate"); 80 this._super(target); 81 this.delta = this.to - this.from; 82 }, 83 /** 84 * @param {Number} dt 85 */ 86 update:function (dt) { 87 this._target.updateTweenAction(this.to - this.delta * (1 - dt), this.key); 88 }, 89 /** 90 * @return {cc.ActionTween} 91 */ 92 reverse:function () { 93 return cc.ActionTween.create(this.duration, this.key, this.to, this.from); 94 } 95 }); 96 97 /** 98 * Creates an initializes the action with the property name (key), and the from and to parameters. 99 * @param {Number} duration 100 * @param {String} key 101 * @param {Number} from 102 * @param {Number} to 103 * @return {cc.ActionTween} 104 */ 105 cc.ActionTween.create = function (duration, key, from, to) { 106 var ret = new cc.ActionTween(); 107 if (ret.initWithDuration(duration, key, from, to)) { 108 return ret; 109 } 110 return null; 111 }; 112