CToggle.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. function CToggle(iXPos,iYPos,oSprite,bActive){
  2. var _bActive;
  3. var _aCbCompleted;
  4. var _aCbOwner;
  5. var _aParams = [];
  6. var _oListenerDown;
  7. var _oListenerUp;
  8. var _oButton;
  9. this._init = function(iXPos,iYPos,oSprite,bActive){
  10. _aCbCompleted=new Array();
  11. _aCbOwner =new Array();
  12. var oData = {
  13. images: [oSprite],
  14. // width, height & registration point of each sprite
  15. frames: {width: oSprite.width/2, height: oSprite.height, regX: (oSprite.width/2)/2, regY: oSprite.height/2},
  16. animations: {state_true:[0],state_false:[1]}
  17. };
  18. var oSpriteSheet = new createjs.SpriteSheet(oData);
  19. _bActive = bActive;
  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.cursor = "pointer";
  24. _oButton.stop();
  25. s_oStage.addChild(_oButton);
  26. this._initListener();
  27. };
  28. this.unload = function(){
  29. _oButton.off("mousedown", _oListenerDown);
  30. _oButton.off("pressup" , _oListenerUp);
  31. s_oStage.removeChild(_oButton);
  32. };
  33. this._initListener = function(){
  34. _oListenerDown = _oButton.on("mousedown", this.buttonDown);
  35. _oListenerUp = _oButton.on("pressup" , this.buttonRelease);
  36. };
  37. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  38. _aCbCompleted[iEvent]=cbCompleted;
  39. _aCbOwner[iEvent] = cbOwner;
  40. };
  41. this.addEventListenerWithParams = function(iEvent,cbCompleted, cbOwner,aParams){
  42. _aCbCompleted[iEvent]=cbCompleted;
  43. _aCbOwner[iEvent] = cbOwner;
  44. _aParams = aParams;
  45. };
  46. this.setActive = function(bActive){
  47. _bActive = bActive;
  48. _oButton.gotoAndStop("state_"+_bActive);
  49. };
  50. this.buttonRelease = function(){
  51. _oButton.scaleX = 1;
  52. _oButton.scaleY = 1;
  53. playSound("click",1,false);
  54. _bActive = !_bActive;
  55. _oButton.gotoAndStop("state_"+_bActive);
  56. playSound("click", 1, false);
  57. if(_aCbCompleted[ON_MOUSE_UP]){
  58. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],_aParams);
  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],_aParams);
  66. }
  67. };
  68. this.setPosition = function(iXPos,iYPos){
  69. _oButton.x = iXPos;
  70. _oButton.y = iYPos;
  71. };
  72. this.setVisible = function(bVisible){
  73. _oButton.visible = bVisible;
  74. };
  75. this._init(iXPos,iYPos,oSprite,bActive);
  76. }