Jump to content

PIXI.loader.resources or TextureCache


Jambutters
 Share

Recommended Posts

Quite a javascript newbie here so bare with me.

So I'm not sure what the differences are between the two if any. So is it recommended to always reload images with loader rather than using fromImage? 

On a non related note: I get an uncaught type error : cannot set property 'frame' of undefined. When I remove the setup call from the bottom, the error message goes away but I'm not sure why. If it ran again, shouldn't there be no errors? Thanks

let renderer = PIXI.autoDetectRenderer(416,416);

let rootStage = new PIXI.Container();
document.body.appendChild(renderer.view);


class Entity {
    constructor(img,id, type, hp, x, y, height, width, spdX, spdY){
        this.img = img;
        this.id = id;
        this.type = type; //for players it this reffers to player class, monsters is different
        this.hp = hp;
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
        this.spdX = spdX;
        this.spdY = spdY;
    }
    attack() {
        console.log("aatt");
    }
}

let gUpdate = () =>{
    //setInterval(gUpdate, 1000/20);
    renderer.render(rootStage);
};


let z;
let setup = () =>{
    let e = PIXI.utils.TextureCache[player.img];
    let rectangle = new PIXI.Rectangle(0,0, 32,32);
    e.frame = rectangle;
    z = new PIXI.Sprite(e); //PIXI.loader.resources[player.img].texture

    rootStage.addChild(z);
    gUpdate();
}

let player = new Entity("img/aa.png","p","pyro",100,0,0,32,32);

PIXI.loader.add(player.img).load(setup);
setup();


 

Link to comment
Share on other sites

Dont do this:

let e = PIXI.utils.TextureCache[player.img];

Instead use the resources the loader loaded for you:

let e = PIXI.loader.resources[player.img].texture;

The error you get is because `PIXI.utils.TextureCache[player.img]` doesn't exist, this is happening because you are calling setup manually:

PIXI.loader.add(player.img).load(setup);
setup();

You pass it in as a callback, then immediately invoke it manually. Don't do that, just pass it in as a callback and the loader will call it when loading has completed (asynchronously).

Link to comment
Share on other sites

11 hours ago, xerver said:

Dont do this:

let e = PIXI.utils.TextureCache[player.img];

Instead use the resources the loader loaded for you:

let e = PIXI.loader.resources[player.img].texture;

Ah I see. So its always best to use the texture in the loader? When would PIXI.utils.TextureCache be used? Kind of confused between the two, they both generally serve the same purpose .

Link to comment
Share on other sites

  • 2 years later...
On 10/5/2016 at 4:19 AM, xerver said:

The texture cache is used to ensure that extra calls to .fromImage() don't make multiple browser requests. If you use the loader, use the loader. If you use the fromImage() methods, use those. Try not to mix them :)

 

How do I load cached images when using Loader.resources?

When I load the app the second time i get warnings that the Texture already have en entry:
"Texture added to the cache with an id [images/circle24.png] that already had an entry"

I can see the image is already present in the texture Cahe, but when using Loader i would like to get the texture from Loader.resources, but that one is empty if i dont load the image again...?

More on the case here:

/thanks

Link to comment
Share on other sites

  • 1 year later...
On 5/24/2019 at 12:43 PM, Kraghen said:

 

How do I load cached images when using Loader.resources?

When I load the app the second time i get warnings that the Texture already have en entry:
"Texture added to the cache with an id [images/circle24.png] that already had an entry"

I can see the image is already present in the texture Cahe, but when using Loader i would like to get the texture from Loader.resources, but that one is empty if i dont load the image again...?

More on the case here:

/thanks

Having similar issues

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