CCActionInstant.js 19 KB

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