互聯(lián)網(wǎng)系統(tǒng)安卓手機(jī)優(yōu)化大師官方下載
????????
目錄
? ? ? ? 1.在a文件中定義一個(gè)公共的變量存儲(chǔ)動(dòng)態(tài)加載的圖集
? ? ? ? 2.在a.js中添加一個(gè)靜態(tài)方法,返回動(dòng)態(tài)加載的圖集
? ? ? ? 3.在b.js中使用a.js中定義的靜態(tài)方法獲取圖集,并使用它
????????假設(shè)a文件中用CC.resource.load
動(dòng)態(tài)加載了一張圖集,b
文件需要使用這張圖集,可以按照以下步驟:
? ? ? ? 1.在a文件中定義一個(gè)公共的變量存儲(chǔ)動(dòng)態(tài)加載的圖集
// a.js
let atlas = null;export default class A extends cc.Component {onLoad() {cc.resources.load("atlas/imgs", cc.SpriteAtlas, (err, res) => {if (err) {cc.error(err.message || err);return;}atlas = res;});}
}
? ? ? ? 2.在a.js
中添加一個(gè)靜態(tài)方法,返回動(dòng)態(tài)加載的圖集
// a.js
let atlas = null;export default class A extends cc.Component {// ...static getAtlas() {return atlas;}
}
? ? ? ? 3.在b.js
中使用a.js
中定義的靜態(tài)方法獲取圖集,并使用它
// b.js
import A from './a';export default class B extends cc.Component {onLoad() {let atlas = A.getAtlas();let spriteFrame = atlas.getSpriteFrame("img_1");// 使用 spriteFrame}
}
????????這樣,就可以從b.js
中成功調(diào)用a.js
中用CC.resource.load
動(dòng)態(tài)加載的圖集了。