Jump to content

How to add things to cache and restore


Drummer_si
 Share

Recommended Posts

Hi... I'm trying to load an image in, tint it in various colours then store to cache, so future calls will be faster.

Is this possible, and if so how?
 

I've tried many different options.. and currently have:

colour = Math.random() * 0xffffff;
 let name = "buildings/tempcity_" + colour;

if(!Global.app.cache.getImage(name)){
    img = new Phaser.Sprite(Global.app, 0, 0, "buildings/tempcity");    
    img.tint = Math.random() * 0xffffff;
    //Is this even the correct way to save data
    Global.app.cache.addRenderTexture(name, img.generateTexture());
}
//Unsure how to load the cache bitmap into a new sprite or image
img = Global.app.add.sprite(x, y, Global.app.cache.getImage(name)); 

 

And help would be gratefully appreciated.

Link to comment
Share on other sites

I haven't tried it but I think it would be something like

let baseImg = this.make.image(0, 0, 'tempcity');

let bitmap = this.make.bitmapData(baseImg.width, baseImg.height);

baseImg.tint = tint;

bitmap.draw(baseImg);

bitmap.generateTexture(name, function (texture) {
  let sprite = this.add.sprite(0, 0, texture);
}, this);

I'm really not sure it's worth the trouble though, unless you're sure that tinting is slowing things down.

Link to comment
Share on other sites

Cheers samme.

I finally did it with the code:

if(!Global.app.cache.checkImageKey(borderNameX)){
  img = new Phaser.Image(Global.app, 0, 0, "border/x");
  img.tint = Number("0x" + tile.owner.colour);
  Global.app.cache.addImage(borderNameX, null, img.generateTexture());					
}

//Then later on:
c = Global.app.make.sprite(0, 0, Global.app.cache.getImage(borderNameY));

 

Definately worth doing as I my test, I rendered 10,000 sprites on screen.. Half of them blue, half red.

Without caching it ran at less than 1fps
With caching, it ran about 40 fps

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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