import { _decorator, Component, Node, director, assetManager, ProgressBar, Label, Sprite, SpriteFrame, resources } from 'cc'; const { ccclass, property } = _decorator; // 微信小游戏API类型声明 declare global { interface Window { wx?: { loadSubpackage?: (options: { name: string; success?: () => void; fail?: (err: any) => void; }) => any; }; } const wx: { loadSubpackage?: (options: { name: string; success?: () => void; fail?: (err: any) => void; }) => any; } | undefined; } @ccclass('LaunchScreen') export class LaunchScreen extends Component { @property(ProgressBar) progressBar: ProgressBar = null; @property(Label) progressLabel: Label = null; @property(Sprite) backgroundSprite: Sprite = null; @property(SpriteFrame) launchBackground: SpriteFrame = null; private totalAssets = 0; private loadedAssets = 0; start() { console.log('[LaunchScreen] 启动页开始初始化'); // 设置启动页背景 this.setupBackground(); // 开始预加载游戏资源 this.preloadGameAssets(); } /** * 设置启动页背景 */ private setupBackground() { if (this.backgroundSprite && this.launchBackground) { this.backgroundSprite.spriteFrame = this.launchBackground; console.log('[LaunchScreen] 启动页背景设置完成'); } } /** * 预加载游戏资源 */ private async preloadGameAssets() { try { console.log('[LaunchScreen] 开始预加载游戏资源'); // 更新进度显示 this.updateProgress(0, '正在加载游戏资源...'); // 直接预加载GameLevel场景(不使用分包) await this.preloadGameLevelScene(); // 预加载完成,跳转到游戏场景 this.loadGameScene(); } catch (error) { console.error('[LaunchScreen] 资源预加载失败:', error); this.updateProgress(0, '资源加载失败,请重试'); } } /** * 直接预加载GameLevel场景 */ private preloadGameLevelScene(): Promise { return new Promise((resolve, reject) => { console.log('[LaunchScreen] 开始预加载GameLevel场景'); // 直接预加载场景 director.preloadScene('GameLevel', (finished: number, total: number) => { const progress = finished / total; this.updateProgress(progress * 100, `正在加载游戏场景... ${Math.floor(progress * 100)}%`); }, (err) => { if (err) { console.error('[LaunchScreen] GameLevel场景预加载失败:', err); // 预加载失败时,仍然尝试直接跳转(可能场景存在但预加载失败) console.log('[LaunchScreen] 预加载失败,但仍将尝试直接加载场景'); this.updateProgress(100, '准备进入游戏...'); resolve(); return; } console.log('[LaunchScreen] GameLevel场景预加载完成'); this.updateProgress(100, '加载完成!'); resolve(); }); }); } /** * 更新加载进度 */ private updateProgress(progress: number, message: string) { if (this.progressBar) { this.progressBar.progress = progress / 100; } if (this.progressLabel) { this.progressLabel.string = message; } console.log(`[LaunchScreen] ${message} (${progress.toFixed(1)}%)`); } /** * 跳转到游戏场景 */ private loadGameScene() { console.log('[LaunchScreen] 准备跳转到GameLevel场景'); // 延迟一秒后跳转,让用户看到加载完成的提示 this.scheduleOnce(() => { // 直接加载GameLevel场景 director.loadScene('GameLevel', (err) => { if (err) { console.error('[LaunchScreen] GameLevel场景加载失败:', err); console.log('[LaunchScreen] 尝试降级方案:停留在启动页面'); this.updateProgress(0, 'GameLevel场景不存在,请检查场景配置'); // 提供重试按钮或其他交互方式 this.scheduleOnce(() => { this.updateProgress(0, '点击屏幕重试或检查场景配置'); }, 2.0); return; } console.log('[LaunchScreen] 成功切换到GameLevel场景'); }); }, 1.0); } }