MyMap.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import CanmeraScrpts from "../Canmera/CanmeraScrpts";
  8. import EventName, { GuideType } from "../EventName/EventName";
  9. import Prop from "../GameProp/Prop";
  10. import LoadManager from "../LoadManager";
  11. import LocalData from "../LocalData";
  12. import RewardMnr from "../Template/RewardMnr";
  13. const { ccclass, property } = cc._decorator;
  14. type MapDataType = {
  15. bg: string,
  16. fs: {
  17. sp: number,
  18. ro: number,
  19. sc: number,
  20. x: number,
  21. y: number,
  22. }[]
  23. }
  24. type MapType = {
  25. map: MapDataType[],
  26. mapX: number,
  27. mapY: number,
  28. }
  29. type MapPosData = {
  30. index_x: number,
  31. index_y: number,
  32. Id: number,
  33. NodePos: cc.Vec2,
  34. SubMapData: MapDataType,
  35. }
  36. type NeedUpdataIndex = {
  37. x: number,
  38. y: number,
  39. }
  40. @ccclass
  41. export default class MyMap extends cc.Component {
  42. MapjsonData: MapType
  43. @property(cc.Node)
  44. CharacterNode: cc.Node = null;
  45. MapPosData: MapPosData[] = []
  46. private _CurrentMapPosData: MapPosData = null;
  47. public get CurrentMapPosData(): MapPosData {
  48. return this._CurrentMapPosData;
  49. }
  50. public set CurrentMapPosData(value: MapPosData) {
  51. if (this._CurrentMapPosData == value) {
  52. } else {
  53. this._CurrentMapPosData = value;
  54. this.UpdateMap()
  55. }
  56. }
  57. //更新自己位置数据的间隔s
  58. UpMyindexInterval: number = 0.1
  59. //更新自己位置数据的间隔s
  60. NeedUpdataIndexData: NeedUpdataIndex[] = []
  61. First_Bron_RigidBody_type = false
  62. protected start(): void {
  63. this.Load('4')
  64. RewardMnr.getInstance().Reset()
  65. }
  66. public Load(level: string) {
  67. // cc.resources.load("json/map" + level, cc.JsonAsset, this.LoadDone.bind(this));
  68. let bundle = cc.assetManager.getBundle("sub");
  69. bundle.load("res/json/map" + level, cc.JsonAsset, this.LoadDone.bind(this));
  70. }
  71. private LoadDone(error: Error, resource: cc.JsonAsset) {
  72. if (error) {
  73. console.log(error.message);
  74. return;
  75. }
  76. if (window['map99']) {
  77. this.MapjsonData = window['map99']
  78. } else {
  79. this.MapjsonData = resource.json;
  80. }
  81. if (this.MapjsonData) {
  82. this.initMap()
  83. }
  84. }
  85. initMap() {
  86. this.NeedUpdataIndexData = this.GetMapUpdateIndex()
  87. //宽多少个
  88. this.MapjsonData.mapX
  89. //高多少个
  90. this.MapjsonData.mapY
  91. let total = this.MapjsonData.mapX * this.MapjsonData.mapY
  92. let index_x = 0
  93. let index_y = 0
  94. //生成全部地图
  95. for (let index = 0; index < total; index++) {
  96. // const SubMap = this.MapjsonData.map[index];
  97. // console.log(SubMap);
  98. //console.log('ID:', index);
  99. if (index % this.MapjsonData.mapX == 0) {
  100. index_y++
  101. index_x = 0
  102. }
  103. index_x++
  104. let SubMapPosData: MapPosData = {
  105. index_x: 1,
  106. index_y: 1,
  107. Id: 1,
  108. NodePos: null,
  109. SubMapData: this.MapjsonData.map[index]
  110. }
  111. SubMapPosData.Id = index
  112. SubMapPosData.index_x = (index_x - 1)
  113. SubMapPosData.index_y = (index_y - 1)
  114. SubMapPosData.NodePos = new cc.Vec2(750 * (index_x - 1), 1334 * (index_y - 1))
  115. this.MapPosData.push(SubMapPosData)
  116. let MapNode = cc.instantiate(new cc.Node())
  117. MapNode.addComponent(cc.Sprite)
  118. MapNode.setContentSize(750, 1334)
  119. MapNode.setPosition(SubMapPosData.NodePos)
  120. MapNode.name = 'bg' + index
  121. MapNode.parent = this.node
  122. console.error(index + '完毕');
  123. }
  124. this.InitCharacterPrefab()
  125. return
  126. console.log(this.MapPosData);
  127. }
  128. //初始化人物的预制体
  129. InitCharacterPrefab() {
  130. (async () => {
  131. let RoleSkin = LocalData.getInstance().getCurrentCharacterSkin();
  132. let Prefab = await LoadManager.instance.LoadAssets<cc.Prefab>('res/Role/Role' + RoleSkin);
  133. if (cc.isValid(Prefab)) {
  134. let PrefabNode = cc.instantiate(Prefab)
  135. cc.Canvas.instance.node.addChild(PrefabNode)
  136. let zindex = cc.Canvas.instance.node.getChildByName("Parabola").getSiblingIndex()
  137. PrefabNode.setSiblingIndex(zindex + 1)
  138. cc.Camera.main.node.getComponent(CanmeraScrpts).Character = PrefabNode
  139. this.CharacterNode = PrefabNode
  140. let wordpos = this.node.getChildByName('bg' + this.MapjsonData['BronID']).convertToWorldSpaceAR(
  141. cc.v2(this.MapjsonData['BronX'], this.MapjsonData['BronY']))
  142. this.CharacterNode.setPosition(this.CharacterNode.parent.convertToNodeSpaceAR(wordpos))
  143. //最开始的时候设置成静态的
  144. this.CharacterNode.getComponent(cc.RigidBody).type = cc.RigidBodyType.Static
  145. console.error('人物完毕');
  146. this.schedule(() => {
  147. let wordPos = this.CharacterNode.parent.convertToWorldSpaceAR(this.CharacterNode.position)
  148. let mapPos = this.node.convertToNodeSpaceAR(wordPos)
  149. this.Myindex(mapPos)
  150. }, this.UpMyindexInterval)
  151. }
  152. })();
  153. }
  154. Myindex(target: cc.Vec3) {
  155. let SceneSize = cc.v2(750, 1334)
  156. let halfX = SceneSize.x / 2
  157. let halfY = SceneSize.y / 2
  158. //是否越界
  159. let iscross = true
  160. for (let index = 0; index < this.MapPosData.length; index++) {
  161. const e = this.MapPosData[index];
  162. if (target.x > e.NodePos.x - halfX &&
  163. target.x <= e.NodePos.x + halfX &&
  164. target.y <= e.NodePos.y + halfY &&
  165. target.y > e.NodePos.y - halfY) {
  166. this.CurrentMapPosData = e
  167. // console.log('ID x y', e.Id, e.index_x, e.index_y);
  168. iscross = false
  169. break;
  170. }
  171. }
  172. if (iscross == true) {
  173. console.error('越界了 发送事件');
  174. }
  175. }
  176. 总共生成的地图数组 = []
  177. async UpdateMap() {
  178. //当前我在的地图下标
  179. this.CurrentMapPosData.index_x
  180. this.CurrentMapPosData.index_y
  181. this.CurrentMapPosData.Id
  182. //需要生成的地图数组
  183. let bronMap = []
  184. let MyIndex = cc.v2(this.CurrentMapPosData.index_x, this.CurrentMapPosData.index_y);
  185. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 })
  186. // bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX })
  187. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 })
  188. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 })
  189. // bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id })
  190. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 })
  191. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 })
  192. // bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX })
  193. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 })
  194. // 新增的上左、上、上右
  195. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 - 2 });
  196. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 - 1 });
  197. bronMap.push({ x: MyIndex.x, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 });
  198. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 + 1 });
  199. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y + 2, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX * 2 + 2 });
  200. // 新增的左、中、右
  201. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 2 });
  202. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 });
  203. bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX });
  204. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 });
  205. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 2 });
  206. // 新增的下左、下、下右
  207. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 2 });
  208. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 });
  209. bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id });
  210. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 });
  211. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 2 });
  212. // 新增的下左、下、下右
  213. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 2 });
  214. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 });
  215. bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX });
  216. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 });
  217. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 2 });
  218. // 新增的下左、下、下右
  219. bronMap.push({ x: MyIndex.x - 2, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 - 2 });
  220. bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 - 1 });
  221. bronMap.push({ x: MyIndex.x, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 });
  222. bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 + 1 });
  223. bronMap.push({ x: MyIndex.x + 2, y: MyIndex.y - 2, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX * 2 + 2 });
  224. // bronMap.push({ x: MyIndex.x, y: MyIndex.y, Id: this.CurrentMapPosData.Id })
  225. // bronMap.push({ x: MyIndex.x, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX })
  226. // bronMap.push({ x: MyIndex.x, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX })
  227. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id - 1 })
  228. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y, Id: this.CurrentMapPosData.Id + 1 })
  229. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX - 1 })
  230. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y + 1, Id: this.CurrentMapPosData.Id + this.MapjsonData.mapX + 1 })
  231. // bronMap.push({ x: MyIndex.x - 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX - 1 })
  232. // bronMap.push({ x: MyIndex.x + 1, y: MyIndex.y - 1, Id: this.CurrentMapPosData.Id - this.MapjsonData.mapX + 1 })
  233. for (let index = 0; index < bronMap.length; index++) {
  234. console.log(bronMap[index].Id);
  235. if (bronMap[index].x < 0 || bronMap[index].x > (this.MapjsonData.mapX - 1)) {
  236. continue
  237. }
  238. if (bronMap[index].y < 0 || bronMap[index].y > (this.MapjsonData.mapY - 1)) {
  239. continue
  240. }
  241. const SubMap = this.getSubMapDataByID(bronMap[index].Id)
  242. if (SubMap) {
  243. if (this.总共生成的地图数组.includes(SubMap.Id)) {
  244. } else {
  245. await this.BronSubMap(SubMap)
  246. this.总共生成的地图数组.push(SubMap.Id)
  247. }
  248. }
  249. }
  250. for (let index = 0; index < this.总共生成的地图数组.length; index++) {
  251. const MapId = this.总共生成的地图数组[index];
  252. const SubMap = this.getSubMapDataByID(MapId)
  253. if (SubMap) {
  254. for (let j = 0; j < bronMap.length; j++) {
  255. const element = bronMap[j];
  256. if (element.Id == MapId) {
  257. break
  258. }
  259. if (j == bronMap.length - 1) {
  260. this.DeleteSubMap(SubMap)
  261. const array = this.总共生成的地图数组
  262. const elementToRemove = MapId; // 要删除的元素
  263. this.总共生成的地图数组 = array.filter(item => item !== elementToRemove);
  264. }
  265. }
  266. }
  267. }
  268. ///初次的时候完全加载完毕才可以玩
  269. if (this.First_Bron_RigidBody_type == false) {
  270. this.First_Bron_RigidBody_type = true
  271. //最开始的时候设置成静态的
  272. this.CharacterNode.getComponent(cc.RigidBody).type = cc.RigidBodyType.Dynamic
  273. }
  274. }
  275. getSubMapDataByID(ID: number) {
  276. for (let k = 0; k < this.MapPosData.length; k++) {
  277. const SubMap = this.MapPosData[k];
  278. if (SubMap.Id == ID) {
  279. return SubMap
  280. }
  281. }
  282. console.error('不存在');
  283. return null
  284. }
  285. // async BronSubMap(SubMap: MapPosData) {
  286. // if (this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite)?.spriteFrame?.name == 'bg' + SubMap.SubMapData.bg) {
  287. // } else {
  288. // let bundle = cc.assetManager.getBundle("sub");
  289. // bundle.load('res/bg/bg' + SubMap.SubMapData.bg, cc.SpriteFrame, (err: Error, SpriteFrame) => {
  290. // if (err) {
  291. // console.log("bg_error:" + err);
  292. // return
  293. // }
  294. // this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = SpriteFrame
  295. // });
  296. // }
  297. // for (let j = 0; j < SubMap.SubMapData.fs.length; j++) {
  298. // const fs = SubMap.SubMapData.fs[j];
  299. // // let id_Flag = SubMap.Id + '' + parseInt(fs.x.toString()) + '-' + parseInt(fs.y.toString())
  300. // let id_Flag = SubMap.Id.toString() + fs.sp.toString() + fs.x.toString() + fs.y.toString()
  301. // if (!RewardMnr.getInstance().isEnable(id_Flag)) {
  302. // continue
  303. // }
  304. // let bundle = cc.assetManager.getBundle("sub");
  305. // bundle.load('res/footstep/footstep' + fs.sp, cc.Prefab, (err: Error, Prefab) => {
  306. // if (err) {
  307. // console.log("bg_error:" + err);
  308. // return
  309. // }
  310. // let SubPrefab = cc.instantiate(Prefab)
  311. // //@ts-ignore
  312. // SubPrefab.getComponent(Prop)?.id_Flag = id_Flag
  313. // //@ts-ignore
  314. // SubPrefab.getComponent(Prop)?.SubMap = SubMap.Id.toString()
  315. // //@ts-ignore
  316. // SubPrefab.getComponent(Prop)?.fs = fs
  317. // SubPrefab.setPosition(fs.x, fs.y)
  318. // SubPrefab.angle = -fs.ro
  319. // SubPrefab.setScale(fs.sc)
  320. // SubPrefab.active = true
  321. // this.node.getChildByName('bg' + SubMap.Id).addChild(SubPrefab)
  322. // });
  323. // }
  324. // }
  325. async BronSubMap(SubMap: MapPosData) {
  326. // 检查背景是否已经加载
  327. if (this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite)?.spriteFrame?.name == 'bg' + SubMap.SubMapData.bg) {
  328. // 背景已经加载,跳过
  329. } else {
  330. try {
  331. let bundle = cc.assetManager.getBundle("sub");
  332. let SpriteFrame = await this.loadSpriteFrameSync(bundle, 'res/bg/bg' + SubMap.SubMapData.bg);
  333. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = SpriteFrame;
  334. } catch (err) {
  335. console.log("bg_error:" + err);
  336. }
  337. }
  338. for (let j = 0; j < SubMap.SubMapData.fs.length; j++) {
  339. const fs = SubMap.SubMapData.fs[j];
  340. let id_Flag = SubMap.Id.toString() + fs.sp.toString() + fs.x.toString() + fs.y.toString();
  341. if (!RewardMnr.getInstance().isEnable(id_Flag)) {
  342. continue;
  343. }
  344. try {
  345. let bundle = cc.assetManager.getBundle("sub");
  346. let Prefab = await this.loadPrefabSync(bundle, 'res/footstep/footstep' + fs.sp);
  347. let SubPrefab = cc.instantiate(Prefab);
  348. //@ts-ignore
  349. SubPrefab.getComponent(Prop)?.id_Flag = id_Flag;
  350. //@ts-ignore
  351. SubPrefab.getComponent(Prop)?.SubMap = SubMap.Id.toString();
  352. //@ts-ignore
  353. SubPrefab.getComponent(Prop)?.fs = fs;
  354. SubPrefab.setPosition(fs.x, fs.y);
  355. SubPrefab.angle = -fs.ro;
  356. SubPrefab.setScale(fs.sc);
  357. SubPrefab.active = true;
  358. this.node.getChildByName('bg' + SubMap.Id).addChild(SubPrefab);
  359. } catch (err) {
  360. console.log("bg_error:" + err);
  361. }
  362. }
  363. }
  364. DeleteSubMap(SubMap: MapPosData) {
  365. this.总共生成的地图数组 = this.总共生成的地图数组.filter(item => item !== SubMap.Id);
  366. this.node.getChildByName('bg' + SubMap.Id).getComponent(cc.Sprite).spriteFrame = null
  367. this.node.getChildByName('bg' + SubMap.Id).children.forEach(e => { e.destroy() })
  368. }
  369. //得到需要更新地图的下标
  370. GetMapUpdateIndex() {
  371. this.MapjsonData.mapX
  372. this.MapjsonData.mapY
  373. let NeedUpMapArray = []
  374. //只有长宽都大于3的
  375. let X_Has_Couont = 0
  376. let Y_Has_Couont = 0
  377. let Youhua = false
  378. if (this.MapjsonData.mapX >= 3 && this.MapjsonData.mapY >= 3) {
  379. X_Has_Couont = this.MapjsonData.mapX - 2
  380. Y_Has_Couont = this.MapjsonData.mapY - 2
  381. Youhua = true
  382. }
  383. if (Youhua) {
  384. //开启优化
  385. for (let i = 1; i < this.MapjsonData.mapX - 1; i++) {
  386. for (let j = 1; j < this.MapjsonData.mapY - 1; j++) {
  387. //输出所有在这个ID需要更新的地图
  388. // console.log(i + j * this.MapjsonData.mapX)
  389. NeedUpMapArray.push({ x: i, y: j })
  390. }
  391. }
  392. } else {
  393. //不开启优化
  394. }
  395. console.log(NeedUpMapArray);
  396. return NeedUpMapArray
  397. }
  398. // 封装一个同步加载 SpriteFrame 的函数
  399. async loadSpriteFrameSync(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.SpriteFrame> {
  400. return new Promise((resolve, reject) => {
  401. bundle.load(path, cc.SpriteFrame, (err: Error, SpriteFrame: cc.SpriteFrame) => {
  402. if (err) {
  403. reject(err);
  404. } else {
  405. resolve(SpriteFrame);
  406. }
  407. });
  408. });
  409. }
  410. // 封装一个同步加载 Prefab 的函数
  411. async loadPrefabSync(bundle: cc.AssetManager.Bundle, path: string): Promise<cc.Prefab> {
  412. return new Promise((resolve, reject) => {
  413. bundle.load(path, cc.Prefab, (err: Error, Prefab: cc.Prefab) => {
  414. if (err) {
  415. reject(err);
  416. } else {
  417. resolve(Prefab);
  418. }
  419. });
  420. });
  421. }
  422. }