CGfxButton.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. function CGfxButton(iXPos, iYPos, oSprite, oParentContainer) {
  2. var _iScaleY;
  3. var _iScaleX;
  4. var _aCbCompleted;
  5. var _aCbOwner;
  6. var _aParams = [];
  7. var _oButton;
  8. var _oTween;
  9. var _oParent;
  10. var _oParentContainer;
  11. var _bBlock;
  12. this._init = function (iXPos, iYPos, oSprite, oParentContainer) {
  13. _iScaleX = 1;
  14. _iScaleY = 1;
  15. _aCbCompleted = new Array();
  16. _aCbOwner = new Array();
  17. _oButton = createBitmap(oSprite);
  18. _oButton.x = iXPos;
  19. _oButton.y = iYPos;
  20. _oButton.regX = oSprite.width / 2;
  21. _oButton.regY = oSprite.height / 2;
  22. if (!s_bMobile)
  23. _oButton.cursor = "pointer";
  24. if (_oParentContainer) {
  25. _oParentContainer.addChild(_oButton);
  26. } else {
  27. s_oStage.addChild(_oButton);
  28. }
  29. _bBlock = false;
  30. this._initListener();
  31. };
  32. this.unload = function () {
  33. createjs.Tween.removeTweens(_oButton);
  34. _oButton.off("mousedown", this.buttonDown);
  35. _oButton.off("pressup", this.buttonRelease);
  36. if (_oParentContainer) {
  37. _oParentContainer.removeChild(_oButton);
  38. } else {
  39. s_oStage.removeChild(_oButton);
  40. }
  41. };
  42. this.setVisible = function (bVisible) {
  43. _oButton.visible = bVisible;
  44. };
  45. this._initListener = function () {
  46. _oButton.on("mousedown", this.buttonDown);
  47. _oButton.on("pressup", this.buttonRelease);
  48. };
  49. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  50. _aCbCompleted[iEvent] = cbCompleted;
  51. _aCbOwner[iEvent] = cbOwner;
  52. };
  53. this.addEventListenerWithParams = function (iEvent, cbCompleted, cbOwner, aParams) {
  54. _aCbCompleted[iEvent] = cbCompleted;
  55. _aCbOwner[iEvent] = cbOwner;
  56. _aParams = aParams;
  57. };
  58. this.buttonRelease = function () {
  59. if (_bBlock) {
  60. return;
  61. }
  62. _oButton.scaleX = _iScaleX;
  63. _oButton.scaleY = _iScaleY;
  64. playSound("click", 1, false);
  65. if (_aCbCompleted[ON_MOUSE_UP]) {
  66. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP], _aParams);
  67. }
  68. };
  69. this.buttonDown = function () {
  70. if (_bBlock) {
  71. return;
  72. }
  73. _oButton.scaleX = _iScaleX * 0.9;
  74. _oButton.scaleY = _iScaleY * 0.9;
  75. if (_aCbCompleted[ON_MOUSE_DOWN]) {
  76. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN], _aParams);
  77. }
  78. };
  79. this.setScale = function (iValue) {
  80. _iScaleX = iValue;
  81. _iScaleY = iValue;
  82. _oButton.scaleX = iValue;
  83. _oButton.scaleY = iValue;
  84. };
  85. this.setScaleX = function (iValue) {
  86. _iScaleX = iValue;
  87. _oButton.scaleX = iValue;
  88. };
  89. this.setPosition = function (iXPos, iYPos) {
  90. _oButton.x = iXPos;
  91. _oButton.y = iYPos;
  92. };
  93. this.setX = function (iXPos) {
  94. _oButton.x = iXPos;
  95. };
  96. this.setY = function (iYPos) {
  97. _oButton.y = iYPos;
  98. };
  99. this.getButtonImage = function () {
  100. return _oButton;
  101. };
  102. this.getX = function () {
  103. return _oButton.x;
  104. };
  105. this.getY = function () {
  106. return _oButton.y;
  107. };
  108. this.block = function (bVal) {
  109. _bBlock = bVal;
  110. };
  111. this.rotation = function (iVal) {
  112. _oButton.rotation = iVal;
  113. };
  114. this.pulseAnimation = function () {
  115. _oTween = createjs.Tween.get(_oButton,{loop:-1}).to({scaleX: _iScaleX * 0.9, scaleY: _iScaleY * 0.9}, 850, createjs.Ease.quadOut).to({scaleX: _iScaleX, scaleY: _iScaleY}, 650, createjs.Ease.quadIn);
  116. };
  117. this.trebleAnimation = function () {
  118. _oTween = createjs.Tween.get(_oButton).to({rotation: 5}, 75, createjs.Ease.quadOut).to({rotation: -5}, 140, createjs.Ease.quadIn).to({rotation: 0}, 75, createjs.Ease.quadIn).wait(750).call(function () {
  119. _oParent.trebleAnimation();
  120. });
  121. };
  122. _oParentContainer = oParentContainer;
  123. _oParent = this;
  124. this._init(iXPos, iYPos, oSprite, oParentContainer);
  125. return this;
  126. }