SelectScene.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { _decorator, Component, Node ,Button,director, PageView} from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('SelectScene')
  4. export class SelectScene extends Component {
  5. @property(Node)
  6. pageView:Node = null;
  7. currentPageIndex:number = null;
  8. start() {
  9. this.currentPageIndex = this.pageView.getComponent(PageView).getCurrentPageIndex();
  10. console.log(this.currentPageIndex);
  11. let BtnHome = this.node.getChildByPath("UIButton/BtnHome");
  12. BtnHome.on(Button.EventType.CLICK,this.onBtnHome,this);
  13. let BtnHelp = this.node.getChildByPath("UIButton/BtnHelp");
  14. BtnHelp.on(Button.EventType.CLICK,this.onBtnHelp,this);
  15. let BtnLeft = this.node.getChildByPath("UIButton/BtnLeft");
  16. BtnLeft.on(Button.EventType.CLICK,this.onBtnLeft,this);
  17. let BtnRight = this.node.getChildByPath("UIButton/BtnRight");
  18. BtnRight.on(Button.EventType.CLICK,this.onBtnRight,this);
  19. }
  20. onBtnHome(){
  21. //console.log("LoginScene");
  22. director.loadScene("LoginScene")
  23. }
  24. onBtnHelp(){
  25. console.log("HelpScene");
  26. //director.loadScene("");
  27. }
  28. onBtnLeft(){
  29. // if(this.currentPageIndex < 0){
  30. // this.currentPageIndex = 0;
  31. // }
  32. // else{
  33. // this.currentPageIndex = this.currentPageIndex - 1;
  34. // }
  35. this.currentPageIndex = Math.max(0, this.currentPageIndex - 1);
  36. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  37. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  38. }
  39. onBtnRight(){
  40. // if(this.currentPageIndex == this.pageView.getComponent(PageView).getPages().length + 1){
  41. // this.currentPageIndex = this.pageView.getComponent(PageView).getPages().length;
  42. // }else{
  43. // this.currentPageIndex = this.currentPageIndex + 1;
  44. // }
  45. //总页数totalPages 最大索引totalPages - 1
  46. const totalPages = this.pageView.getComponent(PageView).getPages().length;
  47. // 确保 currentPageIndex 不会超过 totalPages - 1
  48. if (this.currentPageIndex === totalPages - 1) {
  49. this.currentPageIndex = totalPages - 1; // 保持在最后一页
  50. } else {
  51. this.currentPageIndex = Math.min(this.currentPageIndex + 1, totalPages -1);
  52. }
  53. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  54. }
  55. update(deltaTime: number) {
  56. }
  57. }