1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies 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  * @function
 89  * @return {cc.Show}
 90  * @example
 91  * // example
 92  * var showAction = cc.show();
 93  */
 94 cc.show = function () {
 95     return new cc.Show();
 96 };
 97 /**
 98  * Please use cc.show instead
 99  * @static
100  * @deprecated
101  * @return {cc.Show}
102  */
103 cc.Show.create = cc.show;
104 
105 /**
106  * Hide the node
107  * @class
108  * @extends cc.ActionInstant
109  */
110 cc.Hide = cc.ActionInstant.extend(/** @lends cc.Hide# */{
111     /**
112      * @param {Number} time
113      */
114     update:function (time) {
115         this.target.visible = false;
116     },
117 
118     /**
119      * @return {cc.FiniteTimeAction}
120      */
121     reverse:function () {
122         return cc.Show.create();
123     },
124 
125     clone:function(){
126         return new cc.Hide();
127     }
128 });
129 /**
130  * @function
131  * @return {cc.Hide}
132  * @example
133  * // example
134  * var hideAction = cc.hide();
135  */
136 cc.hide = function () {
137     return new cc.Hide();
138 };
139 /**
140  * @static
141  * @deprecated
142  * @return {cc.Hide}
143  */
144 cc.Hide.create = cc.hide;
145 
146 
147 /** Toggles the visibility of a node
148  * @class
149  * @extends cc.ActionInstant
150  */
151 cc.ToggleVisibility = cc.ActionInstant.extend(/** @lends cc.ToggleVisibility# */{
152     /**
153      * @param {Number} time
154      */
155     update:function (time) {
156         this.target.visible = !this.target.visible;
157     },
158 
159     /**
160      * @return {cc.ToggleVisibility}
161      */
162     reverse:function () {
163         return new cc.ToggleVisibility();
164     },
165 
166     clone:function(){
167         return new cc.ToggleVisibility();
168     }
169 });
170 
171 /**
172  * @function
173  * @return {cc.ToggleVisibility}
174  * @example
175  * // example
176  * var toggleVisibilityAction = cc.toggleVisibility();
177  */
178 cc.toggleVisibility = function () {
179     return new cc.ToggleVisibility();
180 };
181 /**
182  * Please use cc.toggleVisibility instead
183  * @static
184  * @deprecated
185  * @return {cc.ToggleVisibility}
186  */
187 cc.ToggleVisibility.create = cc.toggleVisibility;
188 
189 cc.RemoveSelf = cc.ActionInstant.extend({
190      _isNeedCleanUp: true,
191 
192 	/**
193 	 * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
194 	 *
195 	 * Constructor of cc.RemoveSelf
196 	 * @param {Boolean} [isNeedCleanUp=true]
197 	 *
198 	 * @example
199 	 * // example
200 	 * var removeSelfAction = new cc.RemoveSelf(false);
201 	 */
202     ctor:function(isNeedCleanUp){
203         cc.FiniteTimeAction.prototype.ctor.call(this);
204 
205 	    isNeedCleanUp !== undefined && this.init(isNeedCleanUp);
206     },
207 
208     update:function(time){
209         this.target.removeFromParent(this._isNeedCleanUp);
210     },
211 
212     init:function(isNeedCleanUp){
213         this._isNeedCleanUp = isNeedCleanUp;
214         return true;
215     },
216 
217     reverse:function(){
218         return new cc.RemoveSelf(this._isNeedCleanUp);
219     },
220 
221     clone:function(){
222         return new cc.RemoveSelf(this._isNeedCleanUp);
223     }
224 });
225 
226 /**
227  * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
228  *
229  * @function
230  * @param {Boolean} [isNeedCleanUp=true]
231  * @return {cc.RemoveSelf}
232  *
233  * @example
234  * // example
235  * var removeSelfAction = cc.removeSelf();
236  */
237 cc.removeSelf = function(isNeedCleanUp){
238     return new cc.RemoveSelf(isNeedCleanUp);
239 };
240 /**
241  * Please use cc.removeSelf instead
242  * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
243  *
244  * @static
245  * @deprecated
246  * @param {Boolean} [isNeedCleanUp=true]
247  * @return {cc.RemoveSelf}
248  */
249 cc.RemoveSelf.create = cc.removeSelf;
250 
251 /**
252  * Flips the sprite horizontally
253  * @class
254  * @extends cc.ActionInstant
255  */
256 cc.FlipX = cc.ActionInstant.extend(/** @lends cc.FlipX# */{
257     _flippedX:false,
258 
259 	/**
260 	 * Create a FlipX action to flip or unflip the target
261 	 *
262 	 * Constructor of cc.FlipX
263 	 * @param {Boolean} flip Indicate whether the target should be flipped or not
264 	 *
265 	 * @example
266 	 * var flipXAction = new cc.FlipX(true);
267 	 */
268     ctor:function(flip){
269         cc.FiniteTimeAction.prototype.ctor.call(this);
270         this._flippedX = false;
271 		flip !== undefined && this.initWithFlipX(flip);
272     },
273 
274     /**
275      * @param {Boolean} flip
276      * @return {Boolean}
277      */
278     initWithFlipX:function (flip) {
279         this._flippedX = flip;
280         return true;
281     },
282 
283     /**
284      * @param {Number} time
285      */
286     update:function (time) {
287         this.target.flippedX = this._flippedX;
288     },
289 
290     /**
291      * @return {cc.FiniteTimeAction}
292      */
293     reverse:function () {
294         return cc.FlipX.create(!this._flippedX);
295     },
296 
297     clone:function(){
298         var action = new cc.FlipX();
299         action.initWithFlipX(this._flippedX);
300         return action;
301     }
302 });
303 
304 /**
305  * Create a FlipX action to flip or unflip the target
306  *
307  * @function
308  * @param {Boolean} flip Indicate whether the target should be flipped or not
309  * @return {cc.FlipX}
310  * @example
311  * var flipXAction = cc.flipX(true);
312  */
313 cc.flipX = function (flip) {
314     return new cc.FlipX(flip);
315 };
316 /**
317  * Plese use cc.flipX instead
318  * Create a FlipX action to flip or unflip the target
319  *
320  * @static
321  * @deprecated
322  * @param {Boolean} flip Indicate whether the target should be flipped or not
323  * @return {cc.FlipX}
324  */
325 cc.FlipX.create = cc.flipX;
326 
327 /**
328  * Flips the sprite vertically
329  * @class
330  * @extends cc.ActionInstant
331  */
332 cc.FlipY = cc.ActionInstant.extend(/** @lends cc.FlipY# */{
333     _flippedY:false,
334 
335 	/**
336 	 * Create a FlipY action to flip or unflip the target
337 	 *
338 	 * Constructor of cc.FlipY
339 	 * @param {Boolean} flip
340 	 * @example
341 	 * var flipYAction = new cc.FlipY(true);
342 	 */
343     ctor: function(flip){
344         cc.FiniteTimeAction.prototype.ctor.call(this);
345         this._flippedY = false;
346 
347 		flip !== undefined && this.initWithFlipY(flip);
348     },
349     /**
350      * @param {Boolean} flip
351      * @return {Boolean}
352      */
353     initWithFlipY:function (flip) {
354         this._flippedY = flip;
355         return true;
356     },
357 
358     /**
359      * @param {Number} time
360      */
361     update:function (time) {
362         //this._super();
363         this.target.flippedY = this._flippedY;
364     },
365 
366     /**
367      * @return {cc.FiniteTimeAction}
368      */
369     reverse:function () {
370         return cc.FlipY.create(!this._flippedY);
371     },
372 
373     clone:function(){
374         var action = new cc.FlipY();
375         action.initWithFlipY(this._flippedY);
376         return action;
377     }
378 });
379 /**
380  * Create a FlipY action to flip or unflip the target
381  *
382  * @function
383  * @param {Boolean} flip
384  * @return {cc.FlipY}
385  * @example
386  * var flipYAction = cc.flipY(true);
387  */
388 cc.flipY = function (flip) {
389     return new cc.FlipY(flip);
390 };
391 /**
392  * Please use cc.flipY instead
393  * Create a FlipY action to flip or unflip the target
394  *
395  * @static
396  * @deprecated
397  * @param {Boolean} flip
398  * @return {cc.FlipY}
399  */
400 cc.FlipY.create = cc.flipY;
401 
402 
403 /** Places the node in a certain position
404  * @class
405  * @extends cc.ActionInstant
406  */
407 cc.Place = cc.ActionInstant.extend(/** @lends cc.Place# */{
408     _x: 0,
409 	_y: 0,
410 
411 	/**
412 	 * Creates a Place action with a position
413 	 *
414 	 * Constructor of cc.Place
415 	 * @param {cc.Point|Number} pos
416 	 * @param {Number} [y]
417 	 * @example
418 	 * var placeAction = new cc.Place.create(cc.p(200, 200));
419 	 * var placeAction = new cc.Place.create(200, 200);
420 	 */
421     ctor:function(pos, y){
422         cc.FiniteTimeAction.prototype.ctor.call(this);
423         this._x = 0;
424 	    this._y = 0;
425 
426 		if (pos !== undefined) {
427 			if (pos.x !== undefined) {
428 				y = pos.y;
429 				pos = pos.x;
430 			}
431 			this.initWithPosition(pos, y);
432 		}
433     },
434 
435     /** Initializes a Place action with a position
436      * @param {cc.Point} pos
437      * @return {Boolean}
438      */
439     initWithPosition: function (x, y) {
440         this._x = x;
441         this._y = y;
442         return true;
443     },
444 
445     /**
446      * @param {Number} time
447      */
448     update:function (time) {
449         this.target.setPosition(this._x, this._y);
450     },
451 
452     clone:function(){
453         var action = new cc.Place();
454         action.initWithPosition(this._x, this._y);
455         return action;
456     }
457 });
458 /**
459  * Creates a Place action with a position
460  * @function
461  * @param {cc.Point|Number} pos
462  * @param {Number} [y]
463  * @return {cc.Place}
464  * @example
465  * // example
466  * var placeAction = cc.place(cc.p(200, 200));
467  * var placeAction = cc.place(200, 200);
468  */
469 cc.place = function (pos, y) {
470     return new cc.Place(pos, y);
471 };
472 /**
473  * Please use cc.place instead
474  * Creates a Place action with a position
475  * @static
476  * @deprecated
477  * @param {cc.Point|Number} pos
478  * @param {Number} [y]
479  * @return {cc.Place}
480  */
481 cc.Place.create = cc.place;
482 
483 
484 /** Calls a 'callback'
485  * @class
486  * @extends cc.ActionInstant
487  */
488 cc.CallFunc = cc.ActionInstant.extend(/** @lends cc.CallFunc# */{
489     _selectorTarget:null,
490     _callFunc:null,
491     _function:null,
492     _data:null,
493 
494 	/**
495 	 * Creates a CallFunc action with the callback
496 	 *
497 	 * Constructor of cc.CallFunc
498 	 * @param {function} selector
499 	 * @param {object|null} [selectorTarget]
500 	 * @param {*|null} [data] data for function, it accepts all data types.
501 	 * @example
502 	 * // example
503 	 * // CallFunc without data
504 	 * var finish = new cc.CallFunc(this.removeSprite, this);
505 	 *
506 	 * // CallFunc with data
507 	 * var finish = new cc.CallFunc(this.removeFromParentAndCleanup, this,  true);
508 	 */
509     ctor:function(selector, selectorTarget, data){
510         cc.FiniteTimeAction.prototype.ctor.call(this);
511 
512 		if(selector !== undefined){
513 			if(selectorTarget === undefined)
514 				this.initWithFunction(selector);
515 			else this.initWithFunction(selector, selectorTarget, data);
516 		}
517     },
518 
519     /**
520      * Initializes the action with a function or function and its target
521      * @param {function} selector
522      * @param {object|Null} selectorTarget
523      * @param {*|Null} [data] data for function, it accepts all data types.
524      * @return {Boolean}
525      */
526     initWithFunction:function (selector, selectorTarget, data) {
527 	    if (selectorTarget) {
528             this._data = data;
529             this._callFunc = selector;
530             this._selectorTarget = selectorTarget;
531 	    }
532 	    else if (selector)
533 		    this._function = selector;
534         return true;
535     },
536 
537     /**
538      * execute the function.
539      */
540     execute:function () {
541         if (this._callFunc != null)         //CallFunc, N, ND
542             this._callFunc.call(this._selectorTarget, this.target, this._data);
543         else if(this._function)
544             this._function.call(null, this.target);
545     },
546 
547     /**
548      * @param {Number} time
549      */
550     update:function (time) {
551         //this._super(target);
552         this.execute();
553     },
554 
555     /**
556      * @return {object}
557      */
558     getTargetCallback:function () {
559         return this._selectorTarget;
560     },
561 
562     /**
563      * @param {object} sel
564      */
565     setTargetCallback:function (sel) {
566         if (sel != this._selectorTarget) {
567             if (this._selectorTarget)
568                 this._selectorTarget = null;
569             this._selectorTarget = sel;
570         }
571     },
572 
573     clone:function(){
574        var action = new cc.CallFunc();
575         if(this._selectorTarget){
576              action.initWithFunction(this._callFunc,  this._selectorTarget, this._data)
577         }else if(this._function){
578              action.initWithFunction(this._function);
579         }
580         return action;
581     }
582 });
583 /**
584  * Creates the action with the callback
585  * @function
586  * @param {function} selector
587  * @param {object|null} [selectorTarget]
588  * @param {*|null} [data] data for function, it accepts all data types.
589  * @return {cc.CallFunc}
590  * @example
591  * // example
592  * // CallFunc without data
593  * var finish = cc.callFunc(this.removeSprite, this);
594  *
595  * // CallFunc with data
596  * var finish = cc.callFunc(this.removeFromParentAndCleanup, this._grossini,  true);
597  */
598 cc.callFunc = function (selector, selectorTarget, data) {
599     return new cc.CallFunc(selector, selectorTarget, data);
600 };
601 /**
602  * Please use cc.callFunc instead
603  * Creates the action with the callback
604  * @static
605  * @deprecated
606  * @param {function} selector
607  * @param {object|null} [selectorTarget]
608  * @param {*|null} [data] data for function, it accepts all data types.
609  * @return {cc.CallFunc}
610  */
611 cc.CallFunc.create = cc.callFunc;
612