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 * Instant actions are immediate actions. They don't have a duration like 29 * the CCIntervalAction actions. 30 * @class 31 * @extends cc.FiniteTimeAction 32 */ 33 cc.ActionInstant = cc.FiniteTimeAction.extend(/** @lends cc.ActionInstant# */{ 34 /** 35 * @return {Boolean} 36 */ 37 isDone:function () { 38 return true; 39 }, 40 41 /** 42 * @param {Number} dt 43 */ 44 step:function (dt) { 45 this.update(1); 46 }, 47 48 /** 49 * @param {Number} time 50 */ 51 update:function (time) { 52 //nothing 53 } 54 }); 55 56 /** Show the node 57 * @class 58 * @extends cc.ActionInstant 59 */ 60 cc.Show = cc.ActionInstant.extend(/** @lends cc.Show# */{ 61 /** 62 * @param {cc.Node} target 63 */ 64 update:function (target) { 65 //this._super(target); 66 this._target.setVisible(true); 67 }, 68 69 /** 70 * @return {cc.FiniteTimeAction} 71 */ 72 reverse:function () { 73 return cc.Hide.create.call(this); 74 } 75 }); 76 /** 77 * @return {cc.Show} 78 * @example 79 * // example 80 * var showAction = cc.Show.create(); 81 */ 82 cc.Show.create = function () { 83 return new cc.Show(); 84 }; 85 86 /** 87 * Hide the node 88 * @class 89 * @extends cc.ActionInstant 90 */ 91 cc.Hide = cc.ActionInstant.extend(/** @lends cc.Hide# */{ 92 /** 93 * @param {Number} time 94 */ 95 update:function (time) { 96 //this._super(target); 97 this._target.setVisible(false); 98 }, 99 100 /** 101 * @return {cc.FiniteTimeAction} 102 */ 103 reverse:function () { 104 return cc.Show.create.call(this); 105 } 106 }); 107 /** 108 * @return {cc.Hide} 109 * @example 110 * // example 111 * var hideAction = cc.Hide.create(); 112 */ 113 cc.Hide.create = function () { 114 return (new cc.Hide()); 115 }; 116 117 118 /** Toggles the visibility of a node 119 * @class 120 * @extends cc.ActionInstant 121 */ 122 cc.ToggleVisibility = cc.ActionInstant.extend(/** @lends cc.ToggleVisibility# */{ 123 /** 124 * @param {Number} time 125 */ 126 update:function (time) { 127 //this._super(); 128 this._target.setVisible(!this._target.isVisible()); 129 }, 130 131 /** 132 * @return {cc.ToggleVisibility} 133 */ 134 reverse:function () { 135 return new cc.ToggleVisibility(); 136 } 137 }); 138 139 /** 140 * @return {cc.ToggleVisibility} 141 * @example 142 * // example 143 * var toggleVisibilityAction = cc.ToggleVisibility.create(); 144 */ 145 cc.ToggleVisibility.create = function () { 146 return (new cc.ToggleVisibility()); 147 }; 148 149 /** 150 * Flips the sprite horizontally 151 * @class 152 * @extends cc.ActionInstant 153 */ 154 cc.FlipX = cc.ActionInstant.extend(/** @lends cc.FlipX# */{ 155 /** 156 * @param {Boolean} x 157 * @return {Boolean} 158 */ 159 initWithFlipX:function (x) { 160 this._flipX = x; 161 return true; 162 }, 163 164 /** 165 * @param {Number} time 166 */ 167 update:function (time) { 168 //this._super(); 169 this._target.setFlipX(this._flipX); 170 }, 171 172 /** 173 * @return {cc.FiniteTimeAction} 174 */ 175 reverse:function () { 176 return cc.FlipX.create(!this._flipX); 177 }, 178 _flipX:false 179 }); 180 181 /** 182 * @param {Boolean} x 183 * @return {cc.FlipX} 184 * var flipXAction = cc.FlipX.create(true); 185 */ 186 cc.FlipX.create = function (x) { 187 var ret = new cc.FlipX(); 188 if (ret.initWithFlipX(x)) 189 return ret; 190 return null; 191 }; 192 193 /** 194 * Flips the sprite vertically 195 * @class 196 * @extends cc.ActionInstant 197 */ 198 cc.FlipY = cc.ActionInstant.extend(/** @lends cc.FlipY# */{ 199 /** 200 * @param {Boolean} Y 201 * @return {Boolean} 202 */ 203 initWithFlipY:function (Y) { 204 this._flipY = Y; 205 return true; 206 }, 207 208 /** 209 * @param {Number} time 210 */ 211 update:function (time) { 212 //this._super(); 213 this._target.setFlipY(this._flipY); 214 }, 215 216 /** 217 * @return {cc.FiniteTimeAction} 218 */ 219 reverse:function () { 220 return cc.FlipY.create(!this._flipY); 221 }, 222 _flipY:false 223 }); 224 /** 225 * @param {Boolean} y 226 * @return {cc.FlipY} 227 * @example 228 * // example 229 * var flipYAction = cc.FlipY.create(); 230 */ 231 cc.FlipY.create = function (y) { 232 var ret = new cc.FlipY(); 233 if (ret.initWithFlipY(y)) 234 return ret; 235 return null; 236 }; 237 238 239 /** Places the node in a certain position 240 * @class 241 * @extends cc.ActionInstant 242 */ 243 cc.Place = cc.ActionInstant.extend(/** @lends cc.Place# */{ 244 /** Initializes a Place action with a position 245 * @param {cc.Point} pos 246 * @return {Boolean} 247 */ 248 initWithPosition:function (pos) { 249 this._position = pos; 250 return true; 251 }, 252 253 /** 254 * @param {Number} time 255 */ 256 update:function (time) { 257 //this._super(target); 258 this._target.setPosition(this._position); 259 } 260 }); 261 /** creates a Place action with a position 262 * @param {cc.Point} pos 263 * @return {cc.Place} 264 * @example 265 * // example 266 * var placeAction = cc.Place.create(cc.p(200, 200)); 267 */ 268 cc.Place.create = function (pos) { 269 var ret = new cc.Place(); 270 ret.initWithPosition(pos); 271 return ret; 272 }; 273 274 275 /** Calls a 'callback' 276 * @class 277 * @extends cc.ActionInstant 278 */ 279 cc.CallFunc = cc.ActionInstant.extend(/** @lends cc.CallFunc# */{ 280 /** 281 * @param {function|Null} selector 282 * @param {object} selectorTarget 283 * @param {*|Null} data data for function, it accepts all data types. 284 * @return {Boolean} 285 */ 286 initWithTarget:function (selector, selectorTarget, data) { 287 this._data = data || null; 288 this._callFunc = selector || null; 289 this._selectorTarget = selectorTarget || null; 290 return true; 291 }, 292 293 /** 294 * execute the function. 295 */ 296 execute:function () { 297 if (this._callFunc != null)//CallFunc, N, ND 298 { 299 this._callFunc.call(this._selectorTarget, this._target, this._data); 300 } 301 }, 302 303 /** 304 * @param {Number} time 305 */ 306 update:function (time) { 307 //this._super(target); 308 this.execute(); 309 }, 310 311 /** 312 * @return {object} 313 */ 314 getTargetCallback:function () { 315 return this._selectorTarget; 316 }, 317 318 /** 319 * @param {object} sel 320 */ 321 setTargetCallback:function (sel) { 322 if (sel != this._selectorTarget) { 323 if (this._selectorTarget) { 324 this._selectorTarget = null; 325 } 326 this._selectorTarget = sel; 327 } 328 }, 329 330 copy:function() { 331 var n = new cc.CallFunc(); 332 n.initWithTarget(this._callFunc, this._selectorTarget, this._data ); 333 return n; 334 }, 335 _selectorTarget:null, 336 _callFunc:null 337 }); 338 /** creates the action with the callback 339 * @param {function|Null} selector 340 * @param {object} selectorTarget 341 * @param {*|Null} data data for function, it accepts all data types. 342 * @return {cc.CallFunc} 343 * @example 344 * // example 345 * // CallFunc without data 346 * var finish = cc.CallFunc.create(this.removeSprite, this); 347 * 348 * // CallFunc with data 349 * var finish = cc.CallFunc.create(this.removeFromParentAndCleanup, this._grossini, true), 350 */ 351 352 cc.CallFunc.create = function (selector, selectorTarget, data) { 353 var ret = new cc.CallFunc(); 354 if (ret && ret.initWithTarget(selector, selectorTarget, data)) { 355 ret._callFunc = selector; 356 return ret; 357 } 358 return null; 359 }; 360