笔记十一 :Egret动画式加载场景(基于通用MVC框架)

笔记十一 :Egret动画式加载场景(基于通用MVC框架)

为了确保你能够顺利完成整个过程,以下是一些关键点和步骤的详细说明:

步骤一:理解需求

首先,明确你的需求是实现一个在加载新场景后,旧场景3秒后自动消失的功能,并且在此过程中播放一个动画。

步骤二:设置项目结构

确保你的项目结构清晰,包含以下主要文件和目录:

  • src:源代码目录
    • module:模块目录
      • loading:加载模块
        • LoadingController.ts
        • LoadingView.ts
        • LoadingUISkin.exml
    • scene:场景目录
      • RpgGameScene.ts
    • utils:工具类目录(可选)
      • ResourceUtils.ts

步骤三:实现加载功能

  1. ResourceUtils.ts

    import { Resource } from "egret"; export class ResourceUtils { public static loadAssets(callback: Function): void { let assetAdapter = new egret.DefaultAssetAdapter(); egret.registerImplementation("eui.IAssetAdapter", assetAdapter); RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this); RES.loadConfig("resource/default.res.json", "resource/"); } private static onResourceLoadComplete(event: RES.ResourceEvent): void { if (event.groupName == "preload") { callback(); } } } 
  2. App.ts

    import { ResourceUtils } from "./utils/ResourceUtils"; export class App extends egret.DisplayObjectContainer { public constructor() { super(); this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this); } private onAddToStage(event: egret.Event): void { ResourceUtils.loadAssets(() => { this.startGame(); }); } private startGame(): void { let loadingController = new module.loading.LoadingController(this.stage); this.addChild(loadingController.view); setTimeout(() => { loadingController.closeLoading(); // 加载新场景 this.removeChild(loadingController.view); let rpgGameScene = new scene.RpgGameScene(); this.addChild(rpgGameScene); }, 3000); } } 

步骤四:实现动画播放

  1. LoadingView.ts

    import { eui } from "egret"; import { egret } from "egret"; export class LoadingView extends eui.Component { public zuoRect: eui.Rect; public youRect: eui.Rect; public txtMsg: eui.Label; private flag: boolean = true; constructor(controller: module.loading.LoadingController, layer: BaseEuiLayer) { super(); this.layer = layer; this.controller = controller; this.initUI(); } private initUI(): void { this.zuoRect = new eui.Rect(694.67, 1065.34, 0xf4f4f4); this.youRect = new eui.Rect(659.67, 1052, 0xffffff); this.txtMsg = new eui.Label("资源加载中...", { size: 40, textColor: 0x000000, bold: true }); this.addChild(this.zuoRect); this.addChild(this.youRect); this.addChild(this.txtMsg); this.addEventListener(eui.UIEvent.CHANGE, this.onProgressChange, this); } public setProgress(current: number, total: number): void { this.txtMsg.text = "资源加载中..." + current + "/" + total; if (current == total) { this.playDoneAnimation(); } } private playDoneAnimation(): void { if (this.flag) { this.flag = false; let self = this; let temp1x = self.zuoRect.x; let Tween1 = egret.Tween.get(self.zuoRect).to({ x: temp1x - 600, alpha: 1.0 }, 2000); let temp2x = self.youRect.x; let Tween2 = egret.Tween.get(self.youRect).to({ x: temp2x + 600, alpha: 1.0 }, 2000); let Tween3 = egret.Tween.get(self.txtMsg).to({ alpha: 0 }, 1400); } } private onProgressChange(event: eui.UIEvent): void { this.setProgress(this.controller.progress, this.controller.total); } } 
  2. LoadingController.ts

    import { eui } from "egret"; import { egret } from "egret"; export class LoadingController { public progress: number = 0; public total: number = 10; // 假设有10个资源需要加载 constructor(public stage: egret.Stage) {} public view: module.loading.LoadingView; public openLoading(): void { this.view = new module.loading.LoadingView(this, this.stage); this.stage.addChild(this.view); } public closeLoading(): void { if (this.view) { this.stage.removeChild(this.view); this.view = null; } } } 

步骤五:测试功能

  1. 运行项目
    • 确保所有资源文件(如 default.res.json 和其他图片、音效等)都放在正确的目录中。
    • 运行项目,观察加载过程和动画播放效果。

通过以上步骤,你应该能够实现一个在加载新场景后,旧场景3秒后自动消失并播放动画的功能。如果在过程中遇到任何问题,请检查代码逻辑和资源配置是否正确。

Read more

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.