Jump to content

v3 Loader - accessing the 'keys'


Recommended Posts



I've got a question about the v3 assetloader/resourceloader:

After loading an asset like such:

var loader = PIXI.loader;loader.add('img',"assets/image.jpg");

.. can I access the resource with (what I assume is ) the key?


Edit: posted it wrong the first time :unsure:


In other words? Is there a way to do this:

var sprite = PIXI.Sprite.fromFrame("img");

..instead of what I'm doing currently:

var sprite = PIXI.Sprite.fromFrame("assets/image.jpg");

appreciated as always!

Link to comment
Share on other sites

That is really confusing, because `'img'` is the key but in your example you say you are already using the key...


That is extra confusing because using the key in the fromX() convience methods doesn't work...


If you want to use the loader, just use the loader:



PIXI.loader.add('key', 'someImage.png').load(function (loader, resources) {    // this will log "true", they are all the same:    console.log(PIXI.loader.resources === loader.resources === resources);    // resources is an object keyed by your names, you can get the loaded    // texture like:    console.log(resources.key.texture);});
Link to comment
Share on other sites

I might be mis-interpreting your question (very possible!), but...


Do you just want to load an image and reference its texture using it's file path name?

You can do that like this:

PIXI.loader  .add("assets/image.jpg")  .load(setup);

(The `key` in Pixi's loader `add` method is optional - all you need is the URL.)

Then, after the image has loaded, create the sprite like this:

function setup() {  var sprite = new PIXI.Sprite.fromImage("assets/image.jpg");}
Link to comment
Share on other sites

Thanks @xerver & @d13!


Sorry, I switched the code snippets by mistake :unsure: . But you guys rocked it anyway, I think I've got it.

Just to clarify; I was looking for a less verbose way of accessing the resources after loading them.


It appears that these are the shortest ways of doing it:

new PIXI.Sprite.fromImage("assets/image.jpg");              //ornew PIXI.Sprite(PIXI.loader.resources["assetKey"].texture); //ornew PIXI.Sprite(resources["assetKey"].texture);             // through "var resource = PIXI.loader.resource"


Link to comment
Share on other sites

 I was looking for a less verbose way of accessing the resources after loading them.


I'm always looking for that too!

Hopefull Pixi v.4 will implement something like this: `var sprite = new PIXI.Sprite("assets/image.jpg")`

... and figure out the texture, key, or frame based on the context.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...