CCActionInstant.js 11 KB

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