使用Pixijs,我可以按如下方式加载资产:
var assetsToLoader = ["sprite-sheet-1.json", "sprite-sheet-2.json"];
loader = new PIXI.AssetLoader(assetsToLoader);
loader.onComplete = onAssetsLoaded
loader.load();
function onAssetsLoaded () {
// getAssets here
}
在sprite-sheet-1.json中
{"frames": {
"image1.png":
{
"frame": {"x":0,"y":0,"w":40,"h":40},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":40,"h":40},
"sourceSize": {"w":40,"h":40}
},
"image2.png":
{
"frame": {"x":40,"y":0,"w":40,"h":40},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":40,"h":40},
"sourceSize": {"w":40,"h":40}
}},
"meta": {
"app": "http://www.texturepacker.com",
"version": "1.0",
"image": "../img/sprite-sheet-1.png.png",
"format": "RGBA8888",
"size": {"w":279,"h":40},
"scale": "1",
"smartupdate": "$TexturePacker:SmartUpdate:9e3e5afd01ea8e418afabfbdcd724485$",
"extraMeta": "this is Extra"
}
}
如何以编程方式获取精灵工作表中定义的图像列表?
PIXI将所有精灵存储在全局对象PIXI.TextureCache
中。
function onAssetsLoaded () {
// getAssets here
console.log(PIXI.TextureCache);
}
所以要得到一个单一的纹理,你可以做一些类似的事情
var texture1 = PIXI.TextureCache["image1.png"];
不过,你通常不需要这样做,因为添加精灵就像一样简单
var sprite = PIXI.Sprite.fromFrame("image1.png");