Jump to content

Can I load assets in the background?


ritherz
 Share

Recommended Posts

If I have a menu screen game state (where presumably users would probably spend a few seconds navigating to what they want to do), can I load the assets for the actual game while having the menu screen already displayed and working?

 

So it would look something like this:

 

  1. Preload menu state assets.
  2. Display menu state.
  3. While in the menu state (allowing navigation still), the browser fetches the assets for the next state ("playing the game" state).
  4. Player clicks "play".
  5. Load the rest of the game state assets that haven't been loaded in step (3)
  6. Change to game state.

 

Maybe this isn't even possible since javascript is not multi-threaded heh, but I figure I should at least ask the pros!

 

I'm trying to think of ways that my game can display the menu screen a bit quicker than it does now, so other ideas are welcome as well, but I think the asset downloads is probably the biggest chunk of time.

Link to comment
Share on other sites

Just an idea.

 

If your player is in menu and clicks through to start the game while your game assets haven't loaded what are you gonna do? Block start button until it's loaded? Start the game without all assets leaving blank space? Jsut curious what exactly you are aiming at.

 

Btw you can do what you say, just move loading state after your menu (so you go: preload state - for menu assets > menu state > loading state > game state), is this the way you meant it?

Link to comment
Share on other sites

I think the OP means: the user is going to spend a couple of seconds on the main menu. Why not shorten the first loading bar by a significant fraction and show the intro/main menu immediately, then load the rest of the stuff. Then, on state transition to actually playing the game, show a loading bar, if needed, for the rest of the assets.

 

Looking at the StateManager my initial answer is... maybe? In StateManager#preUpdate it resets the loader on preload. So, it expects all loading stuff to be in the preload callback and clears anything that might be loading already. However, if something were already in the cache at that point the loader would have nothing to do so it would load that resource really quickly if you loaded it again... maybe?

 

If you don't want that to happen you can set Loader#resetLocked to true. No reset, so I *believe* it'll just keep loading files. Haven't tested it, though.

Link to comment
Share on other sites

I think the OP means: the user is going to spend a couple of seconds on the main menu. Why not shorten the first loading bar by a significant fraction and show the intro/main menu immediately, then load the rest of the stuff. Then, on state transition to actually playing the game, show a loading bar, if needed, for the rest of the assets.

 

Looking at the StateManager my initial answer is... maybe? In StateManager#preUpdate it resets the loader on preload. So, it expects all loading stuff to be in the preload callback and clears anything that might be loading already. However, if something were already in the cache at that point the loader would have nothing to do so it would load that resource really quickly if you loaded it again... maybe?

 

If you don't want that to happen you can set Loader#resetLocked to true. No reset, so I *believe* it'll just keep loading files. Haven't tested it, though.

You're right, I want to load game state assets in the background while the player is in the menu state.  If the player manages to start a game before all assets are loaded in the background it would load up the rest of the assets in another loading screen.

 

Yeah, I'd have to play with it a lot myself to figure out if this would work, and I'll probably try some of your ideas if no one else responds- thank you.  I was wondering if this was a solved problem maybe there's a simple set of game.loadspriteinbackground(), game.loadspriteifnotalreadyloaded() functions or something.

Link to comment
Share on other sites

For game.loadspriteifnotalreadyloaded() you could simply check if the texture is in cache or not.

 

The problem is what happens if a sprite only loads partially (eg; 90%) when the user switches state? Probably the sprite would have to be loaded again.

 

Now, game.loadSpriteInBackground() would be a better idea. And I think you can do this by creating a global Phaser.Loader entity (global to the game, not local to a state), or use the existing global Loader. Now, what you should do, is in your state, instead of preloading the stuff, wait for the stuff required for this state to load (eg: check that all textures required for the current state are in the cache). 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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