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 true if the action has finished.
 36      * @return {Boolean}
 37      */
 38     isDone:function () {
 39         return true;
 40     },
 41 
 42     /**
 43      * called every frame with it's delta time. <br />
 44      * DON'T override unless you know what you are doing.
 45      * @param {Number} dt
 46      */
 47     step:function (dt) {
 48         this.update(1);
 49     },
 50 
 51     /**
 52      * Called once per frame. Time is the number of seconds of a frame interval.
 53      *
 54      * @param {Number} dt
 55      */
 56     update:function (dt) {
 57         //nothing
 58     },
 59 
 60     /**
 61      * returns a reversed action. <br />
 62      * For example: <br />
 63      * - The action will be x coordinates of 0 move to 100. <br />
 64      * - The reversed action will be x of 100 move to 0.
 65      * - Will be rewritten
 66      * @returns {cc.Action}
 67      */
 68     reverse:function(){
 69         return this.clone();
 70     },
 71 
 72     /**
 73      * to copy object with deep copy.
 74      * returns a clone of action.
 75      *
 76      * @return {cc.FiniteTimeAction}
 77      */
 78     clone:function(){
 79         return new cc.ActionInstant();
 80     }
 81 });
 82 
 83 /**
 84  * Show the node.
 85  * @class
 86  * @extends cc.ActionInstant
 87  */
 88 cc.Show = cc.ActionInstant.extend(/** @lends cc.Show# */{
 89 
 90     /**
 91      * Called once per frame. Time is the number of seconds of a frame interval.
 92      *
 93      * @param {Number} dt
 94      */
 95     update:function (dt) {
 96         this.target.visible = true;
 97     },
 98 
 99     /**
100      * returns a reversed action. <br />
101      * For example: <br />
102      * - The action will be x coordinates of 0 move to 100. <br />
103      * - The reversed action will be x of 100 move to 0.
104      * - Will be rewritten
105      * @returns {cc.Hide}
106      */
107     reverse:function () {
108         return new cc.Hide();
109     },
110 
111     /**
112      * to copy object with deep copy.
113      * returns a clone of action.
114      *
115      * @return {cc.FiniteTimeAction}
116      */
117     clone:function(){
118         return new cc.Show();
119     }
120 });
121 
122 /**
123  * Show the Node.
124  * @function
125  * @return {cc.Show}
126  * @example
127  * // example
128  * var showAction = cc.show();
129  */
130 cc.show = function () {
131     return new cc.Show();
132 };
133 
134 /**
135  * Show the Node. Please use cc.show instead.
136  * @static
137  * @deprecated since v3.0 <br /> Please use cc.show instead.
138  * @return {cc.Show}
139  */
140 cc.Show.create = cc.show;
141 
142 /**
143  * Hide the node.
144  * @class
145  * @extends cc.ActionInstant
146  */
147 cc.Hide = cc.ActionInstant.extend(/** @lends cc.Hide# */{
148 
149     /**
150      * Called once per frame. Time is the number of seconds of a frame interval.
151      *
152      * @param {Number} dt
153      */
154     update:function (dt) {
155         this.target.visible = false;
156     },
157 
158     /**
159      * returns a reversed action. <br />
160      * For example: <br />
161      * - The action will be x coordinates of 0 move to 100. <br />
162      * - The reversed action will be x of 100 move to 0.
163      * - Will be rewritten
164      * @returns {cc.Show}
165      */
166     reverse:function () {
167         return new cc.Show();
168     },
169 
170     /**
171      * to copy object with deep copy.
172      * returns a clone of action.
173      *
174      * @return {cc.Hide}
175      */
176     clone:function(){
177         return new cc.Hide();
178     }
179 });
180 
181 /**
182  * Hide the node.
183  * @function
184  * @return {cc.Hide}
185  * @example
186  * // example
187  * var hideAction = cc.hide();
188  */
189 cc.hide = function () {
190     return new cc.Hide();
191 };
192 
193 /**
194  * Hide the node. Please use cc.hide instead.
195  * @static
196  * @deprecated since v3.0 <br /> Please use cc.hide instead.
197  * @return {cc.Hide}
198  * @example
199  * // example
200  * var hideAction = cc.hide();
201  */
202 cc.Hide.create = cc.hide;
203 
204 /**
205  * Toggles the visibility of a node.
206  * @class
207  * @extends cc.ActionInstant
208  */
209 cc.ToggleVisibility = cc.ActionInstant.extend(/** @lends cc.ToggleVisibility# */{
210 
211     /**
212      * Called once per frame. Time is the number of seconds of a frame interval.
213      *
214      * @param {Number} dt
215      */
216     update:function (dt) {
217         this.target.visible = !this.target.visible;
218     },
219 
220     /**
221      * returns a reversed action.
222      * @returns {cc.ToggleVisibility}
223      */
224     reverse:function () {
225         return new cc.ToggleVisibility();
226     },
227 
228     /**
229      * to copy object with deep copy.
230      * returns a clone of action.
231      *
232      * @return {cc.ToggleVisibility}
233      */
234     clone:function(){
235         return new cc.ToggleVisibility();
236     }
237 });
238 
239 /**
240  * Toggles the visibility of a node.
241  * @function
242  * @return {cc.ToggleVisibility}
243  * @example
244  * // example
245  * var toggleVisibilityAction = cc.toggleVisibility();
246  */
247 cc.toggleVisibility = function () {
248     return new cc.ToggleVisibility();
249 };
250 
251 /**
252  * Toggles the visibility of a node. Please use cc.toggleVisibility instead.
253  * @static
254  * @deprecated since v3.0 <br /> Please use cc.toggleVisibility instead.
255  * @return {cc.ToggleVisibility}
256  */
257 cc.ToggleVisibility.create = cc.toggleVisibility;
258 
259 /**
260  * Delete self in the next frame.
261  * @class
262  * @extends cc.ActionInstant
263  * @param {Boolean} [isNeedCleanUp=true]
264  *
265  * @example
266  * // example
267  * var removeSelfAction = new cc.RemoveSelf(false);
268  */
269 cc.RemoveSelf = cc.ActionInstant.extend({
270      _isNeedCleanUp: true,
271 
272 	/**
273      * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
274 	 * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
275 	 * @param {Boolean} [isNeedCleanUp=true]
276 	 */
277     ctor:function(isNeedCleanUp){
278         cc.FiniteTimeAction.prototype.ctor.call(this);
279 
280 	    isNeedCleanUp !== undefined && this.init(isNeedCleanUp);
281     },
282 
283     /**
284      * Called once per frame. Time is the number of seconds of a frame interval.
285      *
286      * @param {Number} dt
287      */
288     update:function(dt){
289         this.target.removeFromParent(this._isNeedCleanUp);
290     },
291 
292     /**
293      * Initialization of the node, please do not call this function by yourself, you should pass the parameters to constructor to initialize it
.
294      * @param isNeedCleanUp
295      * @returns {boolean}
296      */
297     init:function(isNeedCleanUp){
298         this._isNeedCleanUp = isNeedCleanUp;
299         return true;
300     },
301 
302     /**
303      * returns a reversed action.
304      */
305     reverse:function(){
306         return new cc.RemoveSelf(this._isNeedCleanUp);
307     },
308 
309     /**
310      * to copy object with deep copy.
311      * returns a clone of action.
312      *
313      * @return {cc.RemoveSelf}
314      */
315     clone:function(){
316         return new cc.RemoveSelf(this._isNeedCleanUp);
317     }
318 });
319 
320 /**
321  * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
322  *
323  * @function
324  * @param {Boolean} [isNeedCleanUp=true]
325  * @return {cc.RemoveSelf}
326  *
327  * @example
328  * // example
329  * var removeSelfAction = cc.removeSelf();
330  */
331 cc.removeSelf = function(isNeedCleanUp){
332     return new cc.RemoveSelf(isNeedCleanUp);
333 };
334 
335 /**
336  * Please use cc.removeSelf instead.
337  * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
338  *
339  * @static
340  * @deprecated since v3.0 <br /> Please use cc.removeSelf instead.
341  * @param {Boolean} [isNeedCleanUp=true]
342  * @return {cc.RemoveSelf}
343  */
344 cc.RemoveSelf.create = cc.removeSelf;
345 
346 /**
347  * Flips the sprite horizontally.
348  * @class
349  * @extends cc.ActionInstant
350  * @param {Boolean} flip Indicate whether the target should be flipped or not
351  *
352  * @example
353  * var flipXAction = new cc.FlipX(true);
354  */
355 cc.FlipX = cc.ActionInstant.extend(/** @lends cc.FlipX# */{
356     _flippedX:false,
357 
358 	/**
359      * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
360 	 * Create a FlipX action to flip or unflip the target.
361 	 * @param {Boolean} flip Indicate whether the target should be flipped or not
362 	 */
363     ctor:function(flip){
364         cc.FiniteTimeAction.prototype.ctor.call(this);
365         this._flippedX = false;
366 		flip !== undefined && this.initWithFlipX(flip);
367     },
368 
369     /**
370      * initializes the action with a set flipX.
371      * @param {Boolean} flip
372      * @return {Boolean}
373      */
374     initWithFlipX:function (flip) {
375         this._flippedX = flip;
376         return true;
377     },
378 
379     /**
380      * Called once per frame. Time is the number of seconds of a frame interval.
381      *
382      * @param {Number}  dt
383      */
384     update:function (dt) {
385         this.target.flippedX = this._flippedX;
386     },
387 
388     /**
389      * returns a reversed action.
390      * @return {cc.FlipX}
391      */
392     reverse:function () {
393         return new cc.FlipX(!this._flippedX);
394     },
395 
396     /**
397      * to copy object with deep copy.
398      * returns a clone of action.
399      *
400      * @return {cc.FiniteTimeAction}
401      */
402     clone:function(){
403         var action = new cc.FlipX();
404         action.initWithFlipX(this._flippedX);
405         return action;
406     }
407 });
408 
409 /**
410  * Create a FlipX action to flip or unflip the target.
411  *
412  * @function
413  * @param {Boolean} flip Indicate whether the target should be flipped or not
414  * @return {cc.FlipX}
415  * @example
416  * var flipXAction = cc.flipX(true);
417  */
418 cc.flipX = function (flip) {
419     return new cc.FlipX(flip);
420 };
421 
422 /**
423  * Plese use cc.flipX instead.
424  * Create a FlipX action to flip or unflip the target
425  *
426  * @static
427  * @deprecated since v3.0 <br /> Plese use cc.flipX instead.
428  * @param {Boolean} flip Indicate whether the target should be flipped or not
429  * @return {cc.FlipX}
430  */
431 cc.FlipX.create = cc.flipX;
432 
433 /**
434  * Flips the sprite vertically
435  * @class
436  * @extends cc.ActionInstant
437  * @param {Boolean} flip
438  * @example
439  * var flipYAction = new cc.FlipY(true);
440  */
441 cc.FlipY = cc.ActionInstant.extend(/** @lends cc.FlipY# */{
442     _flippedY:false,
443 
444 	/**
445      * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
446 	 * Create a FlipY action to flip or unflip the target.
447 	 *
448 	 * @param {Boolean} flip
449 	 */
450     ctor: function(flip){
451         cc.FiniteTimeAction.prototype.ctor.call(this);
452         this._flippedY = false;
453 
454 		flip !== undefined && this.initWithFlipY(flip);
455     },
456 
457     /**
458      * initializes the action with a set flipY.
459      * @param {Boolean} flip
460      * @return {Boolean}
461      */
462     initWithFlipY:function (flip) {
463         this._flippedY = flip;
464         return true;
465     },
466 
467     /**
468      * Called once per frame. Time is the number of seconds of a frame interval.
469      *
470      * @param {Number}  dt
471      */
472     update:function (dt) {
473         this.target.flippedY = this._flippedY;
474     },
475 
476     /**
477      * returns a reversed action.
478      * @return {cc.FlipY}
479      */
480     reverse:function () {
481         return new cc.FlipY(!this._flippedY);
482     },
483 
484     /**
485      * to copy object with deep copy.
486      * returns a clone of action.
487      *
488      * @return {cc.FlipY}
489      */
490     clone:function(){
491         var action = new cc.FlipY();
492         action.initWithFlipY(this._flippedY);
493         return action;
494     }
495 });
496 
497 /**
498  * Create a FlipY action to flip or unflip the target.
499  *
500  * @function
501  * @param {Boolean} flip
502  * @return {cc.FlipY}
503  * @example
504  * var flipYAction = cc.flipY(true);
505  */
506 cc.flipY = function (flip) {
507     return new cc.FlipY(flip);
508 };
509 
510 /**
511  * Please use cc.flipY instead
512  * Create a FlipY action to flip or unflip the target
513  *
514  * @static
515  * @deprecated since v3.0 <br /> Please use cc.flipY instead.
516  * @param {Boolean} flip
517  * @return {cc.FlipY}
518  */
519 cc.FlipY.create = cc.flipY;
520 
521 /**
522  * Places the node in a certain position
523  * @class
524  * @extends cc.ActionInstant
525  * @param {cc.Point|Number} pos
526  * @param {Number} [y]
527  * @example
528  * var placeAction = new cc.Place(cc.p(200, 200));
529  * var placeAction = new cc.Place(200, 200);
530  */
531 cc.Place = cc.ActionInstant.extend(/** @lends cc.Place# */{
532     _x: 0,
533 	_y: 0,
534 
535 	/**
536      * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
537 	 * Creates a Place action with a position.
538 	 * @param {cc.Point|Number} pos
539 	 * @param {Number} [y]
540 	 */
541     ctor:function(pos, y){
542         cc.FiniteTimeAction.prototype.ctor.call(this);
543         this._x = 0;
544 	    this._y = 0;
545 
546 		if (pos !== undefined) {
547 			if (pos.x !== undefined) {
548 				y = pos.y;
549 				pos = pos.x;
550 			}
551 			this.initWithPosition(pos, y);
552 		}
553     },
554 
555     /**
556      * Initializes a Place action with a position
557      * @param {number} x
558      * @param {number} y
559      * @return {Boolean}
560      */
561     initWithPosition: function (x, y) {
562         this._x = x;
563         this._y = y;
564         return true;
565     },
566 
567     /**
568      * Called once per frame. Time is the number of seconds of a frame interval.
569      *
570      * @param {Number}  dt
571      */
572     update:function (dt) {
573         this.target.setPosition(this._x, this._y);
574     },
575 
576     /**
577      * to copy object with deep copy.
578      * returns a clone of action.
579      *
580      * @return {cc.Place}
581      */
582     clone:function(){
583         var action = new cc.Place();
584         action.initWithPosition(this._x, this._y);
585         return action;
586     }
587 });
588 
589 /**
590  * Creates a Place action with a position.
591  * @function
592  * @param {cc.Point|Number} pos
593  * @param {Number} [y]
594  * @return {cc.Place}
595  * @example
596  * // example
597  * var placeAction = cc.place(cc.p(200, 200));
598  * var placeAction = cc.place(200, 200);
599  */
600 cc.place = function (pos, y) {
601     return new cc.Place(pos, y);
602 };
603 
604 /**
605  * Please use cc.place instead.
606  * Creates a Place action with a position.
607  * @static
608  * @deprecated since v3.0 <br /> Please use cc.place instead.
609  * @param {cc.Point|Number} pos
610  * @param {Number} [y]
611  * @return {cc.Place}
612  */
613 cc.Place.create = cc.place;
614 
615 
616 /**
617  * Calls a 'callback'.
618  * @class
619  * @extends cc.ActionInstant
620  * @param {function} selector
621  * @param {object|null} [selectorTarget]
622  * @param {*|null} [data] data for function, it accepts all data types.
623  * @example
624  * // example
625  * // CallFunc without data
626  * var finish = new cc.CallFunc(this.removeSprite, this);
627  *
628  * // CallFunc with data
629  * var finish = new cc.CallFunc(this.removeFromParentAndCleanup, this,  true);
630  */
631 cc.CallFunc = cc.ActionInstant.extend(/** @lends cc.CallFunc# */{
632     _selectorTarget:null,
633     _function:null,
634     _data:null,
635 
636     /**
637      * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
638 	 * Creates a CallFunc action with the callback.
639 	 * @param {function} selector
640 	 * @param {object|null} [selectorTarget]
641 	 * @param {*|null} [data] data for function, it accepts all data types.
642 	 */
643     ctor:function(selector, selectorTarget, data){
644         cc.FiniteTimeAction.prototype.ctor.call(this);
645 
646         this.initWithFunction(selector, selectorTarget, data);
647     },
648 
649     /**
650      * Initializes the action with a function or function and its target
651      * @param {function} selector
652      * @param {object|Null} selectorTarget
653      * @param {*|Null} [data] data for function, it accepts all data types.
654      * @return {Boolean}
655      */
656     initWithFunction:function (selector, selectorTarget, data) {
657         if (selector) {
658             this._function = selector;
659         }
660         if (selectorTarget) {
661             this._selectorTarget = selectorTarget;
662         }
663         if (data !== undefined) {
664             this._data = data;
665         }
666         return true;
667     },
668 
669     /**
670      * execute the function.
671      */
672     execute:function () {
673         if (this._function) {
674             this._function.call(this._selectorTarget, this.target, this._data);
675         }
676     },
677 
678     /**
679      * Called once per frame. Time is the number of seconds of a frame interval.
680      *
681      * @param {Number}  dt
682      */
683     update:function (dt) {
684         this.execute();
685     },
686 
687     /**
688      * Get selectorTarget.
689      * @return {object}
690      */
691     getTargetCallback:function () {
692         return this._selectorTarget;
693     },
694 
695     /**
696      * Set selectorTarget.
697      * @param {object} sel
698      */
699     setTargetCallback:function (sel) {
700         if (sel !== this._selectorTarget) {
701             if (this._selectorTarget)
702                 this._selectorTarget = null;
703             this._selectorTarget = sel;
704         }
705     },
706 
707     /**
708      * to copy object with deep copy.
709      * returns a clone of action.
710      *
711      * @return {cc.CallFunc}
712      */
713     clone:function(){
714         var action = new cc.CallFunc();
715         action.initWithFunction(this._function, this._selectorTarget, this._data);
716         return action;
717     }
718 });
719 
720 /**
721  * Creates the action with the callback
722  * @function
723  * @param {function} selector
724  * @param {object|null} [selectorTarget]
725  * @param {*|null} [data] data for function, it accepts all data types.
726  * @return {cc.CallFunc}
727  * @example
728  * // example
729  * // CallFunc without data
730  * var finish = cc.callFunc(this.removeSprite, this);
731  *
732  * // CallFunc with data
733  * var finish = cc.callFunc(this.removeFromParentAndCleanup, this._grossini,  true);
734  */
735 cc.callFunc = function (selector, selectorTarget, data) {
736     return new cc.CallFunc(selector, selectorTarget, data);
737 };
738 
739 /**
740  * Please use cc.callFunc instead.
741  * Creates the action with the callback.
742  * @static
743  * @deprecated since v3.0 <br /> Please use cc.callFunc instead.
744  * @param {function} selector
745  * @param {object|null} [selectorTarget]
746  * @param {*|null} [data] data for function, it accepts all data types.
747  * @return {cc.CallFunc}
748  */
749 cc.CallFunc.create = cc.callFunc;
750