Launch.js 21 KB

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