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

时间:2015-04-02 17:35:51

标签: javascript canvas pixi.js

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

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

1 个答案:

答案 0 :(得分:0)

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");