NavBarController.ts 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { _decorator, Color, Component, Node, Sprite } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('NavBarController')
  4. export class NavBarController extends Component {
  5. @property({ type: [Node] }) panels: Node[] = []; // 依次给 Main、Shop、Upgrade、Skill
  6. @property({ type: [Node] }) buttons: Node[] = []; // 依次给 Battle、Shop、Upgrade、Skill
  7. private normalColor = new Color(255, 255, 255, 255);
  8. private activeColor = new Color(255, 0, 0, 255); // 红色
  9. start () {
  10. // 默认打开 MainUI
  11. this.switchTo(0);
  12. }
  13. onBattleClick () { this.switchTo(0); }
  14. onShopClick () { this.switchTo(1); }
  15. onUpgradeClick () { this.switchTo(2); }
  16. onSkillClick () { this.switchTo(3); }
  17. private switchTo (index: number) {
  18. // 显示对应面板
  19. this.panels.forEach((p, i) => p.active = i === index);
  20. // 设置按钮颜色
  21. this.buttons.forEach((btn, i) => {
  22. const sp = btn.getComponent(Sprite);
  23. if (sp) sp.color = (i === index) ? this.activeColor : this.normalColor;
  24. });
  25. }
  26. }