CCard.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. function CCard(iX,iY,iType,fScalingFactor,oContainer){
  2. var _bSuspendUpdates = false;
  3. var _bFolded;
  4. var _sType;
  5. var _iX;
  6. var _iY;
  7. var _fScaleFactor;
  8. var _oSprite;
  9. var _scope;
  10. var _oContainer;
  11. this._init = function(iX,iY,iType,fScalingFactor,oContainer){
  12. _iX = iX;
  13. _iY = iY;
  14. _bEliminated = false;
  15. _bFolded = true;
  16. _sType = iType;
  17. _oContainer = oContainer;
  18. _oSprite = createSprite(s_oGame.spriteSheet,"back",CARD_WIDTH/2,CARD_HEIGHT/2,CARD_WIDTH,CARD_HEIGHT);
  19. if (_iX < CANVAS_WIDTH/2) {
  20. _oSprite.x = _iX - CANVAS_WIDTH;
  21. } else {
  22. _oSprite.x = CANVAS_WIDTH + _iX;
  23. };
  24. _oSprite.y = _iY + Math.floor((Math.random()-0.5)*0);
  25. _fScaleFactor = fScalingFactor;
  26. _oSprite.scaleX = _oSprite.scaleY = _fScaleFactor;
  27. _scope = this;
  28. _oSprite.on("click", function() {
  29. this.clicked();
  30. },_scope);
  31. if (!s_bMobile)
  32. _oSprite.cursor = "pointer";
  33. _bSuspendUpdates = true;
  34. s_oGame.suspendUpdates();
  35. _oContainer.addChild(_oSprite);
  36. var oParent = this;
  37. createjs.Tween.get(_oSprite).to({alpha:1, x:_iX, y:_iY}, 500).call(function handleComplete(){
  38. _bSuspendUpdates = false;
  39. s_oGame.restartUpdates();
  40. if(TIME_SHOW_CARDS > 0){
  41. oParent.showCardFirstTime();
  42. }
  43. });
  44. };
  45. this.unload = function(){
  46. _oContainer.removeChild(_oSprite);
  47. };
  48. this.update = function(){
  49. };
  50. this.clicked = function(){
  51. if (_bSuspendUpdates === false && _bFolded) {
  52. s_oGame.cardClicked(this,_sType);
  53. playSound("card", 1, false);
  54. this.clickListener();
  55. };
  56. };
  57. this.showCardFirstTime = function(){
  58. _bSuspendUpdates = true;
  59. createjs.Tween.get(_oSprite).to({scaleX:0.1}, 100).call(function(){
  60. _oContainer.removeChild(_oSprite);
  61. _oSprite = createSprite(s_oGame.spriteSheet,_sType,CARD_WIDTH/2,CARD_HEIGHT/2,CARD_WIDTH,CARD_HEIGHT);
  62. _oSprite.x = _iX;
  63. _oSprite.y = _iY;
  64. _oSprite.scaleX = _oSprite.scaleY = _fScaleFactor;
  65. _oContainer.addChild(_oSprite);
  66. createjs.Tween.get(_oSprite).wait(TIME_SHOW_CARDS).to({scaleX:_fScaleFactor}, 100).call(function(){
  67. _bSuspendUpdates = false;
  68. _bFolded = false;
  69. _scope.clickListener();
  70. _scope.flipCard();
  71. },_scope);
  72. });
  73. };
  74. this.flipCard = function(){
  75. if (_bFolded === true) {
  76. _bSuspendUpdates = true;
  77. createjs.Tween.get(_oSprite).to({scaleX:0.1}, 100).call(function(){
  78. _oContainer.removeChild(_oSprite);
  79. _oSprite = createSprite(s_oGame.spriteSheet,_sType,CARD_WIDTH/2,CARD_HEIGHT/2,CARD_WIDTH,CARD_HEIGHT);
  80. _oSprite.x = _iX;
  81. _oSprite.y = _iY;
  82. _oSprite.scaleX = _oSprite.scaleY = _fScaleFactor;
  83. _oContainer.addChild(_oSprite);
  84. createjs.Tween.get(_oSprite).to({scaleX:_fScaleFactor}, 100).call(function(){
  85. _bSuspendUpdates = false;
  86. _bFolded = false;
  87. s_oGame.addFlippedCard();
  88. _scope.clickListener();
  89. },_scope);
  90. });
  91. } else {
  92. playSound("card", 1, false);
  93. _bSuspendUpdates = true;
  94. createjs.Tween.get(_oSprite).to({scaleX:0.1}, 100).call(function(){
  95. _oContainer.removeChild(_oSprite);
  96. _oSprite = createSprite(s_oGame.spriteSheet,"back",CARD_WIDTH/2,CARD_HEIGHT/2,CARD_WIDTH,CARD_HEIGHT);
  97. _oSprite.x = _iX;
  98. _oSprite.y = _iY;
  99. _oSprite.scaleX = _oSprite.scaleY = _fScaleFactor;
  100. _oContainer.addChild(_oSprite);
  101. createjs.Tween.get(_oSprite).to({scaleX:_fScaleFactor}, 100).call(function(){
  102. _bSuspendUpdates = false;
  103. s_oGame.restartUpdates();
  104. _bFolded = true;
  105. _scope.clickListener();
  106. return true;
  107. },_scope);
  108. });
  109. };
  110. }
  111. this.clickListener = function(){
  112. if (_bSuspendUpdates === false) {
  113. _oSprite.on("click", function() {
  114. this.clicked();
  115. },_scope);
  116. };
  117. };
  118. this.display = function(){
  119. };
  120. this.isFolded = function(){
  121. return _bFolded;
  122. };
  123. this.getType = function(){
  124. return _sType;
  125. };
  126. this.eliminateCard = function(){
  127. _bSuspendUpdates = true;
  128. s_oGame.suspendUpdates();
  129. _oSprite.alpha = 1;
  130. createjs.Tween.get(_oSprite).to({alpha:0}, 400).call(function handleComplete(){
  131. _oContainer.removeChild(_oSprite);
  132. _bSuspendUpdates = false;
  133. s_oGame.restartUpdates();
  134. });
  135. };
  136. this._init(iX,iY,iType,fScalingFactor,oContainer);
  137. }