CNumToggle.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. _oButton.addChild(_oButtonBg, _oExtracted);
  42. this._initListener();
  43. };
  44. this.unload = function () {
  45. _oButton.off("mousedown", this.buttonDown);
  46. _oButton.off("pressup", this.buttonRelease);
  47. oParentContainer.removeChild(_oButton);
  48. };
  49. this._initListener = function () {
  50. _oButton.on("mousedown", this.buttonDown);
  51. _oButton.on("pressup", this.buttonRelease);
  52. };
  53. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  54. _aCbCompleted[iEvent] = cbCompleted;
  55. _aCbOwner[iEvent] = cbOwner;
  56. };
  57. this.addEventListenerWithParams = function (iEvent, cbCompleted, cbOwner, aParams) {
  58. _aCbCompleted[iEvent] = cbCompleted;
  59. _aCbOwner[iEvent] = cbOwner;
  60. _aParams = aParams;
  61. };
  62. this.setActive = function (bActive) {
  63. _bActive = bActive;
  64. _oButtonBg.gotoAndStop("state_" + _bActive);
  65. };
  66. this.buttonRelease = function () {
  67. if (_bBlock) {
  68. return;
  69. }
  70. playSound("click", 1, false);
  71. _bActive = !_bActive;
  72. _oButtonBg.gotoAndStop("state_" + _bActive);
  73. if (_aCbCompleted[ON_MOUSE_UP]) {
  74. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP], _aParams);
  75. }
  76. };
  77. this.buttonDown = function () {
  78. if (_bBlock) {
  79. return;
  80. }
  81. if (_aCbCompleted[ON_MOUSE_DOWN]) {
  82. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN], _aParams);
  83. }
  84. };
  85. this.setPosition = function (iXPos, iYPos) {
  86. _oButton.x = iXPos;
  87. _oButton.y = iYPos;
  88. };
  89. this.getGlobalPosition = function () {
  90. return {x: _oButton.localToGlobal(0, 0).x, y: _oButton.localToGlobal(0, 0).y};
  91. };
  92. this.block = function (bVal) {
  93. _bBlock = bVal;
  94. };
  95. this.setExtracted = function (bVal, iColor) {
  96. _oExtracted.visible = bVal;
  97. _oExtracted.gotoAndStop(iColor);
  98. };
  99. this.highlight = function () {
  100. _oButtonBg.gotoAndPlay(0);
  101. };
  102. this.stopHighlight = function () {
  103. _oButtonBg.gotoAndStop(1);
  104. };
  105. this._init(iXPos, iYPos, iNum, oParentContainer);
  106. }