SelectScene.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { _decorator, Component, Node ,Button,director, PageView, NodeEventType} 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. let BtnHome = this.node.getChildByPath("UIButton/BtnHome");
  11. BtnHome.on(Button.EventType.CLICK,this.onBtnHome,this);
  12. let BtnHelp = this.node.getChildByPath("UIButton/BtnHelp");
  13. BtnHelp.on(Button.EventType.CLICK,this.onBtnHelp,this);
  14. let BtnLeft = this.node.getChildByPath("UIButton/BtnLeft");
  15. BtnLeft.on(Button.EventType.CLICK,this.onBtnLeft,this);
  16. let BtnRight = this.node.getChildByPath("UIButton/BtnRight");
  17. BtnRight.on(Button.EventType.CLICK,this.onBtnRight,this);
  18. let touchArea = this.node.getChildByPath("TouchArea");
  19. touchArea.on(NodeEventType.TOUCH_START,this.onTouchStart,this);
  20. touchArea.on(NodeEventType.TOUCH_END,this.onTouchEnd,this);
  21. }
  22. onBtnHome(){
  23. //console.log("LoginScene");
  24. director.loadScene("LoginScene")
  25. }
  26. onBtnHelp(){
  27. console.log("HelpScene");
  28. //director.loadScene("");
  29. }
  30. onBtnLeft(){
  31. // if(this.currentPageIndex < 0){
  32. // this.currentPageIndex = 0;
  33. // }
  34. // else{
  35. // this.currentPageIndex = this.currentPageIndex - 1;
  36. // }
  37. this.currentPageIndex = Math.max(0, this.currentPageIndex - 1);
  38. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  39. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  40. }
  41. onBtnRight(){
  42. // if(this.currentPageIndex == this.pageView.getComponent(PageView).getPages().length + 1){
  43. // this.currentPageIndex = this.pageView.getComponent(PageView).getPages().length;
  44. // }else{
  45. // this.currentPageIndex = this.currentPageIndex + 1;
  46. // }
  47. //总页数totalPages 最大索引totalPages - 1
  48. const totalPages = this.pageView.getComponent(PageView).getPages().length;
  49. // 确保 currentPageIndex 不会超过 totalPages - 1
  50. if (this.currentPageIndex === totalPages - 1) {
  51. this.currentPageIndex = totalPages - 1; // 保持在最后一页
  52. } else {
  53. this.currentPageIndex = Math.min(this.currentPageIndex + 1, totalPages -1);
  54. }
  55. this.pageView.getComponent(PageView).scrollToPage(this.currentPageIndex);
  56. }
  57. onTouchStart(){
  58. //console.log("TouchStart");
  59. }
  60. onTouchEnd(){
  61. director.loadScene("SelectLevel");
  62. //console.log("TouchEnd");
  63. }
  64. update(deltaTime: number) {
  65. }
  66. }