CLevelBut.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. function CLevelBut(iXPos, iYPos, oSprite, bActive, Level) {
  2. var _bActive;
  3. var _aCbCompleted;
  4. var _aCbOwner;
  5. var _aButton = new Array();
  6. var _aParams = [];
  7. var _oButton;
  8. this._init = function (iXPos, iYPos, oSprite, bActive) {
  9. _aCbCompleted = new Array();
  10. _aCbOwner = new Array();
  11. var oData = {
  12. images: [oSprite],
  13. // width, height & registration point of each sprite
  14. frames: {width: oSprite.width / 2, height: oSprite.height, regX: (oSprite.width / 2) / 2, regY: oSprite.height / 2},
  15. animations: {state_true: [0], state_false: [1]}
  16. };
  17. var oSpriteSheet = new createjs.SpriteSheet(oData);
  18. _bActive = bActive;
  19. _oButton = createSprite(oSpriteSheet, "state_" + _bActive, (oSprite.width / 2) / 2, oSprite.height / 2, oSprite.width / 2, oSprite.height);
  20. _oButton.mouseEnabled = bActive;
  21. _oButton.x = iXPos;
  22. _oButton.y = iYPos;
  23. _oButton.stop();
  24. if (!s_bMobile){
  25. _oButton.cursor = "pointer";
  26. }
  27. s_oStage.addChild(_oButton);
  28. _aButton.push(_oButton);
  29. var oLevelText;
  30. oLevelText = new createjs.Text(Level, "50px " + PRIMARY_FONT, "#ffff00");
  31. oLevelText.x = iXPos;
  32. oLevelText.y = iYPos + 15;
  33. oLevelText.textAlign = "center";
  34. oLevelText.textBaseline = "alphabetic";
  35. oLevelText.lineWidth = 200;
  36. s_oStage.addChild(oLevelText);
  37. this._initListener();
  38. };
  39. this.unload = function () {
  40. _oButton.off("mousedown", this.buttonDown);
  41. _oButton.off("pressup", this.buttonRelease);
  42. s_oStage.removeChild(_oButton);
  43. };
  44. this._initListener = function () {
  45. _oButton.on("mousedown", this.buttonDown);
  46. _oButton.on("pressup", this.buttonRelease);
  47. };
  48. this.viewBut = function (oButton) {
  49. s_oStage.addChild(oButton);
  50. };
  51. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  52. _aCbCompleted[iEvent] = cbCompleted;
  53. _aCbOwner[iEvent] = cbOwner;
  54. };
  55. this.addEventListenerWithParams = function (iEvent, cbCompleted, cbOwner, aParams) {
  56. _aCbCompleted[iEvent] = cbCompleted;
  57. _aCbOwner[iEvent] = cbOwner;
  58. _aParams = aParams;
  59. };
  60. this.ifClickable = function () {
  61. if (_oButton.mouseEnabled === true) {
  62. return 1;
  63. }
  64. return 0;
  65. };
  66. this.setActive = function (iLevel, bActive) {
  67. _bActive = bActive;
  68. _aButton[iLevel].gotoAndStop("state_" + _bActive);
  69. _aButton[iLevel].mouseEnabled = true;
  70. };
  71. this.buttonRelease = function () {
  72. _oButton.scaleX = 1;
  73. _oButton.scaleY = 1;
  74. playSound("click", 1, false);
  75. _bActive = !_bActive;
  76. _oButton.gotoAndStop("state_" + _bActive);
  77. if (_aCbCompleted[ON_MOUSE_UP]) {
  78. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP], _aParams);
  79. }
  80. };
  81. this.buttonDown = function () {
  82. _oButton.scaleX = 0.9;
  83. _oButton.scaleY = 0.9;
  84. if (_aCbCompleted[ON_MOUSE_DOWN]) {
  85. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN], _aParams);
  86. }
  87. };
  88. this.setPosition = function (iXPos, iYPos) {
  89. _oButton.x = iXPos;
  90. _oButton.y = iYPos;
  91. };
  92. this.setVisible = function (bVisible) {
  93. _oButton.visible = bVisible;
  94. };
  95. this._init(iXPos, iYPos, oSprite, bActive);
  96. }