关于截屏时实现游戏暂停以及本地和上线不同步问题

目录

需求:在点击截屏时需要自动暂停游戏运行,并在关闭弹窗后游戏自动恢复

问题:本地测试通过但是部署上线报错


代码仓地址https://github.com/ceilf6/Xbuilder

需求:在点击截屏时需要自动暂停游戏运行,并在关闭弹窗后游戏自动恢复

需要从游戏引擎角度实现,否则游戏都不会完全停止

TypeScript 复制代码
// 在 spx-gui/public/spx_2.0.0-beta9/runner.html 中添加
window.pauseGame = async () => {
  // 使用时间缩放来暂停游戏
  if (typeof window.gdspx_platform_set_time_scale === 'function') {
    window.gdspx_platform_set_time_scale(0.0);  // 时间缩放为0,游戏完全停止
    console.log("游戏已通过时间缩放暂停 (time_scale = 0.0)")
  }
}

window.resumeGame = async () => {
  // 使用时间缩放来恢复游戏
  if (typeof window.gdspx_platform_set_time_scale === 'function') {
    window.gdspx_platform_set_time_scale(1.0);  // 时间缩放为1,游戏正常速度
    console.log("游戏已通过时间缩放恢复 (time_scale = 1.0)")
  }
}

接着通过在引擎组件中通过 defineExpose 来暴露方法

TypeScript 复制代码
defineExpose({
  async pauseGame() {
    const iframeWindow = iframeWindowRef.value
    await iframeWindow.pauseGame()  // 调用iframe中的方法
  },
  async resumeGame() {
    const iframeWindow = iframeWindowRef.value
    await iframeWindow.resumeGame()
  }
})

(且由于 XBuilder 上有两个引擎,需要在两个组件中构建完方法后还得去顶层组件中统一暴露)

然后就是去截屏组件中应用

TypeScript 复制代码
const handleScreenshot = async () => {
  // 1. 暂停游戏
  await projectRunner.pauseGame()
  
  // 2. 截图
  const screenshot = await projectRunner.takeScreenshot()
  
  // 3. 显示截图弹窗
  isScreenshotModalVisible.value = true
}

// 关闭截图弹窗时恢复游戏
const handleCloseScreenshotModal = async () => {
  await projectRunner.resumeGame()
}

录屏加上了观察者模式,因为录屏弹窗的调用是截屏的两倍

TypeScript 复制代码
// 弹窗打开时暂停游戏
watch(() => props.visible, async (newVisible) => {
  if (newVisible) {
    await props.projectRunner.pauseGame()
  }
})

// 开始录屏时恢复游戏
const startGameCanvasRecording = async () => {
  await props.projectRunner.resumeGame()  // 让录屏能录制到游戏画面
}

// 停止录屏时暂停游戏
const handleStopRecording = async () => {
  await props.projectRunner.pauseGame()
}

// 关闭弹窗时恢复游戏
const handleModalClose = async () => {
  await props.projectRunner.resumeGame()
}

问题:本地测试通过但是部署上线报错

通过 vercel 部署上去后报错 找不到新定义的方法

于是我去 vercel 上看了 public 中的 runner.html 文件,发现未和本地同步,于是我去看了 runner 的更新逻辑,发现 install-spx.sh 脚本在构建时会从 GitHub 下载 spx_web.zip 并解压,覆盖我们修改的 runner.html 且 Vite 配置中 spx_* 文件被设置了1年的缓存时间

于是我在脚本文件中重新应用更改

相关推荐
wind1003210 小时前
DLSS5 开启保姆级教程:RTX20/30/40 显卡通用 鸣潮 / 异环 / 绝区零全适配
游戏·电脑·英伟达·dlss·鸣潮
全速向光15 小时前
JoiPlay 模拟器:在手机上畅玩你所热爱的独立游戏
游戏·智能手机
yangmu32031 天前
《黎明行者之血》MOD整合包保姆级安装与使用指南
游戏·游戏程序
Jgch222 天前
出海Facebook广告账户怎么选?
游戏·facebook
长脖鹿Johnny2 天前
游戏输入系统框架设计(三):网络输入权威与可验证性
网络·游戏·游戏开发·架构设计·输入系统·网络同步
谈资一本本2 天前
电脑全能修复大师 Gilisoft Total Repair,智能检测修复系统、应用、游戏问题!DLL、DirectX、.NET运行库损坏修复+缺失自动补齐!
游戏·电脑·.net
程序员-Benothing2 天前
H3 Max开放世界RPG:AI原生游戏,从“能生成“到“能玩“差了多远
游戏·ai-native
CoderYanger3 天前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
无别0523 天前
【做游戏】做完游戏Demo后没地方发布?
游戏·玩游戏
lilian2333 天前
HarmonyOS 7 新特性(二十九)|游戏伴随服务:悬浮协作与质量降级
游戏·语音识别·harmonyos