CToggle.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. function CToggle(iXPos,iYPos,oSprite,bActive,oParentContainer){
  2. var _bDisable;
  3. var _bActive;
  4. var _aCbCompleted;
  5. var _aCbOwner;
  6. var _oButton;
  7. var _oParentContainer;
  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. _bDisable = false;
  20. _oButton = createSprite(oSpriteSheet, "state_"+_bActive,(oSprite.width/2)/2,oSprite.height/2,oSprite.width/2,oSprite.height);
  21. _oButton.x = iXPos;
  22. _oButton.y = iYPos;
  23. _oButton.stop();
  24. _oParentContainer.addChild(_oButton);
  25. this._initListener();
  26. };
  27. this.unload = function(){
  28. _oButton.off("mousedown", this.buttonDown);
  29. _oButton.off("pressup" , this.buttonRelease);
  30. if(!s_bMobile){
  31. _oButton.off("mouseover", this.buttonOver);
  32. }
  33. _oParentContainer.removeChild(_oButton);
  34. };
  35. this._initListener = function(){
  36. _oButton.on("mousedown", this.buttonDown);
  37. _oButton.on("pressup" , this.buttonRelease);
  38. if(!s_bMobile){
  39. _oButton.on("mouseover", this.buttonOver);
  40. }
  41. };
  42. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  43. _aCbCompleted[iEvent]=cbCompleted;
  44. _aCbOwner[iEvent] = cbOwner;
  45. };
  46. this.setActive = function(bActive){
  47. _bActive = bActive;
  48. _oButton.gotoAndStop("state_"+_bActive);
  49. };
  50. this.enable = function(){
  51. _bDisable = false;
  52. };
  53. this.disable = function(){
  54. _bDisable = true;
  55. };
  56. this.buttonRelease = function(){
  57. if(_bDisable){
  58. return
  59. };
  60. _oButton.scaleX = 1;
  61. _oButton.scaleY = 1;
  62. playSound("click",1,false);
  63. _bActive = !_bActive;
  64. _oButton.gotoAndStop("state_"+_bActive);
  65. if(_aCbCompleted[ON_MOUSE_UP]){
  66. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],_bActive);
  67. }
  68. };
  69. this.buttonDown = function(){
  70. if(_bDisable){
  71. return
  72. };
  73. _oButton.scaleX = 0.9;
  74. _oButton.scaleY = 0.9;
  75. if(_aCbCompleted[ON_MOUSE_DOWN]){
  76. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN]);
  77. }
  78. };
  79. this.buttonOver = function(evt){
  80. if(_bDisable){
  81. return
  82. };
  83. if(!s_bMobile){
  84. evt.target.cursor = "pointer";
  85. }
  86. };
  87. this.setPosition = function(iXPos,iYPos){
  88. _oButton.x = iXPos;
  89. _oButton.y = iYPos;
  90. };
  91. _oParentContainer = oParentContainer;
  92. this._init(iXPos,iYPos,oSprite,bActive);
  93. }