Jump to content

Properly preloading assets for use with all states?


christian.tucker
 Share

Recommended Posts

It's becoming quite tedious to have to load all of the assets for the current state everytime I switch the state, examples of when this becomes a problem is when networking code requests a sprite that's not yet loaded, which causes invisible players, monsters, etc to be drawn. This is due to how I handle events at login, none-the-less; However I've handled things in this way for numerous years while writing games in Java.

 

Inside of my states, I use the standard preload: function to load sprites/sound/sheets/etc.

    preload: function () {        game.load.spritesheet('warrior', 'assets/sprites/player/character/warrior.png', 32, 64);        game.load.image('grass', 'assets/sprites/tiles/grass.png');    },

However these are only good for the this state, once I change to another state these assets are no-longer usable until I load them again. What's the best way to prevent this. What's the best way to load everything at startup to have referencable at any time?

 

 

Link to comment
Share on other sites

Fairly sure you don't have to load all the assets every state switch, which is why the basic Boot > Preloader > Game state sequence works...

The solution used at work was to call the preload function from all expected states in the Preloader state.

The other option is to just put all the preload code into the preload state.

Link to comment
Share on other sites

Fairly sure you don't have to load all the assets every state switch, which is why the basic Boot > Preloader > Game state sequence works...

The solution used at work was to call the preload function from all expected states in the Preloader state.

The other option is to just put all the preload code into the preload state.

 

Perhaps this is a pretty novice question, and I hate to ask so many questions, but Javascript really isn't my forte, from my understanding you're saying to create a state that's loaded before every other state that loads everything in it's preload function, then to switch the state, this would make it so I don't need to preload any data in the new state(granted everything I needed was loaded in the "preloadstate", is that correct?

Link to comment
Share on other sites

Essentially, yes that is it...

There is an example project in the Phaser download that is nicely hidden away in resources\Project Templates\Basic folder.

This contains 4 states: Boot, Preloader, Main Menu, Game. (I rarely use the Main Menu state, so I forgot about it.)

Those files are nicely commented, and form the basis of all my projects.

Link to comment
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...
 Share

  • Recently Browsing   0 members

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