noelrb

Dynamic load image

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!

Share this post


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

Share this post


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

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

Share this post


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

Share this post


Link to post
Share on other sites

@feudalwars did you solve this?

I was getting something similar as I was trying to instantiate a separate loader and have two different loading threads.  It was because Phaser.Loader will make calls to shared game objects like StateManager meaning there's not any point in instantiating a separate one.

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.