Sign in to follow this  
Jambutters

PIXI.loader.resources or TextureCache

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


 

Share this post


Link to post
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).

Share this post


Link to post
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 .

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.