CToggle.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function CToggle(iXPos,iYPos,oSprite,bActive,oParentContainer){
  2. var _bActive;
  3. var _aCbCompleted;
  4. var _aCbOwner;
  5. var _aParams = [];
  6. var _oListenerDown;
  7. var _oListenerUp;
  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.cursor = "pointer";
  25. _oButton.stop();
  26. _oParentContainer.addChild(_oButton);
  27. this._initListener();
  28. };
  29. this.unload = function(){
  30. _oButton.off("mousedown", _oListenerDown);
  31. _oButton.off("pressup" , _oListenerUp);
  32. _oParentContainer.removeChild(_oButton);
  33. };
  34. this._initListener = function(){
  35. _oListenerDown = _oButton.on("mousedown", this.buttonDown);
  36. _oListenerUp = _oButton.on("pressup" , this.buttonRelease);
  37. };
  38. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  39. _aCbCompleted[iEvent]=cbCompleted;
  40. _aCbOwner[iEvent] = cbOwner;
  41. };
  42. this.addEventListenerWithParams = function(iEvent,cbCompleted, cbOwner,aParams){
  43. _aCbCompleted[iEvent]=cbCompleted;
  44. _aCbOwner[iEvent] = cbOwner;
  45. _aParams = aParams;
  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. _bActive = !_bActive;
  55. _oButton.gotoAndStop("state_"+_bActive);
  56. if(_aCbCompleted[ON_MOUSE_UP]){
  57. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],_aParams);
  58. }
  59. };
  60. this.buttonDown = function(){
  61. _oButton.scaleX = 0.9;
  62. _oButton.scaleY = 0.9;
  63. if(_aCbCompleted[ON_MOUSE_DOWN]){
  64. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN],_aParams);
  65. }
  66. };
  67. this.setPosition = function(iXPos,iYPos){
  68. _oButton.x = iXPos;
  69. _oButton.y = iYPos;
  70. };
  71. this.setVisible = function(bVisible){
  72. _oButton.visible = bVisible;
  73. };
  74. _oParentContainer = oParentContainer;
  75. this._init(iXPos,iYPos,oSprite,bActive);
  76. }