CToggle.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. function CToggle(iXPos, iYPos, oSprite, bActive, oParentContainer) {
  2. var _bActive;
  3. var _aCbCompleted;
  4. var _aCbOwner;
  5. var _oButton;
  6. var _oListenerMouseDown;
  7. var _oListenerMouseUp;
  8. var _oParentContainer;
  9. this._init = function (iXPos, iYPos, oSprite, bActive, oParentContainer) {
  10. if (oParentContainer !== undefined) {
  11. _oParentContainer = oParentContainer;
  12. } else {
  13. _oParentContainer = s_oStage;
  14. }
  15. _aCbCompleted = new Array();
  16. _aCbOwner = new Array();
  17. var oData = {
  18. images: [oSprite],
  19. // width, height & registration point of each sprite
  20. frames: {width: oSprite.width / 2, height: oSprite.height, regX: (oSprite.width / 2) / 2, regY: oSprite.height / 2},
  21. animations: {state_true: [0], state_false: [1]}
  22. };
  23. var oSpriteSheet = new createjs.SpriteSheet(oData);
  24. _bActive = bActive;
  25. _oButton = createSprite(oSpriteSheet, "state_" + _bActive, (oSprite.width / 2) / 2, oSprite.height / 2, oSprite.width / 2, oSprite.height);
  26. _oButton.x = iXPos;
  27. _oButton.y = iYPos;
  28. _oButton.stop();
  29. if (!s_bMobile)
  30. _oButton.cursor = "pointer";
  31. _oParentContainer.addChild(_oButton);
  32. this._initListener();
  33. };
  34. this.unload = function () {
  35. _oButton.off("mousedown", _oListenerMouseDown);
  36. _oButton.off("pressup", _oListenerMouseUp);
  37. _oParentContainer.removeChild(_oButton);
  38. };
  39. this._initListener = function () {
  40. _oListenerMouseDown = _oButton.on("mousedown", this.buttonDown);
  41. _oListenerMouseUp = _oButton.on("pressup", this.buttonRelease);
  42. };
  43. this.addEventListener = function (iEvent, cbCompleted, cbOwner) {
  44. _aCbCompleted[iEvent] = cbCompleted;
  45. _aCbOwner[iEvent] = cbOwner;
  46. };
  47. this.setCursorType = function (szValue) {
  48. _oButton.cursor = szValue;
  49. };
  50. this.setActive = function (bActive) {
  51. _bActive = bActive;
  52. _oButton.gotoAndStop("state_" + _bActive);
  53. };
  54. this.buttonRelease = function () {
  55. _oButton.scaleX = 1;
  56. _oButton.scaleY = 1;
  57. playSound("click", 1,false);
  58. _bActive = !_bActive;
  59. _oButton.gotoAndStop("state_" + _bActive);
  60. if (_aCbCompleted[ON_MOUSE_UP]) {
  61. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP], _bActive);
  62. }
  63. };
  64. this.buttonDown = function () {
  65. _oButton.scaleX = 0.9;
  66. _oButton.scaleY = 0.9;
  67. if (_aCbCompleted[ON_MOUSE_DOWN]) {
  68. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN]);
  69. }
  70. };
  71. this.setPosition = function (iX, iY) {
  72. _oButton.x = iX;
  73. _oButton.y = iY;
  74. };
  75. this._init(iXPos, iYPos, oSprite, bActive, oParentContainer);
  76. }