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

目录

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

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


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

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

相关推荐
xiezhr3 天前
米哈游36岁程序员被曝复工当晚猝死出租屋内
游戏·程序员·游戏开发
爱搞虚幻的阿恺7 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
智算菩萨7 天前
儿童游乐空间的双维建构:室内淘气堡与室外亲子乐园的发展学理、功能分野与协同育人机制研究
游戏·游戏策划
marteker7 天前
房地产市场平台Zillow与《魔兽世界》合作展示游戏内房屋
游戏
wanhengidc7 天前
云手机 打造云端算力
运维·服务器·网络·游戏·智能手机
henry1010107 天前
DeepSeek生成的HTML5小游戏 -- 黑8台球
前端·javascript·css·游戏·html
yuanmenghao8 天前
从零开始:使用 Claude Code 打造字母消除游戏
游戏·glm·claudecode
阿甘编程点滴8 天前
2026年推荐以下5款游戏直播提词器
游戏
PieroPc8 天前
HTML5 Canvas 平台跳跃游戏
前端·游戏·html5
Swift社区8 天前
LeetCode 390 消除游戏 - Swift 题解
leetcode·游戏·swift