CToggle.js 3.0 KB

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