Button.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const {ccclass, property} = cc._decorator;
  2. @ccclass
  3. export default class Button extends cc.Component {
  4. @property(cc.SpriteFrame)
  5. normal: cc.SpriteFrame = null;
  6. @property(cc.SpriteFrame)
  7. pressed: cc.SpriteFrame = null;
  8. @property
  9. pressedScale: number = 1.1;
  10. @property
  11. transDuration: number = 0.1;
  12. @property([cc.Node])
  13. private nodes: cc.Node[] = [];
  14. initScale: number = 0;
  15. onLoad () {
  16. // let that = this;
  17. this.initScale = this.node.scale;
  18. // function onTouchDown (event) {
  19. // that.node.getComponent(cc.Sprite).spriteFrame = that.pressed;
  20. // let scaleDownAction = cc.scaleTo(that.transDuration, that.pressedScale);
  21. // this.stopAllActions();
  22. // this.runAction(scaleDownAction);
  23. // console.log('按下');
  24. // }
  25. // function onTouchUp (event) {
  26. // that.node.getComponent(cc.Sprite).spriteFrame = that.normal;
  27. // let scaleUpAction = cc.scaleTo(that.transDuration, that.initScale);
  28. // this.stopAllActions();
  29. // this.runAction(scaleUpAction);
  30. // }
  31. // this.node.on('touchstart', onTouchDown, this.node);
  32. // this.node.on('touchend', onTouchUp, this.node);
  33. // this.node.on('touchcancel', onTouchUp, this.node);
  34. }
  35. Show(){
  36. this.node.getComponent(cc.Sprite).spriteFrame = this.pressed;
  37. let scaleDownAction = cc.scaleTo(this.transDuration, this.pressedScale);
  38. this.node.stopAllActions();
  39. this.node.runAction(scaleDownAction);
  40. for (let i = 0; i < this.nodes.length; i++) {
  41. let that = this.nodes[i];
  42. let button = that.getComponent(Button);
  43. if(button){
  44. button.Hide();
  45. }
  46. }
  47. }
  48. Hide(){
  49. this.node.getComponent(cc.Sprite).spriteFrame = this.normal;
  50. let scaleUpAction = cc.scaleTo(this.transDuration, this.initScale||this.node.scale);
  51. this.node.stopAllActions();
  52. this.node.runAction(scaleUpAction);
  53. }
  54. selected(){
  55. this.node.getComponent(cc.Sprite).spriteFrame = this.pressed;
  56. let scaleDownAction = cc.scaleTo(this.transDuration, this.pressedScale);
  57. this.node.stopAllActions();
  58. this.node.runAction(scaleDownAction);
  59. }
  60. unselected(){
  61. this.node.getComponent(cc.Sprite).spriteFrame = this.normal;
  62. let scaleUpAction = cc.scaleTo(this.transDuration, this.initScale||this.node.scale);
  63. this.node.stopAllActions();
  64. this.node.runAction(scaleUpAction);
  65. }
  66. }