CPowerBar.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. function CPowerBar(iXPos, iYPos, oParentContainer) {
  2. var _oContainer;
  3. var _pStartPos;
  4. var _oArrowMask;
  5. var _oArrow;
  6. var _oArrowFill;
  7. var _oArrowFrame;
  8. var _iMaskWidth;
  9. var _iMaskHeight;
  10. var _oTextPower;
  11. var _oParentContainer;
  12. this._init = function (iXPos, iYPos) {
  13. _pStartPos = {x: iXPos, y: iYPos};
  14. _oContainer = new createjs.Container();
  15. _oContainer.x = iXPos;
  16. _oContainer.y = iYPos;
  17. _oParentContainer.addChild(_oContainer);
  18. var oSpriteArrow = s_oSpriteLibrary.getSprite("power_bar_bg");
  19. _oArrow = createBitmap(oSpriteArrow);
  20. _oArrow.regX = oSpriteArrow.width * 0.5;
  21. _oArrow.regY = oSpriteArrow.height;
  22. _oContainer.addChild(_oArrow);
  23. _oArrowFill = createBitmap(s_oSpriteLibrary.getSprite("power_bar_fill"));
  24. _oArrowFill.regX = oSpriteArrow.width * 0.5;
  25. _oArrowFill.regY = oSpriteArrow.height;
  26. _oContainer.addChild(_oArrowFill);
  27. _iMaskWidth = oSpriteArrow.width;
  28. _iMaskHeight = oSpriteArrow.height;
  29. _oArrowMask = new createjs.Shape();
  30. _oArrowMask.graphics.beginFill("rgba(0,0,0,0.01)").drawRect(_oArrow.x, _oArrow.y, _iMaskWidth, 0);
  31. _oArrowMask.regX = _iMaskWidth * 0.5;
  32. _oArrowMask.regY = _iMaskHeight;
  33. _oContainer.addChild(_oArrowMask);
  34. var oSpriteArrowFrame = s_oSpriteLibrary.getSprite("power_bar_frame");
  35. _oArrowFrame = createBitmap(oSpriteArrowFrame);
  36. _oArrowFrame.regX = oSpriteArrowFrame.width * 0.5;
  37. _oArrowFrame.regY = oSpriteArrowFrame.height;
  38. _oContainer.addChild(_oArrowFrame);
  39. _oContainer.scaleY = -1;
  40. _oArrowFill.mask = _oArrowMask;
  41. this.createTextPower();
  42. };
  43. this.unload = function () {
  44. _oParentContainer.removeChild(_oContainer);
  45. };
  46. this.setVisible = function (bVisible) {
  47. _oContainer.visible = bVisible;
  48. };
  49. this.createTextPower = function () {
  50. _oTextPower = new createjs.Text(TEXT_POWER, 36 + "px " + FONT_GAME, "#ffffff");
  51. _oTextPower.textAlign = "center";
  52. _oTextPower.textBaseline = "middle";
  53. _oTextPower.y = -_iMaskHeight - 50;
  54. _oTextPower.scaleY = -1;
  55. _oContainer.addChild(_oTextPower);
  56. };
  57. this.setPosition = function (iXPos, iYPos) {
  58. _oContainer.x = iXPos;
  59. _oContainer.y = iYPos;
  60. };
  61. this.setX = function (iXPos) {
  62. _oContainer.x = iXPos;
  63. };
  64. this.setY = function (iYPos) {
  65. _oContainer.y = iYPos;
  66. };
  67. this.getX = function () {
  68. return _oContainer.x;
  69. };
  70. this.getY = function () {
  71. return _oContainer.y;
  72. };
  73. this.getStartPos = function () {
  74. return _pStartPos;
  75. };
  76. this.removeTweensMask = function () {
  77. createjs.Tween.removeTweens(_oArrowMask.graphics.command);
  78. };
  79. this.removeTweensContainer = function () {
  80. createjs.Tween.removeTweens(_oContainer);
  81. };
  82. this.animFade = function (fAlpha) {
  83. createjs.Tween.get(_oContainer).to({alpha: fAlpha}, 250, createjs.Ease.circleOut).call(function () {
  84. if (fAlpha === 0) {
  85. _oContainer.visible = false;
  86. }
  87. });
  88. };
  89. this.getMaskValue = function () {
  90. return _oArrowMask.graphics.command.h;
  91. };
  92. this.getMaskHeight = function () {
  93. return _iMaskHeight;
  94. };
  95. this.getAngle = function () {
  96. return _oContainer.rotation;
  97. };
  98. this.mask = function (iVal) {
  99. _oArrowMask.graphics.clear();
  100. var iNewMaskHeight = Math.floor((iVal * _iMaskHeight) / 100);
  101. _oArrowMask.graphics.beginFill("rgba(0,0,0,0.01)").drawRect(_oArrow.x, _oArrow.y, _iMaskWidth, iNewMaskHeight);
  102. };
  103. this.animateMask = function (iTime) {
  104. var oParent = this;
  105. createjs.Tween.get(_oArrowMask.graphics.command).to({h: _iMaskHeight}, iTime, createjs.Ease.cubicIn).call(function () {
  106. createjs.Tween.get(_oArrowMask.graphics.command).to({h: 0}, iTime, createjs.Ease.cubicOut).call(function () {
  107. oParent.animateMask(iTime);
  108. });
  109. });
  110. };
  111. this.animateRotation = function (iTime) {
  112. var oParent = this;
  113. createjs.Tween.get(_oContainer).to({rotation: MAX_EFFECT_ANGLE}, iTime, createjs.Ease.cubicInOut).call(function () {
  114. createjs.Tween.get(_oContainer).to({rotation: -MAX_EFFECT_ANGLE}, iTime, createjs.Ease.cubicInOut).call(function () {
  115. oParent.animateRotation(iTime);
  116. });
  117. });
  118. };
  119. _oParentContainer = oParentContainer;
  120. this._init(iXPos, iYPos);
  121. return this;
  122. }