Jump to content

Dynamic load image


noelrb
 Share

Recommended Posts

Hi everyone. I am creating a game that consuming an image api.

 

Is it possible to load or change an image outside the preload method? For example, in the create method. I am making a game with a lot of images,  and i need to load in game runtime.

 

I tryed to change de cache object, but  without hit. Also tryed to use game.load.image, but not run out of preLoad method.

 

Any idea? 

 

Thanks!

Link to comment
Share on other sites

Ho noelrb,

you can create your own instance of the loader class to do this. The loader will put everything in the cache so it's ready to use.

But beware of overriding existing ids. This won't magically update existing objects using that texture. Only the ones created afterwards.

To update existing elements:

You have to refresh them with sprite.loadTexture. To update an entire texture atlas you have to refresh every single element with loadTexture that uses a texture from the atlas.

loader = new Phaser.Loader(game)loader.image('someimage', '//url/to/image' )loader.atlasJSONHash('anotherAtlas', '//url/to/texture' , '//url/to/atlas' )loader.onLoadComplete.addOnce(onLoaded)loader.start()onLoaded = function(){   console.log('everything is loaded and ready to be used')}

Regards George

Link to comment
Share on other sites

  • 1 year later...

Ho noelrb,

you can create your own instance of the loader class to do this. The loader will put everything in the cache so it's ready to use.

But beware of overriding existing ids. This won't magically update existing objects using that texture. Only the ones created afterwards.

To update existing elements:

You have to refresh them with sprite.loadTexture. To update an entire texture atlas you have to refresh every single element with loadTexture that uses a texture from the atlas.

loader = new Phaser.Loader(game)loader.image('someimage', '//url/to/image' )loader.atlasJSONHash('anotherAtlas', '//url/to/texture' , '//url/to/atlas' )loader.onLoadComplete.addOnce(onLoaded)loader.start()onLoaded = function(){   console.log('everything is loaded and ready to be used')}

Regards George

Well that just saved me about 2 hours of troubleshooting. Thanks!

Link to comment
Share on other sites

Spoke to soon. I noticed that after creating a new instance of the load class and loading a bunch of images after preload, many of my existing textures no longer load. I get errors like:

 

Texture with key 'cursor' not found.phaser-2.3.0.js:35781 Texture with key 'brush' not found.phaser-2.3.0.js:35781 Texture with key 'brush2b' not found.
 
I realize this probably has something to do with what George mentioned, "overriding existing ids." What IDs could I be overwriting if the texture keys are unique?
Link to comment
Share on other sites

  • 9 months later...
 Share

  • Recently Browsing   0 members

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