CNumToggle.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. function CNumToggle(iXPos, iYPos, iNum, oParentContainer) {
  2. var _bActive;
  3. var _bBlock;
  4. var _aCbCompleted;
  5. var _aCbOwner;
  6. var _oButton;
  7. var _oButtonBg;
  8. var _oExtracted;
  9. var _aParams = [];
  10. this._init = function (iXPos, iYPos, iNum, oParentContainer) {
  11. _bBlock = false;
  12. _aCbCompleted = new Array();
  13. _aCbOwner = new Array();
  14. _oButton = new createjs.Container();
  15. _oButton.x = iXPos;
  16. _oButton.y = iYPos;
  17. oParentContainer.addChild(_oButton);
  18. var oSprite = s_oSpriteLibrary.getSprite('num_button');
  19. var oData = {
  20. images: [oSprite],
  21. framerate: 5,
  22. // width, height & registration point of each sprite
  23. frames: {width: oSprite.width / 2, height: oSprite.height, regX: (oSprite.width / 2) / 2, regY: oSprite.height / 2},
  24. animations: {state_true: [0], state_false: [1]}
  25. };
  26. var oSpriteSheet = new createjs.SpriteSheet(oData);
  27. _bActive = false;
  28. _oButtonBg = createSprite(oSpriteSheet, "state_" + _bActive, (oSprite.width / 2) / 2, oSprite.height / 2, oSprite.width / 2, oSprite.height);
  29. _oButtonBg.stop();
  30. var oSprite = s_oSpriteLibrary.getSprite('ball');
  31. var oData = {
  32. images: [oSprite],
  33. // width, height & registration point of each sprite
  34. frames: {width: oSprite.width / NUM_DIFFERENT_BALLS, height: oSprite.height, regX: (oSprite.width / NUM_DIFFERENT_BALLS) / 2, regY: oSprite.height / 2},
  35. animations: {red: [0], green: [1], cyan: [0], violet: [1], blue: [1]}
  36. };
  37. var oSpriteSheet = new createjs.SpriteSheet(oData);
  38. _oExtracted = createSprite(oSpriteSheet, "red", (oSprite.width / NUM_DIFFERENT_BALLS) / 2, oSprite.height / 2, oSprite.width / NUM_DIFFERENT_BALLS, oSprite.height);//new createBitmap(oSprite);
  39. _oExtracted.gotoAndStop(0);
  40. _oExtracted.visible = false;
  41. if (!s_bMobile){
  42. _oButton.cursor = "pointer";
  43. }
  44. _oButton.addChild(_oButtonBg, _oExtracted);
  45. this._initListener();
  46. };
  47. this.unload = function () {
  48. _oButton.off("mousedown", this.buttonDown);
  49. _oButton.off("pressup", this.buttonRelease);
  50. oParentContainer.removeChild(_oButton);
  51. };
  52. this._initListener = function () {
  53. _oButton.on("mousedown", this.buttonDown);
  54. _oButton.on("pressup", this.buttonRelease);
  55. };
  56. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  57. _aCbCompleted[iEvent] = cbCompleted;
  58. _aCbOwner[iEvent] = cbOwner;
  59. };
  60. this.addEventListenerWithParams = function (iEvent, cbCompleted, cbOwner, aParams) {
  61. _aCbCompleted[iEvent] = cbCompleted;
  62. _aCbOwner[iEvent] = cbOwner;
  63. _aParams = aParams;
  64. };
  65. this.setActive = function (bActive) {
  66. _bActive = bActive;
  67. _oButtonBg.gotoAndStop("state_" + _bActive);
  68. };
  69. this.buttonRelease = function () {
  70. if (_bBlock) {
  71. return;
  72. }
  73. playSound("click", 1, false);
  74. _bActive = !_bActive;
  75. _oButtonBg.gotoAndStop("state_" + _bActive);
  76. if (_aCbCompleted[ON_MOUSE_UP]) {
  77. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP], _aParams);
  78. }
  79. };
  80. this.buttonDown = function () {
  81. if (_bBlock) {
  82. return;
  83. }
  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.getGlobalPosition = function () {
  93. return {x: _oButton.localToGlobal(0, 0).x, y: _oButton.localToGlobal(0, 0).y};
  94. };
  95. this.block = function (bVal) {
  96. _bBlock = bVal;
  97. };
  98. this.setExtracted = function (bVal, iColor) {
  99. _oExtracted.visible = bVal;
  100. _oExtracted.gotoAndStop(iColor);
  101. };
  102. this.highlight = function () {
  103. _oButtonBg.gotoAndPlay(0);
  104. };
  105. this.stopHighlight = function () {
  106. _oButtonBg.gotoAndStop(1);
  107. };
  108. this._init(iXPos, iYPos, iNum, oParentContainer);
  109. }