Jump to content

v3 access cached texture by name


terebentina
 Share

Recommended Posts

Hi,

 

I was wondering if there's any way to access a texture that was already loaded via PIXI.loader by its given resource name. 

The reason for this is because I'd prefer not to use the path to the image all over my code, in case I change that.

So if I load a resource like 

{name: 'foo', url: 'assets/img/foo.png'}

 I would like to then use

PIXI.Sprite.fromName('foo');

instead of 

PIXI.Sprite.fromImage('assets/img/foo.png');

I know there's no "fromName" method in Sprite but is something like this doable without having access to the original resource object returned by the loader in the load() method?

 

Link to comment
Share on other sites

When the loader completes all the resources it loaded are in `PIXI.loader.resources`, it also passes the resources to the complete callback you register:

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

  • 6 months later...

I see false returned from this.

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

Also,  PIXI.loader is comming back undefined ... any clue what the issue might be?  I'm a PIXI noob, but am rather far along with Javascript, so this is kind of baffling.

Link to comment
Share on other sites

hmm, dont know what to suggest, other than version (which is fine), you're not using any funky module loader that hides `PIXI` from global?

<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/3.0.8/pixi.min.js"></script>
// log output> PIXI< Object {VERSION: "3.0.8", PI_2: 6.283185307179586, RAD_TO_DEG: 57.29577951308232, DEG_TO_RAD: 0.017453292519943295, TARGET_FPMS: 0.06…}> PIXI.loader< f {baseUrl: "", progress: 0, loading: false, _progressChunk: 0, _beforeMiddleware: Array[0]…}

Although, thinking about it, you cant have `false` returned, if `PIXI.loader` is undefined it would throw when you try to call a method on an undefined type.

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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