如何使用 Pixijs 获取加载精灵 sheet 资产中定义的图像

how to get images defined in loaded sprite sheet asset with Pixijs

使用 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  
}

in 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"
    }
}

如何以编程方式获取 sprite sheets 中定义的图像列表?

PIXI 将您所有的精灵存储在全局对象中 PIXI.TextureCache

function onAssetsLoaded () {
   // getAssets here  
   console.log(PIXI.TextureCache);
}

所以要获得单个纹理,您可以做类似的事情

var texture1 = PIXI.TextureCache["image1.png"];

虽然您通常永远不需要这样做,因为添加 Sprite 就像

一样简单
var sprite = PIXI.Sprite.fromFrame("image1.png");