Launch.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. cc.Class({
  2. extends: cc.BaseClass,
  3. properties: {
  4. m_updateTipNode: cc.Node,
  5. },
  6. ctor: function () {
  7. this.m_nNeedUpdate = 0;
  8. this.m_updateURL = null;
  9. },
  10. // onLoad: function () {
  11. // cc.debug.setDisplayStats(false);
  12. // FitSize(this.node);
  13. // ShowO2I(this.node, 0.5);
  14. // this.$('Version@Label').string = ``;
  15. // if(!this.m_Loading) this.m_Loading = this.$('loading');
  16. // this.m_Loading.zIndex = 100;
  17. // this.m_Loading.active = false;
  18. // this.$('ani').active = false;
  19. // this.$('Logo').active = false;
  20. // window.LoadSetting();
  21. // this.m_StartAni = this.$('StartAni').getComponent(dragonBones.ArmatureDisplay);
  22. // if(window.START_ANIMATION == 0){
  23. // this.m_nNeedUpdate = 1;
  24. // this.$('ani').active = true;
  25. // this.scheduleOnce(this.OnTimer_DelayShowLogo, 0.4);
  26. // this.m_StartAni.node.active = false;
  27. // }else{
  28. // this.m_StartAni.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this);
  29. // this.m_StartAni.node.active = true;
  30. // this.m_StartAni.playAnimation('newAnimation', 1);
  31. // }
  32. // },
  33. onLoad: function () {
  34. // 关闭调试统计信息显示(如FPS、DrawCall等)
  35. cc.debug.setDisplayStats(false);
  36. // 调整当前节点尺寸以适应屏幕大小(假设为自定义函数,用于屏幕适配)
  37. FitSize(this.node);
  38. // 显示从Out到In的过渡效果,持续0.5秒(假设为自定义动画效果函数)
  39. ShowO2I(this.node, 0.5);
  40. // 初始化版本号标签并置空(通过自定义选择器获取Label组件)
  41. if (cc.sys.OS_ANDROID == cc.sys.os) {
  42. this.$('Version@Label').string = window.APP_BOTTOM_VER_ANDROID;
  43. }else{
  44. this.$('Version@Label').string = window.APP_BOTTOM_VER_IOS;
  45. }
  46. this.$('VersionUI@Label').string = ""//window.APP_VERSION_UI;
  47. // 初始化加载界面节点
  48. if (!this.m_Loading)
  49. this.m_Loading = this.$('loading'); // 获取loading子节点
  50. this.m_Loading.zIndex = 100; // 设置渲染层级
  51. this.m_Loading.active = false; // 默认隐藏加载界面
  52. // 禁用动画节点和Logo节点
  53. this.$('ani').active = false;
  54. this.$('Logo').active = false;
  55. // 加载游戏设置(假设为全局函数,读取本地存储配置等)
  56. window.LoadSetting();
  57. // 获取龙骨动画组件(开场动画)
  58. // this.m_StartAni = this.$('StartAni').getComponent(dragonBones.ArmatureDisplay);
  59. // 根据启动动画设置执行不同逻辑
  60. if (window.START_ANIMATION == 0) {
  61. // 跳过开场动画的情况
  62. this.m_nNeedUpdate = 1; // 标记需要更新状态(具体用途需结合上下文)
  63. // this.$('ani').active = true; // 启用备用动画节点
  64. // 延迟0.4秒执行Logo显示(等待备用动画播放)
  65. this.scheduleOnce(this.OnTimer_DelayShowLogo, 0.4);
  66. // this.m_StartAni.node.active = false; // 隐藏龙骨动画节点
  67. } else {
  68. // // 需要播放开场动画的情况
  69. // // 添加动画播放完成事件监听
  70. // this.m_StartAni.addEventListener(
  71. // dragonBones.EventObject.COMPLETE,
  72. // this.animationEventHandler, // 动画结束回调
  73. // this // 回调上下文
  74. // );
  75. // this.m_StartAni.node.active = true; // 启用龙骨动画节点
  76. // this.m_StartAni.playAnimation('newAnimation', 1); // 播放指定动画(1次)
  77. }
  78. if (this.m_updateTipNode) {
  79. this.m_updateTipNode.active = false;
  80. }
  81. this._getServerIP();
  82. },
  83. /* 代码逻辑说明:
  84. 1. 初始化阶段:
  85. - 关闭调试显示
  86. - 屏幕适配处理
  87. - 显示过渡效果
  88. - 初始化界面元素状态
  89. 2. 动画流程控制:
  90. - 通过全局变量 START_ANIMATION 控制是否播放完整开场动画
  91. - 值为0时:
  92. * 跳过龙骨动画
  93. * 启用备用动画节点
  94. * 延迟显示Logo
  95. - 其他值时:
  96. * 播放龙骨开场动画
  97. * 动画结束后通过 animationEventHandler 处理后续逻辑
  98. 3. 注意事项:
  99. - $() 可能是自定义节点选择器方法
  100. - FitSize/ShowO2I 应为项目内封装的工具函数
  101. - START_ANIMATION 可能表示首次启动/重复启动状态
  102. - m_nNeedUpdate 可能用于控制后续更新逻辑
  103. */
  104. // start: function () {
  105. // g_Launch = this;
  106. // g_Login = null;
  107. // g_Lobby = null;
  108. // g_Table = null;
  109. // g_CurScene = this;
  110. // },
  111. // animationEventHandler: function (event) {
  112. // if (event.type === dragonBones.EventObject.COMPLETE) {
  113. // this.m_StartAni.node.active = false;
  114. // this.m_nNeedUpdate = 1;
  115. // this.OnTimer_DelayShowLogo();
  116. // //this.scheduleOnce(this.OnTimer_DelayShowLogo, 0.4);
  117. // }
  118. // },
  119. // OnTimer_DelayShowLogo: function() {
  120. // this.$('ani').active = true;
  121. // this.$('Logo').active = true;
  122. // },
  123. // onEnable: function() {
  124. // cc.director.on('LocalVersion',this.OnLocalVersion, this);
  125. // },
  126. // OnLocalVersion: function(ver) {
  127. // if(!ver) return;
  128. // window.APP_VERSION = ver;
  129. // this.$('Version@Label').string = `v${ver}`;
  130. // },
  131. // onDisable: function() {
  132. // cc.director.off('LocalVersion',this.OnLocalVersion, this);
  133. // },
  134. start: function () {
  135. // 初始化全局场景引用
  136. g_Launch = this; // 记录启动场景实例
  137. g_Login = null; // 清空登录场景引用
  138. g_Lobby = null; // 清空大厅场景引用
  139. g_Table = null; // 清空游戏桌引用
  140. g_CurScene = this; // 设置当前活动场景为启动场景
  141. },
  142. /* 动画事件处理回调 */
  143. // animationEventHandler: function (event) {
  144. // // 当龙骨动画播放完成时
  145. // if (event.type === dragonBones.EventObject.COMPLETE) {
  146. // // this.m_StartAni.node.active = false; // 隐藏开场动画节点
  147. // this.m_nNeedUpdate = 1; // 触发状态更新标志
  148. // this.OnTimer_DelayShowLogo(); // 立即显示Logo
  149. // // 原始延迟调用方案(已注释):
  150. // // this.scheduleOnce(this.OnTimer_DelayShowLogo, 0.4);
  151. // }
  152. // },
  153. /* 延迟显示Logo的定时回调 */
  154. OnTimer_DelayShowLogo: function () {
  155. this.$('ani').active = true; // 启用过渡动画节点
  156. this.$('Logo').active = true; // 显示主Logo节点
  157. },
  158. /* 组件启用时的生命周期回调 */
  159. onEnable: function () {
  160. // 注册本地版本号事件监听
  161. cc.director.on('LocalVersion', this.OnLocalVersion, this);
  162. },
  163. /* 处理本地版本号事件 */
  164. OnLocalVersion: function (ver) {
  165. if (!ver) return; // 无效版本号直接返回
  166. // 更新全局版本号并显示在UI
  167. window.APP_VERSION = ver; // 存储到全局变量
  168. this.$('Version@Label').string = `v${ver}`; // 更新版本号标签
  169. console.log("OnLocalVersion----1", ver)
  170. },
  171. /* 组件禁用时的生命周期回调 */
  172. onDisable: function () {
  173. // 移除事件监听防止内存泄漏
  174. cc.director.off('LocalVersion', this.OnLocalVersion, this);
  175. },
  176. //获取服务器最新IP
  177. _getServerIP() {
  178. if(window.GAME_TEST)return;
  179. this.updateIP = true;
  180. var xhr = new XMLHttpRequest();
  181. xhr.onreadystatechange = function () {
  182. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
  183. console.log("a1.king-mahjong.tw 返回",xhr.responseText);
  184. this.updateIP = false;
  185. window.UpdateIPRelatedAddresses(xhr.responseText);
  186. }
  187. }.bind(this);
  188. xhr.timeout = 5000;
  189. xhr.open("GET", encodeURI('https://a1.king-mahjong.tw'), true);
  190. xhr.send();
  191. },
  192. /* 代码逻辑说明:
  193. 1. 场景管理:
  194. - start() 初始化全局场景引用体系
  195. - 通过重置 g_Login/g_Lobby 等引用确保场景切换的干净状态
  196. - g_CurScene 实现当前场景的集中访问点
  197. 2. 动画流程控制:
  198. - animationEventHandler 处理开场动画结束事件
  199. - 完成时切换至Logo显示流程
  200. - 原设计使用延迟调用,现改为立即执行(需确认动画衔接是否合理)
  201. 3. 版本号管理:
  202. - 通过事件机制获取本地版本号
  203. - LocalVersion 事件可能由以下场景触发:
  204. * 本地存储读取完成
  205. * 网络请求获取最新版本
  206. * 热更新流程结束
  207. - 版本号显示使用ES6模板字符串格式化
  208. 4. 事件管理:
  209. - onEnable/onDisable 组成对称的事件监听生命周期
  210. - 防止组件销毁后残留监听导致报错
  211. 5. 注意事项:
  212. - 全局变量(g_XX)的使用需确保单场景切换时的正确清理
  213. - m_nNeedUpdate 的具体作用需结合更新逻辑分析
  214. - 直接调用 OnTimer_DelayShowLogo() 需确认不需要等待动画残留资源释放
  215. - 版本号标签更新依赖选择器正确指向目标节点
  216. */
  217. // update: function () {
  218. // if (this.m_nNeedUpdate > 0) {
  219. // this.m_nNeedUpdate--;
  220. // } else {
  221. // return;
  222. // }
  223. // this.LoadConfig();
  224. // cc.gPreLoader.Init(function () {
  225. // if (cc.sys.isNative) {
  226. // this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  227. // Js.CheckUpdate(function() { this.ShowLogin(); }.bind(this));
  228. // }.bind(this));
  229. // } else {
  230. // if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT || cc.sys.browserType == cc.sys.BROWSER_TYPE_MOBILE_QQ) {
  231. // ChangeScene('Lobby');
  232. // } else {
  233. // this.ShowLogin();
  234. // }
  235. // }
  236. // }.bind(this));
  237. // },
  238. update: function () {
  239. //获取最新IP
  240. if (this.updateIP) {
  241. return;
  242. }
  243. // 更新逻辑控制:仅在 m_nNeedUpdate 有效时执行
  244. if (this.m_nNeedUpdate > 0) {
  245. this.m_nNeedUpdate--; // 递减更新计数器
  246. } else {
  247. return; // 未达到更新条件时直接退出
  248. }
  249. // 加载基础配置文件(假设为游戏核心配置)
  250. this.LoadConfig();
  251. // cc.assetManager.loadBundle("21201", "", function (err, bundle) {
  252. // if (err) {
  253. // console.log('加载bundle错误---', "21201");
  254. // let bundle21201 = cc.assetManager.getBundle('21201');
  255. // console.log('加载bundle---21201', bundle21201);
  256. // return console.error(err);
  257. // }
  258. // console.log('加载bundle成功---21201');
  259. // //console.log('1-----------------',JSON.stringify(bundle));
  260. // console.log('2-----------------', JSON.stringify(bundle._config.paths._map));
  261. // for (let i in bundle._config.paths._map) {
  262. // let asset = bundle._config.paths._map[i];
  263. // console.log('3-----------------', asset);
  264. // for (let j = 0; j < asset.length; j++) {
  265. // console.log('4-----------------', JSON.stringify(asset[j]));
  266. // console.log('41-----------------', JSON.stringify(asset[j].ctor));
  267. // console.log('5-----------------', (asset[j].ctor));
  268. // console.log('6-----------------', (asset[j].ctor.toString()));
  269. // for (let key in asset[j]) {
  270. // if (typeof asset[j][key] === 'function') {
  271. // console.log(key + '()');
  272. // } else {
  273. // console.log(key + ': ' + asset[j][key]);
  274. // }
  275. // }
  276. // return;
  277. // }
  278. // }
  279. // console.dir(bundle, { depth: null });
  280. // }.bind(this));
  281. // return;
  282. // 初始化预加载系统
  283. cc.gPreLoader.Init(function (url) {
  284. if (url) {
  285. this.m_updateURL = url;
  286. if (this.m_updateTipNode) {
  287. this.m_updateTipNode.active = true;
  288. }
  289. } else {
  290. this.ShowLogin();
  291. }
  292. return;
  293. // 平台判断分支
  294. if (cc.sys.isNative) { // 原生平台(iOS/Android)&& window.IS_UPDATE
  295. // 显示更新管理弹窗
  296. this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  297. // 执行更新检查
  298. Js.CheckUpdate(function () {
  299. this.ShowLogin(); // 更新完成后显示登录界面
  300. }.bind(this));
  301. }.bind(this));
  302. } else { // Web 平台
  303. // 判断是否在微信/QQ内置浏览器
  304. // if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT ||
  305. // cc.sys.browserType == cc.sys.BROWSER_TYPE_MOBILE_QQ) {
  306. // ChangeScene('Lobby'); // 特殊浏览器直接进入大厅
  307. // } else {
  308. // this.ShowLogin(); // 普通浏览器显示登录界面
  309. // }
  310. this.ShowLogin();
  311. }
  312. }.bind(this)); // 保持上下文绑定
  313. },
  314. /* 代码逻辑说明:
  315. 1. 更新节奏控制:
  316. - 通过 m_nNeedUpdate 实现延迟更新机制
  317. - 当外部设置 m_nNeedUpdate = 1 时,实际在下一帧才会执行核心逻辑
  318. - 适用于等待其他初始化完成的场景
  319. 2. 核心流程:
  320. LoadConfig -> 预加载初始化 -> 平台判断 -> 执行对应流程
  321. 3. 平台差异化处理:
  322. - 原生平台:
  323. * 强制进行热更新检查
  324. * 使用 UpdateManager 弹窗处理更新流程
  325. * 更新完成后自动跳登录
  326. - Web平台:
  327. * 微信/QQ浏览器跳过登录直接进大厅(可能利用内置鉴权)
  328. * 普通浏览器需要显示登录界面
  329. 4. 关键方法说明:
  330. - ShowPrefabDLG: 动态加载预制弹窗
  331. * 参数1: 预制体名称
  332. * 参数2: 父节点
  333. * 参数3: 弹窗加载完成回调
  334. - CheckUpdate: 封装的热更新检查方法
  335. - ChangeScene: 场景切换工具方法
  336. 5. 注意事项:
  337. - 依赖 cc.gPreLoader 预加载系统的正确初始化
  338. - ShowPrefabDLG 需要确保预制体资源的正确加载
  339. - 微信/QQ浏览器逻辑可能需要配套的授权登录机制
  340. - 缺少错误处理逻辑(更新失败、网络异常等情况)
  341. - 使用函数式编程需注意闭包内存管理
  342. - bind(this) 的频繁使用可能影响性能
  343. */
  344. // ShowLogin: function () {
  345. // this.ShowPrefabDLG("Login", this.node, function () {}.bind(this));
  346. // },
  347. // ShowUpdate: function () {
  348. // this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  349. // Js.StartPreload(true, 0, function(){
  350. // this.ShowLogin();
  351. // }.bind(this));
  352. // }.bind(this));
  353. // },
  354. ShowLogin: function () {
  355. // 显示登录界面弹窗
  356. console.log("ShowLogin----")
  357. // cc.assetManager.loadBundle('Test', (err, bundle) => {
  358. // console.log("bundle",bundle)
  359. // bundle.load(`Nutton`, cc.Prefab, function (err, prefab) {
  360. // let newNode = cc.instantiate(prefab);
  361. // console.log("newNode",newNode,prefab)
  362. // this.node.addChild(newNode);
  363. // }.bind(this));
  364. // });
  365. // return;
  366. this.ShowPrefabDLG(
  367. "Login", // 预制体名称:登录界面
  368. this.node, // 父节点:当前场景根节点
  369. function () { // 加载完成回调(空操作)
  370. // 可在此处添加登录界面初始化后操作
  371. }.bind(this) // 确保回调函数中的this指向当前组件
  372. );
  373. },
  374. /* 代码逻辑说明:
  375. 1. 核心功能:动态加载并显示登录界面
  376. 2. 参数说明:
  377. - "Login": 预制体资源路径(假设在resources目录)
  378. - this.node: 作为弹窗的父节点,确保正确层级
  379. - 空回调:可能用于预留后续扩展
  380. 3. 典型使用场景:
  381. - 用户手动点击登录按钮
  382. - 自动登录失败时切换至登录界面
  383. */
  384. ShowUpdate: function () {
  385. // 显示更新管理器弹窗并启动预加载
  386. this.ShowPrefabDLG(
  387. "UpdateManager", // 预制体名称:更新管理器
  388. this.node, // 父节点:当前场景根节点
  389. function (Js) { // 弹窗实例回调
  390. // 启动预加载流程
  391. Js.StartPreload(
  392. true, // 参数1:是否显示进度条(假设)
  393. 0, // 参数2:预加载阶段标识
  394. function () {
  395. this.ShowLogin(); // 预加载完成后显示登录界面
  396. }.bind(this)
  397. );
  398. }.bind(this)
  399. );
  400. },
  401. /* 代码逻辑说明:
  402. 1. 核心流程:
  403. 显示更新弹窗 -> 初始化预加载 -> 完成后跳转登录
  404. 2. 参数详解:
  405. - StartPreload 参数分析:
  406. * true: 可能控制是否显示加载进度界面
  407. * 0: 可能表示预加载阶段或资源类型标识
  408. * 回调函数:衔接后续登录流程
  409. 3. 功能特点:
  410. - 更新管理器的显示与预加载流程解耦
  411. - 通过回调链实现异步操作顺序控制
  412. - 支持可视化更新进度显示
  413. 4. 注意事项:
  414. - 需要确保UpdateManager预制体实现StartPreload方法
  415. - 参数的具体含义需结合StartPreload实现分析
  416. - 缺少加载失败的重试机制
  417. - 硬编码参数不利于后续维护
  418. */
  419. /* 跨方法关联分析:
  420. 1. 流程衔接:
  421. ShowUpdate -> StartPreload完成 -> ShowLogin
  422. 2. 设计模式:
  423. - 模块化:将更新、登录功能封装为独立预制件
  424. - 观察者模式:通过回调函数实现流程控制
  425. 3. 改进建议:
  426. - 添加加载失败的回调处理
  427. - 使用配置对象代替硬编码参数
  428. - 考虑网络状态检测
  429. - 增加加载过渡动画
  430. */
  431. // //游戏入口
  432. // EnterGameScene:function(){
  433. // // 加载游戏
  434. // if(GameDef && g_ServerListDataLast){
  435. // if(window.LOG_NET_DATA)console.log("地址:", g_ServerListDataLast.szServerAddr+":"+g_ServerListDataLast.wServerPort);
  436. // this.m_Loading.active = true;
  437. // this.ShowPrefabDLG("UpdateManager", this.m_Loading, function (Js) {
  438. // Js.StartPreload(0, g_ServerListDataLast.wKindID, function() {
  439. // cc.gPreLoader.LoadRes(`Image_BG_BG${GameDef.BGIndex}`, '' + GameDef.KIND_ID, function(res) {
  440. // window.gGameBG = 'loading';
  441. // ChangeScene('Table');
  442. // }.bind(this));
  443. // }.bind(this));
  444. // }.bind(this));
  445. // }
  446. // },
  447. // LoadConfig: function() {
  448. // cc.share.LoadConfig();
  449. // }
  450. //游戏入口
  451. EnterGameScene: function () {
  452. // 进入游戏场景主流程
  453. if (GameDef && g_ServerListDataLast) { // 校验游戏配置和服务器数据已加载
  454. // 调试模式下打印服务器连接信息
  455. if (window.LOG_NET_DATA) console.log("地址:", g_ServerListDataLast.szServerAddr + ":" + g_ServerListDataLast.wServerPort);
  456. // 显示加载界面
  457. this.m_Loading.active = true;
  458. // 加载更新管理器弹窗
  459. this.ShowPrefabDLG("UpdateManager", this.m_Loading, function (Js) {
  460. // 启动资源预加载流程
  461. Js.StartPreload(
  462. 0, // 参数1:预加载模式(0可能表示基础资源加载)
  463. g_ServerListDataLast.wKindID, // 参数2:游戏种类ID
  464. function () { // 预加载完成回调
  465. // 加载特定游戏背景资源
  466. cc.gPreLoader.LoadRes(
  467. `Image_BG_BG${GameDef.BGIndex}`, // 动态拼接背景图路径
  468. '' + GameDef.KIND_ID, // 资源分类标识
  469. function (res) { // 资源加载完成回调
  470. window.gGameBG = 'loading'; // 设置全局背景状态
  471. ChangeScene('Table'); // 切换到游戏桌场景
  472. }.bind(this)
  473. );
  474. }.bind(this)
  475. );
  476. }.bind(this));
  477. }
  478. },
  479. LoadConfig: function () {
  480. cc.share.LoadConfig(); // 可能包含:音效开关、语言包、UI皮肤等全局配置
  481. },
  482. /* 功能扩展建议:
  483. 1. 添加加载超时机制:
  484. setTimeout(() => {
  485. if(!this.resLoaded) showErrorTip();
  486. }, 10000);
  487. 2. 实现进度反馈:
  488. - 在UpdateManager中暴露加载进度事件
  489. - 在加载界面添加进度条动画
  490. 3. 资源加载优化:
  491. - 使用cc.resources.load替代直接路径引用
  492. - 添加资源加载失败重试机制
  493. 4. 安全校验增强:
  494. - 验证服务器地址的合法性
  495. - 添加SSL证书检测(针对HTTPS)
  496. */
  497. onclickUpdateJump() {
  498. if (this.m_updateURL) {
  499. ThirdPartyOpenUrl(this.m_updateURL);
  500. }
  501. },
  502. });