import { _decorator, Component, Node, tween, UITransform, Vec3 } from "cc"; import { EventDispatcher } from "../libs/EventDispatcher"; import { GlobalData } from "../config/GlobalData"; import { GameState } from "../config/Config"; const { ccclass, property } = _decorator; @ccclass("ContonueModal") export class ContonueModal extends Component { // 滑动区域的节点 @property(Node) private swipeNode: Node = null; // 提示 1 节点 @property(Node) private tipNode1: Node = null; // 提示 2 节点 @property(Node) private tipNode2: Node = null; start() { // 隐藏继续游戏弹窗 this.node.active = false; // 初始化位置 this.node.setPosition(0, 0, 0); // 监听继续游戏事件 EventDispatcher.getTarget().on( EventDispatcher.SHOW_CONTINUE_MODAL, this.showModal, this, ); // 监听滑动事件 this.swipeNode.on(Node.EventType.TOUCH_START, this.onSwipeStart, this); } // 显示继续游戏弹窗 showModal() { // 游戏暂停 GlobalData.gameState = GameState.PAUSED; // 显示继续游戏弹窗 this.node.active = true; // 操作提示动画 if (GlobalData.lifeNumPerRound > 0) { // 执行提示1的动画 this.playTipAnimation(this.tipNode1); } else { // 执行提示2的动画 this.playTipAnimation(this.tipNode2); } } // 关闭继续游戏弹窗 closeModal() { // 关闭继续游戏弹窗 this.node.active = false; } // 滑动开始事件 onSwipeStart() { // 关闭继续游戏弹窗 this.closeModal(); // 等 2 s之后恢复游戏 this.scheduleOnce(() => { GlobalData.gameState = GameState.PLAYING; // 发送继续游戏事件 EventDispatcher.getTarget().emit(EventDispatcher.GAME_RESUME); }, 2); } // 暂停游戏 onPauseGame() { console.log(111, "暂停游戏"); // 发送暂停事件 EventDispatcher.getTarget().emit(EventDispatcher.SHOW_PAUSE_MODAL); } // 播放提示1的动画 playTipAnimation(tNode: Node) { // 显示提示1 tNode.active = true; // 获取提示节点的位置 const oriPos = tNode.getPosition().clone(); // 获取提示的宽度 const tipWidth = tNode.getComponent(UITransform).width + 5; // 执行动画 tween(tNode) .to(0.3, { position: new Vec3(oriPos.x - tipWidth, oriPos.y, 0), }) .delay(0.8) .to(0.3, { position: oriPos, }) .call(() => { // 隐藏提示1 tNode.active = false; // 重置节点位置 tNode.setPosition(oriPos); }) .start(); } update(deltaTime: number) {} }