Launch.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. this.updateIP = true;
  179. var xhr = cc.loader.getXMLHttpRequest();
  180. xhr.onreadystatechange = function () {
  181. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
  182. console.log("a1.king-mahjong.tw 返回",xhr.responseText);
  183. this.updateIP = false;
  184. window.UpdateIPRelatedAddresses(xhr.responseText);
  185. }
  186. }.bind(this);
  187. xhr.timeout = 5000;
  188. xhr.open("GET", encodeURI('https://a1.king-mahjong.tw'), true);
  189. xhr.send();
  190. },
  191. /* 代码逻辑说明:
  192. 1. 场景管理:
  193. - start() 初始化全局场景引用体系
  194. - 通过重置 g_Login/g_Lobby 等引用确保场景切换的干净状态
  195. - g_CurScene 实现当前场景的集中访问点
  196. 2. 动画流程控制:
  197. - animationEventHandler 处理开场动画结束事件
  198. - 完成时切换至Logo显示流程
  199. - 原设计使用延迟调用,现改为立即执行(需确认动画衔接是否合理)
  200. 3. 版本号管理:
  201. - 通过事件机制获取本地版本号
  202. - LocalVersion 事件可能由以下场景触发:
  203. * 本地存储读取完成
  204. * 网络请求获取最新版本
  205. * 热更新流程结束
  206. - 版本号显示使用ES6模板字符串格式化
  207. 4. 事件管理:
  208. - onEnable/onDisable 组成对称的事件监听生命周期
  209. - 防止组件销毁后残留监听导致报错
  210. 5. 注意事项:
  211. - 全局变量(g_XX)的使用需确保单场景切换时的正确清理
  212. - m_nNeedUpdate 的具体作用需结合更新逻辑分析
  213. - 直接调用 OnTimer_DelayShowLogo() 需确认不需要等待动画残留资源释放
  214. - 版本号标签更新依赖选择器正确指向目标节点
  215. */
  216. // update: function () {
  217. // if (this.m_nNeedUpdate > 0) {
  218. // this.m_nNeedUpdate--;
  219. // } else {
  220. // return;
  221. // }
  222. // this.LoadConfig();
  223. // cc.gPreLoader.Init(function () {
  224. // if (cc.sys.isNative) {
  225. // this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  226. // Js.CheckUpdate(function() { this.ShowLogin(); }.bind(this));
  227. // }.bind(this));
  228. // } else {
  229. // if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT || cc.sys.browserType == cc.sys.BROWSER_TYPE_MOBILE_QQ) {
  230. // ChangeScene('Lobby');
  231. // } else {
  232. // this.ShowLogin();
  233. // }
  234. // }
  235. // }.bind(this));
  236. // },
  237. update: function () {
  238. //获取最新IP
  239. if (this.updateIP) {
  240. return;
  241. }
  242. // 更新逻辑控制:仅在 m_nNeedUpdate 有效时执行
  243. if (this.m_nNeedUpdate > 0) {
  244. this.m_nNeedUpdate--; // 递减更新计数器
  245. } else {
  246. return; // 未达到更新条件时直接退出
  247. }
  248. // 加载基础配置文件(假设为游戏核心配置)
  249. this.LoadConfig();
  250. // cc.assetManager.loadBundle("21201", "", function (err, bundle) {
  251. // if (err) {
  252. // console.log('加载bundle错误---', "21201");
  253. // let bundle21201 = cc.assetManager.getBundle('21201');
  254. // console.log('加载bundle---21201', bundle21201);
  255. // return console.error(err);
  256. // }
  257. // console.log('加载bundle成功---21201');
  258. // //console.log('1-----------------',JSON.stringify(bundle));
  259. // console.log('2-----------------', JSON.stringify(bundle._config.paths._map));
  260. // for (let i in bundle._config.paths._map) {
  261. // let asset = bundle._config.paths._map[i];
  262. // console.log('3-----------------', asset);
  263. // for (let j = 0; j < asset.length; j++) {
  264. // console.log('4-----------------', JSON.stringify(asset[j]));
  265. // console.log('41-----------------', JSON.stringify(asset[j].ctor));
  266. // console.log('5-----------------', (asset[j].ctor));
  267. // console.log('6-----------------', (asset[j].ctor.toString()));
  268. // for (let key in asset[j]) {
  269. // if (typeof asset[j][key] === 'function') {
  270. // console.log(key + '()');
  271. // } else {
  272. // console.log(key + ': ' + asset[j][key]);
  273. // }
  274. // }
  275. // return;
  276. // }
  277. // }
  278. // console.dir(bundle, { depth: null });
  279. // }.bind(this));
  280. // return;
  281. // 初始化预加载系统
  282. cc.gPreLoader.Init(function (url) {
  283. if (url) {
  284. this.m_updateURL = url;
  285. if (this.m_updateTipNode) {
  286. this.m_updateTipNode.active = true;
  287. }
  288. } else {
  289. this.ShowLogin();
  290. }
  291. return;
  292. // 平台判断分支
  293. if (cc.sys.isNative) { // 原生平台(iOS/Android)&& window.IS_UPDATE
  294. // 显示更新管理弹窗
  295. this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  296. // 执行更新检查
  297. Js.CheckUpdate(function () {
  298. this.ShowLogin(); // 更新完成后显示登录界面
  299. }.bind(this));
  300. }.bind(this));
  301. } else { // Web 平台
  302. // 判断是否在微信/QQ内置浏览器
  303. // if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT ||
  304. // cc.sys.browserType == cc.sys.BROWSER_TYPE_MOBILE_QQ) {
  305. // ChangeScene('Lobby'); // 特殊浏览器直接进入大厅
  306. // } else {
  307. // this.ShowLogin(); // 普通浏览器显示登录界面
  308. // }
  309. this.ShowLogin();
  310. }
  311. }.bind(this)); // 保持上下文绑定
  312. },
  313. /* 代码逻辑说明:
  314. 1. 更新节奏控制:
  315. - 通过 m_nNeedUpdate 实现延迟更新机制
  316. - 当外部设置 m_nNeedUpdate = 1 时,实际在下一帧才会执行核心逻辑
  317. - 适用于等待其他初始化完成的场景
  318. 2. 核心流程:
  319. LoadConfig -> 预加载初始化 -> 平台判断 -> 执行对应流程
  320. 3. 平台差异化处理:
  321. - 原生平台:
  322. * 强制进行热更新检查
  323. * 使用 UpdateManager 弹窗处理更新流程
  324. * 更新完成后自动跳登录
  325. - Web平台:
  326. * 微信/QQ浏览器跳过登录直接进大厅(可能利用内置鉴权)
  327. * 普通浏览器需要显示登录界面
  328. 4. 关键方法说明:
  329. - ShowPrefabDLG: 动态加载预制弹窗
  330. * 参数1: 预制体名称
  331. * 参数2: 父节点
  332. * 参数3: 弹窗加载完成回调
  333. - CheckUpdate: 封装的热更新检查方法
  334. - ChangeScene: 场景切换工具方法
  335. 5. 注意事项:
  336. - 依赖 cc.gPreLoader 预加载系统的正确初始化
  337. - ShowPrefabDLG 需要确保预制体资源的正确加载
  338. - 微信/QQ浏览器逻辑可能需要配套的授权登录机制
  339. - 缺少错误处理逻辑(更新失败、网络异常等情况)
  340. - 使用函数式编程需注意闭包内存管理
  341. - bind(this) 的频繁使用可能影响性能
  342. */
  343. // ShowLogin: function () {
  344. // this.ShowPrefabDLG("Login", this.node, function () {}.bind(this));
  345. // },
  346. // ShowUpdate: function () {
  347. // this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  348. // Js.StartPreload(true, 0, function(){
  349. // this.ShowLogin();
  350. // }.bind(this));
  351. // }.bind(this));
  352. // },
  353. ShowLogin: function () {
  354. // 显示登录界面弹窗
  355. console.log("ShowLogin----")
  356. // cc.assetManager.loadBundle('Test', (err, bundle) => {
  357. // console.log("bundle",bundle)
  358. // bundle.load(`Nutton`, cc.Prefab, function (err, prefab) {
  359. // let newNode = cc.instantiate(prefab);
  360. // console.log("newNode",newNode,prefab)
  361. // this.node.addChild(newNode);
  362. // }.bind(this));
  363. // });
  364. // return;
  365. this.ShowPrefabDLG(
  366. "Login", // 预制体名称:登录界面
  367. this.node, // 父节点:当前场景根节点
  368. function () { // 加载完成回调(空操作)
  369. // 可在此处添加登录界面初始化后操作
  370. }.bind(this) // 确保回调函数中的this指向当前组件
  371. );
  372. },
  373. /* 代码逻辑说明:
  374. 1. 核心功能:动态加载并显示登录界面
  375. 2. 参数说明:
  376. - "Login": 预制体资源路径(假设在resources目录)
  377. - this.node: 作为弹窗的父节点,确保正确层级
  378. - 空回调:可能用于预留后续扩展
  379. 3. 典型使用场景:
  380. - 用户手动点击登录按钮
  381. - 自动登录失败时切换至登录界面
  382. */
  383. ShowUpdate: function () {
  384. // 显示更新管理器弹窗并启动预加载
  385. this.ShowPrefabDLG(
  386. "UpdateManager", // 预制体名称:更新管理器
  387. this.node, // 父节点:当前场景根节点
  388. function (Js) { // 弹窗实例回调
  389. // 启动预加载流程
  390. Js.StartPreload(
  391. true, // 参数1:是否显示进度条(假设)
  392. 0, // 参数2:预加载阶段标识
  393. function () {
  394. this.ShowLogin(); // 预加载完成后显示登录界面
  395. }.bind(this)
  396. );
  397. }.bind(this)
  398. );
  399. },
  400. /* 代码逻辑说明:
  401. 1. 核心流程:
  402. 显示更新弹窗 -> 初始化预加载 -> 完成后跳转登录
  403. 2. 参数详解:
  404. - StartPreload 参数分析:
  405. * true: 可能控制是否显示加载进度界面
  406. * 0: 可能表示预加载阶段或资源类型标识
  407. * 回调函数:衔接后续登录流程
  408. 3. 功能特点:
  409. - 更新管理器的显示与预加载流程解耦
  410. - 通过回调链实现异步操作顺序控制
  411. - 支持可视化更新进度显示
  412. 4. 注意事项:
  413. - 需要确保UpdateManager预制体实现StartPreload方法
  414. - 参数的具体含义需结合StartPreload实现分析
  415. - 缺少加载失败的重试机制
  416. - 硬编码参数不利于后续维护
  417. */
  418. /* 跨方法关联分析:
  419. 1. 流程衔接:
  420. ShowUpdate -> StartPreload完成 -> ShowLogin
  421. 2. 设计模式:
  422. - 模块化:将更新、登录功能封装为独立预制件
  423. - 观察者模式:通过回调函数实现流程控制
  424. 3. 改进建议:
  425. - 添加加载失败的回调处理
  426. - 使用配置对象代替硬编码参数
  427. - 考虑网络状态检测
  428. - 增加加载过渡动画
  429. */
  430. // //游戏入口
  431. // EnterGameScene:function(){
  432. // // 加载游戏
  433. // if(GameDef && g_ServerListDataLast){
  434. // if(window.LOG_NET_DATA)console.log("地址:", g_ServerListDataLast.szServerAddr+":"+g_ServerListDataLast.wServerPort);
  435. // this.m_Loading.active = true;
  436. // this.ShowPrefabDLG("UpdateManager", this.m_Loading, function (Js) {
  437. // Js.StartPreload(0, g_ServerListDataLast.wKindID, function() {
  438. // cc.gPreLoader.LoadRes(`Image_BG_BG${GameDef.BGIndex}`, '' + GameDef.KIND_ID, function(res) {
  439. // window.gGameBG = 'loading';
  440. // ChangeScene('Table');
  441. // }.bind(this));
  442. // }.bind(this));
  443. // }.bind(this));
  444. // }
  445. // },
  446. // LoadConfig: function() {
  447. // cc.share.LoadConfig();
  448. // }
  449. //游戏入口
  450. EnterGameScene: function () {
  451. // 进入游戏场景主流程
  452. if (GameDef && g_ServerListDataLast) { // 校验游戏配置和服务器数据已加载
  453. // 调试模式下打印服务器连接信息
  454. if (window.LOG_NET_DATA) console.log("地址:", g_ServerListDataLast.szServerAddr + ":" + g_ServerListDataLast.wServerPort);
  455. // 显示加载界面
  456. this.m_Loading.active = true;
  457. // 加载更新管理器弹窗
  458. this.ShowPrefabDLG("UpdateManager", this.m_Loading, function (Js) {
  459. // 启动资源预加载流程
  460. Js.StartPreload(
  461. 0, // 参数1:预加载模式(0可能表示基础资源加载)
  462. g_ServerListDataLast.wKindID, // 参数2:游戏种类ID
  463. function () { // 预加载完成回调
  464. // 加载特定游戏背景资源
  465. cc.gPreLoader.LoadRes(
  466. `Image_BG_BG${GameDef.BGIndex}`, // 动态拼接背景图路径
  467. '' + GameDef.KIND_ID, // 资源分类标识
  468. function (res) { // 资源加载完成回调
  469. window.gGameBG = 'loading'; // 设置全局背景状态
  470. ChangeScene('Table'); // 切换到游戏桌场景
  471. }.bind(this)
  472. );
  473. }.bind(this)
  474. );
  475. }.bind(this));
  476. }
  477. },
  478. LoadConfig: function () {
  479. cc.share.LoadConfig(); // 可能包含:音效开关、语言包、UI皮肤等全局配置
  480. },
  481. /* 功能扩展建议:
  482. 1. 添加加载超时机制:
  483. setTimeout(() => {
  484. if(!this.resLoaded) showErrorTip();
  485. }, 10000);
  486. 2. 实现进度反馈:
  487. - 在UpdateManager中暴露加载进度事件
  488. - 在加载界面添加进度条动画
  489. 3. 资源加载优化:
  490. - 使用cc.resources.load替代直接路径引用
  491. - 添加资源加载失败重试机制
  492. 4. 安全校验增强:
  493. - 验证服务器地址的合法性
  494. - 添加SSL证书检测(针对HTTPS)
  495. */
  496. onclickUpdateJump() {
  497. if (this.m_updateURL) {
  498. ThirdPartyOpenUrl(this.m_updateURL);
  499. }
  500. },
  501. });