CToggle.js 2.7 KB

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