feat: 支持失败弹窗

This commit is contained in:
richarjiang
2025-09-29 15:39:27 +08:00
parent facdae5c5e
commit dec7ce62ee
6 changed files with 1265 additions and 587 deletions

View File

@@ -1,4 +1,4 @@
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween, Animation, Collider2D, Contact2DType, Label, Color, Canvas, UITransform, AudioSource, Sprite } from 'cc';
import { _decorator, Component, Node, Vec3, input, Input, EventTouch, Camera, view, tween, Animation, Collider2D, Contact2DType, Label, Color, Canvas, UITransform, AudioSource, Sprite, director } from 'cc';
import { TiledMapPathfinder } from './TiledMapPathfinder';
const { ccclass, property } = _decorator;
@@ -16,6 +16,9 @@ export class PlayerController extends Component {
@property(Node)
bonusWuqi: Node | null = null;
@property(Node)
failedDialog: Node | null = null;
@property(Camera)
camera: Camera | null = null; // 主摄像机
@@ -515,6 +518,11 @@ export class PlayerController extends Component {
// 设置游戏结束标志,禁止后续寻路
this.isGameOver = true;
console.log('游戏结束,禁止寻路');
// 显示失败弹窗
this.scheduleOnce(() => {
this.showFailedDialog();
}, 1); // 延迟1秒显示失败弹窗让玩家死亡动画播放完成
}
this.isAttacking = false;
@@ -803,4 +811,55 @@ export class PlayerController extends Component {
this.pendingPopupHide = null;
}
}
/**
* 显示失败弹窗
*/
public showFailedDialog() {
this.showPopupAtCameraCenter(this.failedDialog, '失败弹窗');
this.setupRetryButtonListener();
}
/**
* 设置重试按钮监听器
*/
private setupRetryButtonListener() {
if (!this.failedDialog) {
console.warn('失败弹窗节点未设置,无法监听重试按钮');
return;
}
// 查找Retry按钮节点
const retryButton = this.failedDialog.getChildByName('Retry');
if (!retryButton) {
console.warn('未找到Retry按钮节点');
return;
}
// 移除之前的监听器(如果存在)
retryButton.off(Node.EventType.TOUCH_END, this.onRetryButtonClick, this);
// 添加新的监听器
retryButton.on(Node.EventType.TOUCH_END, this.onRetryButtonClick, this);
console.log('已设置重试按钮监听器');
}
/**
* 重试按钮点击事件处理
*/
private onRetryButtonClick() {
console.log('重试按钮被点击,重新加载当前场景');
// 隐藏失败弹窗
if (this.failedDialog) {
this.hidePopupWithAnimation(this.failedDialog, '失败弹窗');
}
// 重新加载当前场景
this.scheduleOnce(() => {
// 使用Cocos Creator的场景管理器重新加载当前场景
const sceneName = director.getScene().name;
director.loadScene(sceneName);
}, 0.3); // 延迟0.3秒,让弹窗消失动画完成
}
}