Jump to content

How can I load assets mid game?


Vaughan
 Share

Recommended Posts

I'm working on a game where objects are spawned by players and I want to load the assets from the disk as they're created. I don't want to load everything up front. However, it seems like loading after the preload function dosen't actually seem to do add anything to the cache. 

It does not seem like loadTexture does anything, either. 

Link to comment
Share on other sites

I do have multiple states. While on the GameplayState, an object may be spawned already in the asset folder (there's a lot of them). It's added and loadTexture is called to load in the sprite. I want to fetch the sprite mid-game -- loading it all at once probably isn't a great idea. Or at the very least, I need a way to add sprites for an entire folder. 

Link to comment
Share on other sites

It's really not a great idea to load data mid-play. Phaser obviously can't load a "folder", it has no concept of folders and as JavaScript can't read a filesystem it would be impossible anyway, so it's up to you to provide the list of files.

 

But if you still want to go ahead you can call the Loader from any point. If you're doing it outside of preload then just add the assets via game.load.image() (or whatever), add a listener for when the load completes (game.load.onLoadComplete.add(yourFunc, this) and when the queue is ready just start it: game.load.start().

 

You may need to reset the loader first (load.reset), but I'm pretty sure it does that once the preload completes anyway.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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