Jump to content

V3 loader - acessing textures on "progress"


Schoening
 Share

Recommended Posts

I would like to change my code from this:

var burger = new PIXI.Sprite( textures["burger"].texture );

To this:

var burger = new PIXI.Sprite( textures["burger"] );

I thought that the easiest way of doing this would be to store the textures seperately from the parent object.

var texturesOnly = {};var name = textures["burger"].name;var texture = textures["burger"].texture;texturesOnly[name] = texture;

The most convienient place for this pseudo-code would be the loader.on("progress", cb); so that I can use the current loaded object and put it inside my custom texture object:

loader.on("progress", onTextureLoadProgress);function onTextureLoadProgress () {  var name = arguments[1];  var texture = arguments[1].texture;  var texturesOnly[name] = texture;};

But that code does not work!

The texture is undefined. 

 

Also, what is the point of .on("complete" and .once("complete" if .load gets passed the resource object aswell ? 

I have read the manual pages and I am still none the wiser...

Link to comment
Share on other sites

There were a lot of questions here, let me try to break it down:
 

I would like to change my code from this:

var burger = new PIXI.Sprite( textures["burger"].texture );
To this:
var burger = new PIXI.Sprite( textures["burger"] );
I thought that the easiest way of doing this would be to store the textures seperately from the parent object.

 


I honestly can't imagine why this would be something you need to do, but it definitely can be done.

 

 

var texturesOnly = {};var name = textures["burger"].name;var texture = textures["burger"].texture;texturesOnly[name] = texture;
The most convienient place for this pseudo-code would be the loader.on("progress", cb); so that I can use the current loaded object and put it inside my custom texture object:
loader.on("progress", onTextureLoadProgress);function onTextureLoadProgress () {  var name = arguments[1];  var texture = arguments[1].texture;  var texturesOnly[name] = texture;};
But that code does not work!
The texture is undefined.

 


Correct, progress event is emitted before the middleware runs for an object. This has to do with progress values changing as more resources are added to the loader, which can happen in a middleware. Feel free to change this functionality in resource-loader if you want, as long as all the tests pass. Easier would be just on load parse the resources object into your cache structure, then just use it.
 

 

 

Also, what is the point of .on("complete" and .once("complete" if .load gets passed the resource object aswell ? 
I have read the manual pages and I am still none the wiser...


The `.load()` method accepts a callback. If you pass one (you don't have to) it just binds it to the 'complete' event for you. Everything is done via events, callback parameters are just sugar for them.

The difference between `.on('complete', ...);` and `.once('complete', ...);` is that `.on()` is called each time an event is emitted, and `.once()` is only called a single time when the next instance of that event is emitted. You can read about the event API in EventEmitter3. You can listen to the 'complete' event yourself, or pass the load method a callack, they both operate exactly the same.

Link to comment
Share on other sites

Assuming your image file is called `images/burger.png`, you could just do something like this: 

var textures = PIXI.utils.TextureCache;var burger = new PIXI.Sprite(textures["images/burger.png"]);
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...