给你稍微调整了一下代码。保证顺序即可。
import { _decorator, Component, instantiate, Node, Prefab, resources, Sprite, SpriteFrame } from 'cc';
import mapInfo from './Map1';
const { ccclass, property } = _decorator;
@ccclass('MapManager')
export class MapManager extends Component {
@property(Prefab) prefabBlock:Prefab;
@property(Prefab) prefabRigid:Prefab;
private _loadedMapBlock: Node[][] = [];
private _loadedCount = 0;
private _totoalCount = 0;
start() {
this._loadedMapBlock.length = mapInfo.length;
mapInfo.forEach((item,index) => { //根据json生成瓦片地图,每一个元素为地图每一列
const column = this._loadedMapBlock[index] = [];
mapInfo[index].forEach((data,index) => {
++this._totoalCount;
resources.load(data.src, SpriteFrame, (err, spriteFrame)=> {
++this._loadedCount;
if(err) {
console.log(err);
}
let node: Node;
if(data.type == 'way') {
let te = instantiate(this.prefabBlock)
te.getComponent(Sprite).spriteFrame = spriteFrame
node = te;
// this.node.addChild(te);
}else {
let rigid = instantiate(this.prefabRigid)
rigid.getComponent(Sprite).spriteFrame = spriteFrame
node = rigid;
// this.node.addChild(rigid);
}
column[index] = node;
if (this._loadedCount == this._totoalCount) {
this.onAllBlockLoaded();
}
})
})
})
}
private onAllBlockLoaded() {
this._loadedMapBlock.forEach((column, x) => {
column.forEach((node, y) => {
this.node.addChild(node);
})
})
}
update(deltaTime: number) {
}
}







