【救救孩子】微信小游戏cc.sys.getSafeAreaRect()报错

  • Creator 版本: 2.4.2

  • 目标平台: 小游戏开发工具

  • 重现方式:调用cc.sys.getSafeAreaRect()

  • 首个报错:
    gameThirdScriptError Cannot read property 'top' of undefined;at setTimeout callback function TypeError: Cannot read property 'top' of undefined at Object.window.__globalAdapter.u.getSafeArea (http://127.0.0.1:38586/game/adapter-min.js:2088:17) at Object.e.getSafeAreaRect (http://127.0.0.1:38586/game/adapter-min.js:2054:21) at e.onUILoaded (http://127.0.0.1:38586/game/assets/main/index.js:91:24) at m (http://127.0.0.1:38586/game/src/assets/Script/Lib/fairygui.js:3982:147) at Function.<anonymous> (http://127.0.0.1:38586/game/cocos/cocos2d-js-min.js:1:807026) at http://127.0.0.1:38586/game/__dev__/WAGame.js:3:20858 at http://127.0.0.1:38586/game/__dev__/WAGame.js:14:13648

  • 重现概率: 100%

可以使用微信自己的api

哦哦,懂了懂了,多谢

3.7.1版,在使用过程中统计到了很多线上报错:
“message”:"MiniProgramError\nCannot read properties of undefined (reading ‘top’)\nTypeError: Cannot read properties of undefined (reading ‘top’)\n at e.get (https://usr/game.js:31:230026)\n at Object.getSafeAreaRect (https://usr/game.js:31:234091)\n

于是乎逛了一下论坛发现的确也有人遇到这个问题,但没有解释,遂自发查找了一下原因,发现是微信小游戏适配获取安全区区域时未做兼容处理


而微信文档却明确说明需要开发者自行兼容

暂时只能自己兼容了,望官方后续完善,最后祝愿cocos creator越做越好吧

其实很好处理,自己写个SafeArea的组件,然后自己做平台判断,如果是微信平台就调微信接口,如果不是就调cocos接口。
但最近因为项目原因从3.x切回2.x了,发现2.x的SafeArea组件是可以在微信小游戏上用的,目前项目刚上线,没发现啥适配问题