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 reverse:function(){ 56 return this.clone(); 57 }, 58 59 clone:function(){ 60 return new cc.ActionInstant(); 61 } 62 }); 63 64 /** Show the node 65 * @class 66 * @extends cc.ActionInstant 67 */ 68 cc.Show = cc.ActionInstant.extend(/** @lends cc.Show# */{ 69 /** 70 * @param {Number} time 71 */ 72 update:function (time) { 73 this.target.visible = true; 74 }, 75 76 /** 77 * @return {cc.FiniteTimeAction} 78 */ 79 reverse:function () { 80 return cc.Hide.create(); 81 }, 82 83 clone:function(){ 84 return new cc.Show(); 85 } 86 }); 87 /** 88 * @return {cc.Show} 89 * @example 90 * // example 91 * var showAction = cc.Show.create(); 92 */ 93 cc.Show.create = function () { 94 return new cc.Show(); 95 }; 96 97 /** 98 * Hide the node 99 * @class 100 * @extends cc.ActionInstant 101 */ 102 cc.Hide = cc.ActionInstant.extend(/** @lends cc.Hide# */{ 103 /** 104 * @param {Number} time 105 */ 106 update:function (time) { 107 this.target.visible = false; 108 }, 109 110 /** 111 * @return {cc.FiniteTimeAction} 112 */ 113 reverse:function () { 114 return cc.Show.create(); 115 }, 116 117 clone:function(){ 118 return new cc.Hide(); 119 } 120 }); 121 /** 122 * @return {cc.Hide} 123 * @example 124 * // example 125 * var hideAction = cc.Hide.create(); 126 */ 127 cc.Hide.create = function () { 128 return (new cc.Hide()); 129 }; 130 131 132 /** Toggles the visibility of a node 133 * @class 134 * @extends cc.ActionInstant 135 */ 136 cc.ToggleVisibility = cc.ActionInstant.extend(/** @lends cc.ToggleVisibility# */{ 137 /** 138 * @param {Number} time 139 */ 140 update:function (time) { 141 this.target.visible = !this.target.visible; 142 }, 143 144 /** 145 * @return {cc.ToggleVisibility} 146 */ 147 reverse:function () { 148 return new cc.ToggleVisibility(); 149 }, 150 151 clone:function(){ 152 return new cc.ToggleVisibility(); 153 } 154 }); 155 156 /** 157 * @return {cc.ToggleVisibility} 158 * @example 159 * // example 160 * var toggleVisibilityAction = cc.ToggleVisibility.create(); 161 */ 162 cc.ToggleVisibility.create = function () { 163 return (new cc.ToggleVisibility()); 164 }; 165 166 cc.RemoveSelf = cc.ActionInstant.extend({ 167 _isNeedCleanUp:true, 168 ctor:function(){ 169 cc.FiniteTimeAction.prototype.ctor.call(this); 170 this._isNeedCleanUp = true; 171 }, 172 173 update:function(time){ 174 this.target.removeFromParent(this._isNeedCleanUp); 175 }, 176 177 init:function(isNeedCleanUp){ 178 this._isNeedCleanUp = isNeedCleanUp; 179 return true; 180 }, 181 182 reverse:function(){ 183 return new cc.RemoveSelf(this._isNeedCleanUp); 184 }, 185 186 clone:function(){ 187 return new cc.RemoveSelf(this._isNeedCleanUp); 188 } 189 }); 190 191 cc.RemoveSelf.create = function(isNeedCleanUp){ 192 if(isNeedCleanUp == null) 193 isNeedCleanUp = true; 194 var removeSelf = new cc.RemoveSelf(); 195 if(removeSelf) 196 removeSelf.init(isNeedCleanUp); 197 return removeSelf; 198 }; 199 200 /** 201 * Flips the sprite horizontally 202 * @class 203 * @extends cc.ActionInstant 204 */ 205 cc.FlipX = cc.ActionInstant.extend(/** @lends cc.FlipX# */{ 206 _flippedX:false, 207 ctor:function(){ 208 cc.FiniteTimeAction.prototype.ctor.call(this); 209 this._flippedX = false; 210 }, 211 /** 212 * @param {Boolean} x 213 * @return {Boolean} 214 */ 215 initWithFlipX:function (x) { 216 this._flippedX = x; 217 return true; 218 }, 219 220 /** 221 * @param {Number} time 222 */ 223 update:function (time) { 224 this.target.flippedX = this._flippedX; 225 }, 226 227 /** 228 * @return {cc.FiniteTimeAction} 229 */ 230 reverse:function () { 231 return cc.FlipX.create(!this._flippedX); 232 }, 233 234 clone:function(){ 235 var action = new cc.FlipX(); 236 action.initWithFlipX(this._flippedX); 237 return action; 238 } 239 }); 240 241 /** 242 * @param {Boolean} x 243 * @return {cc.FlipX} 244 * var flipXAction = cc.FlipX.create(true); 245 */ 246 cc.FlipX.create = function (x) { 247 var ret = new cc.FlipX(); 248 if (ret.initWithFlipX(x)) 249 return ret; 250 return null; 251 }; 252 253 /** 254 * Flips the sprite vertically 255 * @class 256 * @extends cc.ActionInstant 257 */ 258 cc.FlipY = cc.ActionInstant.extend(/** @lends cc.FlipY# */{ 259 _flippedY:false, 260 ctor:function(){ 261 cc.FiniteTimeAction.prototype.ctor.call(this); 262 this._flippedY = false; 263 }, 264 /** 265 * @param {Boolean} Y 266 * @return {Boolean} 267 */ 268 initWithFlipY:function (Y) { 269 this._flippedY = Y; 270 return true; 271 }, 272 273 /** 274 * @param {Number} time 275 */ 276 update:function (time) { 277 //this._super(); 278 this.target.flippedY = this._flippedY; 279 }, 280 281 /** 282 * @return {cc.FiniteTimeAction} 283 */ 284 reverse:function () { 285 return cc.FlipY.create(!this._flippedY); 286 }, 287 288 clone:function(){ 289 var action = new cc.FlipY(); 290 action.initWithFlipY(this._flippedY); 291 return action; 292 } 293 }); 294 /** 295 * @param {Boolean} y 296 * @return {cc.FlipY} 297 * @example 298 * // example 299 * var flipYAction = cc.FlipY.create(); 300 */ 301 cc.FlipY.create = function (y) { 302 var ret = new cc.FlipY(); 303 if (ret.initWithFlipY(y)) 304 return ret; 305 return null; 306 }; 307 308 309 /** Places the node in a certain position 310 * @class 311 * @extends cc.ActionInstant 312 */ 313 cc.Place = cc.ActionInstant.extend(/** @lends cc.Place# */{ 314 _x: 0, 315 _y: 0, 316 ctor:function(){ 317 cc.FiniteTimeAction.prototype.ctor.call(this); 318 this._x = 0; 319 this._y = 0; 320 }, 321 322 /** Initializes a Place action with a position 323 * @param {cc.Point} pos 324 * @return {Boolean} 325 */ 326 initWithPosition: function (x, y) { 327 this._x = x; 328 this._y = y; 329 return true; 330 }, 331 332 /** 333 * @param {Number} time 334 */ 335 update:function (time) { 336 this.target.setPosition(this._x, this._y); 337 }, 338 339 clone:function(){ 340 var action = new cc.Place(); 341 action.initWithPosition(this._x, this._y); 342 return action; 343 } 344 }); 345 /** creates a Place action with a position 346 * @param {cc.Point} pos 347 * @return {cc.Place} 348 * @example 349 * // example 350 * var placeAction = cc.Place.create(cc.p(200, 200)); 351 */ 352 cc.Place.create = function (pos) { 353 var ret = new cc.Place(); 354 ret.initWithPosition(pos.x, pos.y); 355 return ret; 356 }; 357 358 359 /** Calls a 'callback' 360 * @class 361 * @extends cc.ActionInstant 362 */ 363 cc.CallFunc = cc.ActionInstant.extend(/** @lends cc.CallFunc# */{ 364 _selectorTarget:null, 365 _callFunc:null, 366 _function:null, 367 _data:null, 368 369 ctor:function(){ 370 cc.FiniteTimeAction.prototype.ctor.call(this); 371 this._selectorTarget = null; 372 this._callFunc = null; 373 this._function = null; 374 this._data = null; 375 }, 376 377 /** 378 * @param {function|Null} selector 379 * @param {object} selectorTarget 380 * @param {*|Null} data data for function, it accepts all data types. 381 * @return {Boolean} 382 */ 383 initWithTarget:function (selector, selectorTarget, data) { 384 this._data = data; 385 this._callFunc = selector; 386 this._selectorTarget = selectorTarget; 387 return true; 388 }, 389 390 /** 391 * initializes the action with the std::function<void()> 392 * @param {function} func 393 * @returns {boolean} 394 */ 395 initWithFunction:function(func){ 396 this._function = func; 397 return true; 398 }, 399 400 /** 401 * execute the function. 402 */ 403 execute:function () { 404 if (this._callFunc != null) //CallFunc, N, ND 405 this._callFunc.call(this._selectorTarget, this.target, this._data); 406 else if(this._function) 407 this._function.call(null, this.target); 408 }, 409 410 /** 411 * @param {Number} time 412 */ 413 update:function (time) { 414 //this._super(target); 415 this.execute(); 416 }, 417 418 /** 419 * @return {object} 420 */ 421 getTargetCallback:function () { 422 return this._selectorTarget; 423 }, 424 425 /** 426 * @param {object} sel 427 */ 428 setTargetCallback:function (sel) { 429 if (sel != this._selectorTarget) { 430 if (this._selectorTarget) 431 this._selectorTarget = null; 432 this._selectorTarget = sel; 433 } 434 }, 435 436 copy:function() { 437 var n = new cc.CallFunc(); 438 if(this._selectorTarget){ 439 n.initWithTarget(this._callFunc, this._selectorTarget, this._data) 440 }else if(this._function){ 441 n.initWithFunction(this._function); 442 } 443 return n; 444 }, 445 446 clone:function(){ 447 var action = new cc.CallFunc(); 448 if(this._selectorTarget){ 449 action.initWithTarget(this._callFunc, this._selectorTarget, this._data) 450 }else if(this._function){ 451 action.initWithFunction(this._function); 452 } 453 return action; 454 } 455 }); 456 /** creates the action with the callback 457 * @param {function} selector 458 * @param {object|null} [selectorTarget=] 459 * @param {*|Null} [data=] data for function, it accepts all data types. 460 * @return {cc.CallFunc} 461 * @example 462 * // example 463 * // CallFunc without data 464 * var finish = cc.CallFunc.create(this.removeSprite, this); 465 * 466 * // CallFunc with data 467 * var finish = cc.CallFunc.create(this.removeFromParentAndCleanup, this._grossini, true), 468 */ 469 cc.CallFunc.create = function (selector, selectorTarget, data) { 470 var ret = new cc.CallFunc(); 471 if(selectorTarget === undefined){ 472 if (ret && ret.initWithFunction(selector)) { 473 return ret; 474 } 475 }else{ 476 if (ret && ret.initWithTarget(selector, selectorTarget, data)) { 477 ret._callFunc = selector; 478 return ret; 479 } 480 } 481 return null; 482 }; 483