Launch.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. if (window.WPMJ == 0) {
  35. this.$('Logowp').active = true;
  36. this.$('Logommh').active = false;
  37. } else {
  38. this.$('Logowp').active = false;
  39. this.$('Logommh').active = true;
  40. }
  41. // 关闭调试统计信息显示(如FPS、DrawCall等)
  42. cc.debug.setDisplayStats(false);
  43. // 调整当前节点尺寸以适应屏幕大小(假设为自定义函数,用于屏幕适配)
  44. FitSize(this.node);
  45. // 显示从Out到In的过渡效果,持续0.5秒(假设为自定义动画效果函数)
  46. ShowO2I(this.node, 0.5);
  47. // 初始化版本号标签并置空(通过自定义选择器获取Label组件)
  48. if (cc.sys.OS_ANDROID == cc.sys.os) {
  49. this.$('Version@Label').string = window.APP_BOTTOM_VER_ANDROID;
  50. } else {
  51. this.$('Version@Label').string = window.APP_BOTTOM_VER_IOS;
  52. }
  53. this.$('VersionUI@Label').string = ""//window.APP_VERSION_UI;
  54. // 初始化加载界面节点
  55. if (!this.m_Loading)
  56. this.m_Loading = this.$('loading'); // 获取loading子节点
  57. this.m_Loading.zIndex = 100; // 设置渲染层级
  58. this.m_Loading.active = false; // 默认隐藏加载界面
  59. // 禁用动画节点和Logo节点
  60. this.$('ani').active = false;
  61. //this.$('Logo').active = false;
  62. // 加载游戏设置(假设为全局函数,读取本地存储配置等)
  63. window.LoadSetting();
  64. // 获取龙骨动画组件(开场动画)
  65. // this.m_StartAni = this.$('StartAni').getComponent(dragonBones.ArmatureDisplay);
  66. // 根据启动动画设置执行不同逻辑
  67. if (window.START_ANIMATION == 0) {
  68. // 跳过开场动画的情况
  69. this.m_nNeedUpdate = 1; // 标记需要更新状态(具体用途需结合上下文)
  70. // this.$('ani').active = true; // 启用备用动画节点
  71. // 延迟0.4秒执行Logo显示(等待备用动画播放)
  72. this.scheduleOnce(this.OnTimer_DelayShowLogo, 0.4);
  73. // this.m_StartAni.node.active = false; // 隐藏龙骨动画节点
  74. } else {
  75. // // 需要播放开场动画的情况
  76. // // 添加动画播放完成事件监听
  77. // this.m_StartAni.addEventListener(
  78. // dragonBones.EventObject.COMPLETE,
  79. // this.animationEventHandler, // 动画结束回调
  80. // this // 回调上下文
  81. // );
  82. // this.m_StartAni.node.active = true; // 启用龙骨动画节点
  83. // this.m_StartAni.playAnimation('newAnimation', 1); // 播放指定动画(1次)
  84. }
  85. if (this.m_updateTipNode) {
  86. this.m_updateTipNode.active = false;
  87. }
  88. // 在初始化页面时显示加载提示
  89. // this.scheduleOnce(this.ShowLoading(null, '加载中...'),3)
  90. this.ShowLoading(null, '加载中...')
  91. // this.setTimeout(() => {
  92. // this.getServerIP
  93. // }, 2000);
  94. this.timeoutID = setTimeout(this.getServerIP.bind(this), 3000);
  95. // this.scheduleOnce(this.getServerIP, 2);
  96. // this.scheduleOnce(this.getServerIP, 5);
  97. this.getServerIP();
  98. },
  99. // start: function () {
  100. // g_Launch = this;
  101. // g_Login = null;
  102. // g_Lobby = null;
  103. // g_Table = null;
  104. // g_CurScene = this;
  105. // },
  106. // animationEventHandler: function (event) {
  107. // if (event.type === dragonBones.EventObject.COMPLETE) {
  108. // this.m_StartAni.node.active = false;
  109. // this.m_nNeedUpdate = 1;
  110. // this.OnTimer_DelayShowLogo();
  111. // //this.scheduleOnce(this.OnTimer_DelayShowLogo, 0.4);
  112. // }
  113. // },
  114. // OnTimer_DelayShowLogo: function() {
  115. // this.$('ani').active = true;
  116. // this.$('Logo').active = true;
  117. // },
  118. // onEnable: function() {
  119. // cc.director.on('LocalVersion',this.OnLocalVersion, this);
  120. // },
  121. // OnLocalVersion: function(ver) {
  122. // if(!ver) return;
  123. // window.APP_VERSION = ver;
  124. // this.$('Version@Label').string = `v${ver}`;
  125. // },
  126. // onDisable: function() {
  127. // cc.director.off('LocalVersion',this.OnLocalVersion, this);
  128. // },
  129. start: function () {
  130. // 初始化全局场景引用
  131. g_Launch = this; // 记录启动场景实例
  132. g_Login = null; // 清空登录场景引用
  133. g_Lobby = null; // 清空大厅场景引用
  134. g_Table = null; // 清空游戏桌引用
  135. g_CurScene = this; // 设置当前活动场景为启动场景
  136. },
  137. /* 动画事件处理回调 */
  138. // animationEventHandler: function (event) {
  139. // // 当龙骨动画播放完成时
  140. // if (event.type === dragonBones.EventObject.COMPLETE) {
  141. // // this.m_StartAni.node.active = false; // 隐藏开场动画节点
  142. // this.m_nNeedUpdate = 1; // 触发状态更新标志
  143. // this.OnTimer_DelayShowLogo(); // 立即显示Logo
  144. // // 原始延迟调用方案(已注释):
  145. // // this.scheduleOnce(this.OnTimer_DelayShowLogo, 0.4);
  146. // }
  147. // },
  148. /* 延迟显示Logo的定时回调 */
  149. OnTimer_DelayShowLogo: function () {
  150. this.$('ani').active = true; // 启用过渡动画节点
  151. // this.$('Logo').active = true; // 显示主Logo节点
  152. },
  153. /* 组件启用时的生命周期回调 */
  154. onEnable: function () {
  155. // 注册本地版本号事件监听
  156. cc.director.on('LocalVersion', this.OnLocalVersion, this);
  157. },
  158. /* 处理本地版本号事件 */
  159. OnLocalVersion: function (ver) {
  160. if (!ver) return; // 无效版本号直接返回
  161. // 更新全局版本号并显示在UI
  162. window.APP_VERSION = ver; // 存储到全局变量
  163. this.$('Version@Label').string = `v${ver}`; // 更新版本号标签
  164. console.log("OnLocalVersion----1", ver)
  165. },
  166. /* 组件禁用时的生命周期回调 */
  167. onDisable: function () {
  168. // 移除事件监听防止内存泄漏
  169. cc.director.off('LocalVersion', this.OnLocalVersion, this);
  170. },
  171. //获取服务器最新IP
  172. getServerIP() {
  173. if (window.GET_IP == false) return;
  174. if ((window.OPEN_DOMAIN_NET == true && cc.sys.isNative != cc.sys.os)) return;
  175. //this.ShowLogin(null, '加载中');
  176. console.log("获取IP--")
  177. this.updateIP = true;
  178. var xhr = new XMLHttpRequest();
  179. xhr.onreadystatechange = function () {
  180. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
  181. //this.StopLogin();
  182. console.log("获取到ip:");
  183. //获取到ip了就关闭定时器,不再轮询
  184. clearTimeout(this.timeoutID);
  185. console.log("a1.wangpaimj.com 返回", xhr.responseText);
  186. this.updateIP = false;
  187. window.UpdateIPRelatedAddresses(xhr.responseText);
  188. }
  189. }.bind(this);
  190. xhr.timeout = 5000;
  191. xhr.open("GET", encodeURI('http://a1.wangpaimj.com'), true);
  192. xhr.send();
  193. },
  194. // getServerIP() {
  195. // // console.log("window.GET_IP:", window.GET_IP, "window.GET_IP == false:", window.GET_IP == false);
  196. // if (window.GET_IP == false) {
  197. // return;
  198. // }
  199. // // console.log("window.OPEN_DOMAIN_NET:", window.OPEN_DOMAIN_NET, "window.OPEN_DOMAIN_NET == true && cc.sys.isNative != cc.sys.os:", window.OPEN_DOMAIN_NET == true && cc.sys.isNative != cc.sys.os);
  200. // if ((window.OPEN_DOMAIN_NET == true && cc.sys.isNative != cc.sys.os)) {
  201. // return;
  202. // }
  203. // // 初始化轮询参数
  204. // this.m_ipRetryCount = 0;
  205. // this.m_ipMaxRetries = 5; // 最大重试次数
  206. // this.m_ipBaseDelay = 2000; // 基础重试间隔(毫秒)
  207. // this.m_ipRetryTimer = null;
  208. // console.log("✅ 开始获取IP(轮询模式)--");
  209. // this.updateIP = true;
  210. // this._makeIPRequest();
  211. // },
  212. // // 发起IP请求(轮询核心方法)
  213. // _makeIPRequest() {
  214. // if (this.m_ipRetryCount >= this.m_ipMaxRetries) {
  215. // console.log("❌ IP获取失败,已达到最大重试次数:" + this.m_ipMaxRetries);
  216. // this.updateIP = false;
  217. // // 可以在这里添加失败回调或默认IP处理
  218. // return;
  219. // }
  220. // console.log("🔄 第 " + (this.m_ipRetryCount + 1) + " 次尝试获取IP...");
  221. // var xhr = new XMLHttpRequest();
  222. // xhr.onreadystatechange = function () {
  223. // if (xhr.readyState === 4) {
  224. // if (xhr.status >= 200 && xhr.status < 400) {
  225. // // 请求成功
  226. // console.log("✅ IP获取成功");
  227. // this.updateIP = false;
  228. // this._clearRetryTimer();
  229. // window.UpdateIPRelatedAddresses(xhr.responseText);
  230. // } else {
  231. // // 请求失败,安排重试
  232. // this._scheduleRetry("HTTP错误: " + xhr.status);
  233. // }
  234. // }
  235. // }.bind(this);
  236. // xhr.onerror = function () {
  237. // this._scheduleRetry("网络错误");
  238. // }.bind(this);
  239. // xhr.ontimeout = function () {
  240. // this._scheduleRetry("请求超时");
  241. // }.bind(this);
  242. // xhr.timeout = 5000;
  243. // xhr.open("GET", encodeURI('http://a1.wangpaimj.com'), true);
  244. // xhr.send();
  245. // },
  246. // // 安排重试
  247. // _scheduleRetry(errorMsg) {
  248. // this.m_ipRetryCount++;
  249. // console.log("⚠️ IP获取失败:" + errorMsg + ",将在" + this._getRetryDelay() + "ms后重试(" + this.m_ipRetryCount + "/" + this.m_ipMaxRetries + ")");
  250. // this._clearRetryTimer();
  251. // // 使用递增延迟(避免雪崩效应)
  252. // this.m_ipRetryTimer = this.scheduleOnce(function () {
  253. // this._makeIPRequest();
  254. // }.bind(this), this._getRetryDelay() / 1000); // scheduleOnce使用秒为单位
  255. // },
  256. // // 计算重试延迟(递增算法)
  257. // _getRetryDelay() {
  258. // // 使用指数退避策略:基础延迟 * 2^重试次数,最大不超过30秒
  259. // return Math.min(this.m_ipBaseDelay * Math.pow(2, this.m_ipRetryCount), 30000);
  260. // },
  261. // // 清除重试定时器
  262. // _clearRetryTimer() {
  263. // if (this.m_ipRetryTimer) {
  264. // this.unschedule(this.m_ipRetryTimer);
  265. // this.m_ipRetryTimer = null;
  266. // }
  267. // },
  268. // update: function () {
  269. // if (this.m_nNeedUpdate > 0) {
  270. // this.m_nNeedUpdate--;
  271. // } else {
  272. // return;
  273. // }
  274. // this.LoadConfig();
  275. // cc.gPreLoader.Init(function () {
  276. // if (cc.sys.isNative) {
  277. // this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  278. // Js.CheckUpdate(function() { this.ShowLogin(); }.bind(this));
  279. // }.bind(this));
  280. // } else {
  281. // if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT || cc.sys.browserType == cc.sys.BROWSER_TYPE_MOBILE_QQ) {
  282. // ChangeScene('Lobby');
  283. // } else {
  284. // this.ShowLogin();
  285. // }
  286. // }
  287. // }.bind(this));
  288. // },
  289. update: function () {
  290. //获取最新IP
  291. if (this.updateIP) {
  292. return;
  293. }
  294. // 更新逻辑控制:仅在 m_nNeedUpdate 有效时执行
  295. if (this.m_nNeedUpdate > 0) {
  296. this.m_nNeedUpdate--; // 递减更新计数器
  297. } else {
  298. return; // 未达到更新条件时直接退出
  299. }
  300. // 加载基础配置文件(假设为游戏核心配置)
  301. this.LoadConfig();
  302. // cc.assetManager.loadBundle("21201", "", function (err, bundle) {
  303. // if (err) {
  304. // console.log('加载bundle错误---', "21201");
  305. // let bundle21201 = cc.assetManager.getBundle('21201');
  306. // console.log('加载bundle---21201', bundle21201);
  307. // return console.error(err);
  308. // }
  309. // console.log('加载bundle成功---21201');
  310. // //console.log('1-----------------',JSON.stringify(bundle));
  311. // console.log('2-----------------', JSON.stringify(bundle._config.paths._map));
  312. // for (let i in bundle._config.paths._map) {
  313. // let asset = bundle._config.paths._map[i];
  314. // console.log('3-----------------', asset);
  315. // for (let j = 0; j < asset.length; j++) {
  316. // console.log('4-----------------', JSON.stringify(asset[j]));
  317. // console.log('41-----------------', JSON.stringify(asset[j].ctor));
  318. // console.log('5-----------------', (asset[j].ctor));
  319. // console.log('6-----------------', (asset[j].ctor.toString()));
  320. // for (let key in asset[j]) {
  321. // if (typeof asset[j][key] === 'function') {
  322. // console.log(key + '()');
  323. // } else {
  324. // console.log(key + ': ' + asset[j][key]);
  325. // }
  326. // }
  327. // return;
  328. // }
  329. // }
  330. // console.dir(bundle, { depth: null });
  331. // }.bind(this));
  332. // return;
  333. // 初始化预加载系统
  334. cc.gPreLoader.Init(function (url) {
  335. if (url) {
  336. this.m_updateURL = url;
  337. if (this.m_updateTipNode) {
  338. this.m_updateTipNode.active = true;
  339. }
  340. console.log("加载完毕");
  341. this.StopLoading();
  342. } else {
  343. this.ShowLogin();
  344. }
  345. return;
  346. // 平台判断分支
  347. if (cc.sys.isNative) { // 原生平台(iOS/Android)&& window.IS_UPDATE
  348. // 显示更新管理弹窗
  349. this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  350. // 执行更新检查
  351. Js.CheckUpdate(function () {
  352. this.ShowLogin(); // 更新完成后显示登录界面
  353. }.bind(this));
  354. }.bind(this));
  355. } else { // Web 平台
  356. // 判断是否在微信/QQ内置浏览器
  357. // if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT ||
  358. // cc.sys.browserType == cc.sys.BROWSER_TYPE_MOBILE_QQ) {
  359. // ChangeScene('Lobby'); // 特殊浏览器直接进入大厅
  360. // } else {
  361. // this.ShowLogin(); // 普通浏览器显示登录界面
  362. // }
  363. this.ShowLogin();
  364. }
  365. }.bind(this)); // 保持上下文绑定
  366. },
  367. // ShowLogin: function () {
  368. // this.ShowPrefabDLG("Login", this.node, function () {}.bind(this));
  369. // },
  370. // ShowUpdate: function () {
  371. // this.ShowPrefabDLG("UpdateManager", this.node, function (Js) {
  372. // Js.StartPreload(true, 0, function(){
  373. // this.ShowLogin();
  374. // }.bind(this));
  375. // }.bind(this));
  376. // },
  377. ShowLogin: function () {
  378. // 显示登录界面弹窗
  379. console.log("ShowLogin----")
  380. // cc.assetManager.loadBundle('Test', (err, bundle) => {
  381. // console.log("bundle",bundle)
  382. // bundle.load(`Nutton`, cc.Prefab, function (err, prefab) {
  383. // let newNode = cc.instantiate(prefab);
  384. // console.log("newNode",newNode,prefab)
  385. // this.node.addChild(newNode);
  386. // }.bind(this));
  387. // });
  388. // return;
  389. this.ShowPrefabDLG(
  390. "Login", // 预制体名称:登录界面
  391. this.node, // 父节点:当前场景根节点
  392. function () { // 加载完成回调(空操作)
  393. // 可在此处添加登录界面初始化后操作
  394. console.log("加载完毕");
  395. this.StopLoading();
  396. }.bind(this) // 确保回调函数中的this指向当前组件
  397. );
  398. },
  399. ShowUpdate: function () {
  400. // 显示更新管理器弹窗并启动预加载
  401. this.ShowPrefabDLG(
  402. "UpdateManager", // 预制体名称:更新管理器
  403. this.node, // 父节点:当前场景根节点
  404. function (Js) { // 弹窗实例回调
  405. // 启动预加载流程
  406. Js.StartPreload(
  407. true, // 参数1:是否显示进度条(假设)
  408. 0, // 参数2:预加载阶段标识
  409. function () {
  410. this.ShowLogin(); // 预加载完成后显示登录界面
  411. }.bind(this)
  412. );
  413. }.bind(this)
  414. );
  415. },
  416. // //游戏入口
  417. // EnterGameScene:function(){
  418. // // 加载游戏
  419. // if(GameDef && g_ServerListDataLast){
  420. // if(window.LOG_NET_DATA)console.log("地址:", g_ServerListDataLast.szServerAddr+":"+g_ServerListDataLast.wServerPort);
  421. // this.m_Loading.active = true;
  422. // this.ShowPrefabDLG("UpdateManager", this.m_Loading, function (Js) {
  423. // Js.StartPreload(0, g_ServerListDataLast.wKindID, function() {
  424. // cc.gPreLoader.LoadRes(`Image_BG_BG${GameDef.BGIndex}`, '' + GameDef.KIND_ID, function(res) {
  425. // window.gGameBG = 'loading';
  426. // ChangeScene('Table');
  427. // }.bind(this));
  428. // }.bind(this));
  429. // }.bind(this));
  430. // }
  431. // },
  432. // LoadConfig: function() {
  433. // cc.share.LoadConfig();
  434. // }
  435. //游戏入口
  436. EnterGameScene: function () {
  437. // 进入游戏场景主流程
  438. if (GameDef && g_ServerListDataLast) { // 校验游戏配置和服务器数据已加载
  439. // 调试模式下打印服务器连接信息
  440. if (window.LOG_NET_DATA) console.log("地址launch:", g_ServerListDataLast.szServerAddr + ":" + g_ServerListDataLast.wServerPort);
  441. // 显示加载界面
  442. this.m_Loading.active = true;
  443. // 加载更新管理器弹窗
  444. this.ShowPrefabDLG("UpdateManager", this.m_Loading, function (Js) {
  445. // 启动资源预加载流程
  446. Js.StartPreload(
  447. 0, // 参数1:预加载模式(0可能表示基础资源加载)
  448. g_ServerListDataLast.wKindID, // 参数2:游戏种类ID
  449. function () { // 预加载完成回调
  450. // 加载特定游戏背景资源
  451. cc.gPreLoader.LoadRes(
  452. `Image_BG_BG${GameDef.BGIndex}`, // 动态拼接背景图路径
  453. '' + GameDef.KIND_ID, // 资源分类标识
  454. function (res) { // 资源加载完成回调
  455. window.gGameBG = 'loading'; // 设置全局背景状态
  456. ChangeScene('Table'); // 切换到游戏桌场景
  457. }.bind(this)
  458. );
  459. }.bind(this)
  460. );
  461. }.bind(this));
  462. }
  463. },
  464. LoadConfig: function () {
  465. cc.share.LoadConfig(); // 可能包含:音效开关、语言包、UI皮肤等全局配置
  466. },
  467. onclickUpdateJump() {
  468. if (this.m_updateURL) {
  469. ThirdPartyOpenUrl(this.m_updateURL);
  470. }
  471. },
  472. });