CGfxButton.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. function CGfxButton(iXPos,iYPos,oSprite,oParentContainer){
  2. var _aCbCompleted;
  3. var _aCbOwner;
  4. var _oListenerDown;
  5. var _oListenerRelease;
  6. var _oListenerOver;
  7. var _oListenerMove;
  8. var _oListenerOut;
  9. var _oButton;
  10. var _oParentContainer;
  11. this._init =function(iXPos,iYPos,oSprite){
  12. _aCbCompleted=new Array();
  13. _aCbOwner =new Array();
  14. _oButton = new createBitmap( oSprite);
  15. _oButton.x = iXPos;
  16. _oButton.y = iYPos;
  17. _oButton.regX = oSprite.width/2;
  18. _oButton.regY = oSprite.height/2;
  19. _oButton.cursor = "pointer";
  20. _oParentContainer.addChild(_oButton);
  21. this._initListener();
  22. };
  23. this.unload = function(){
  24. _oButton.off("mousedown",_oListenerDown);
  25. _oButton.off("pressup" , _oListenerRelease);
  26. _oButton.off("pressmove" , _oListenerMove);
  27. if(s_bMobile === false){
  28. _oButton.off("mouseover" , _oListenerOver);
  29. _oButton.off("mouseout" , _oListenerOut);
  30. }
  31. _oParentContainer.removeChild(_oButton);
  32. };
  33. this.setVisible = function(bVisible){
  34. _oButton.visible = bVisible;
  35. };
  36. this._initListener = function(){
  37. _oListenerDown = _oButton.on("mousedown", this.buttonDown);
  38. _oListenerRelease = _oButton.on("pressup" , this.buttonRelease);
  39. _oListenerMove = _oButton.on("pressmove" , this.onPressMove);
  40. if(s_bMobile === false){
  41. _oListenerOver = _oButton.on("mouseover" , this.buttonOver);
  42. _oListenerOut = _oButton.on("mouseout" , this.buttonOut);
  43. }
  44. };
  45. this.addEventListener = function( iEvent,cbCompleted, cbOwner ){
  46. _aCbCompleted[iEvent]=cbCompleted;
  47. _aCbOwner[iEvent] = cbOwner;
  48. };
  49. this.buttonRelease = function(evt){
  50. playSound("click", 1,false);
  51. _oButton.scaleX = 1;
  52. _oButton.scaleY = 1;
  53. if(_aCbCompleted[ON_MOUSE_UP]){
  54. _aCbCompleted[ON_MOUSE_UP].call(_aCbOwner[ON_MOUSE_UP],{event:evt});
  55. }
  56. };
  57. this.buttonDown = function(evt){
  58. _oButton.scaleX = 0.9;
  59. _oButton.scaleY = 0.9;
  60. if(_aCbCompleted[ON_MOUSE_DOWN]){
  61. _aCbCompleted[ON_MOUSE_DOWN].call(_aCbOwner[ON_MOUSE_DOWN],{event:evt});
  62. }
  63. };
  64. this.onPressMove = function(evt){
  65. if(_aCbCompleted[ON_PRESS_MOVE]){
  66. _aCbCompleted[ON_PRESS_MOVE].call(_aCbOwner[ON_PRESS_MOVE],{event:evt});
  67. }
  68. };
  69. this.buttonOver = function(){
  70. if(_aCbCompleted[ON_MOUSE_OVER]){
  71. _aCbCompleted[ON_MOUSE_OVER].call(_aCbOwner[ON_MOUSE_OVER]);
  72. }
  73. };
  74. this.buttonOut = function(){
  75. if(_aCbCompleted[ON_MOUSE_OUT]){
  76. _aCbCompleted[ON_MOUSE_OUT].call(_aCbOwner[ON_MOUSE_OUT]);
  77. }
  78. };
  79. this.setPosition = function(iXPos,iYPos){
  80. _oButton.x = iXPos;
  81. _oButton.y = iYPos;
  82. };
  83. this.setX = function(iXPos){
  84. _oButton.x = iXPos;
  85. };
  86. this.setY = function(iYPos){
  87. _oButton.y = iYPos;
  88. };
  89. this.getSprite = function(){
  90. return _oButton;
  91. };
  92. this.getX = function(){
  93. return _oButton.x;
  94. };
  95. this.getY = function(){
  96. return _oButton.y;
  97. };
  98. _oParentContainer = oParentContainer;
  99. this._init(iXPos,iYPos,oSprite);
  100. return this;
  101. }