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

目录

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

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


代码仓地址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年的缓存时间

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

相关推荐
I Am a robert girl2 小时前
从 GameHorizon 拆解多时间尺度游戏评测:数据管线、离线基准与在线回放的三层设计
数据库·游戏·多时间尺度·数据管线·游戏评测·离线基准·在线回放
大囚长3 小时前
AI意图预测与游戏全球同服对战的超光速零延迟探究
人工智能·游戏
qq36219670510 小时前
OBB 数据包是什么?大型游戏 APK 装完闪退的元凶(2026)
游戏
xiaolang_8616_wjl12 小时前
c++游戏_宝可梦大世界(源代码)
开发语言·c++·游戏
凯基迪科技1 天前
Unity游戏加密锁逆向方案、选型与攻防分析
游戏·unity·游戏引擎
Solange__1 天前
航天科普怎么用AI生成3D场景?从科学约束、尺度分层到网页展示的完整方案
图像处理·人工智能·游戏·3d·ai·自然语言处理·关卡设计
C MIKE2 天前
小熊杂货铺小游戏开发
游戏
装备研究社2 天前
对峙2掉线排查实测
游戏
xcLeigh2 天前
Unity基础:Destroy与DestroyImmediate——正确销毁游戏物体的方式
游戏·unity·游戏引擎·教程
sunxfancy2 天前
EVEngine 0.6 到 0.6.1:让 AI 参与游戏开发,还需要补上哪些环节?
游戏