Closed
Description
Lets make the loader better. Presently we're loading sprites by referencing the src then hardcoding the key in whatever module we're using them from. Ideally we want to load the reference to the src and key! and not hard code anything at all.
In the config, lets not store all of the sprite references in the loader attribute, instead lets make a default.resources attribute. The loader should handle the resources and that's it. Here's a code example:
config.js
config.default.graphicResources = {
placeHolder: {
src: "/location/ph.png",
key: "placeHolder1"
},
character: {
src:"/location/character.png",
key:"character1"
}
}
load.js
load.loadGraphics= (srcList) => {
for (let i in srcList){
let data = srcList[i];
game.load.image(data.key, data.src);
}
}
load.init = (data) => {
const graphics = data.graphicResources || config.default.graphicResources;
load.loadGraphics(graphics);
}
Now in any of our modules that need to create a sprite we can do this:
gameLoop.js
gameLoop.create = () => {
let characterSpriteData = [
config.default.graphicResources.character.key,
config.default.graphicResources.character.src
];
game.add.sprite(...characterSpriteData);
}
The tasks are:
- update config model with something like
config.default.graphicsResources
- dynamically load graphics with helper function like
loadGraphics
above.