brentstrandy Posted May 17, 2016 Share Posted May 17, 2016 Is it possible to load the same sprite under different names without loading multiple instances of the sprite? Preloader dilemma (notice how the same sprite is being loaded multiple times): this.load.image('alien x blood', 'images/red splatter.png'); this.load.image('alien y blood', 'images/red splatter.png'); this.load.image('alien z blood', 'images/green splatter.png'); I load the same sprite because during gameplay I dynamically choose sprites based on a string: this.game.add.sprite(x, y, 'alien ' + alienType + ' blood'); In my happy little world I'd like to be able to do something like this: this.load.image(['alien x blood', 'alien y blood'], 'images/red splatter.png'); Any recommendations/thoughts? Link to comment Share on other sites More sharing options...
brejep Posted May 17, 2016 Share Posted May 17, 2016 I would load the image once with a single key. This is the key associated with the image in the cache and I think it would unnecessarily complicate things to have several references to the same image in the cache. My suggestion to solve your problem would be to map these strings to a cache key. This could be done fairly easily with an object: this.load.image('red blood splatter', 'images/red splatter.png'); this.load.image('green blood splatter', 'images/green splatter.png'); var cacheKeyMap = { 'alien x blood': 'red blood splatter', 'alien y blood': 'red blood splatter', 'alien z blood': 'green blood splatter' }; Then you just need to look up the mapping to get your cache key: this.game.add.sprite(x, y, cacheKeyMap['alien ' + alienType + ' blood']); brentstrandy 1 Link to comment Share on other sites More sharing options...
o0Corps0o Posted May 17, 2016 Share Posted May 17, 2016 why can't you just have this.load.image('alien xy blood', 'images/red splatter.png'); ? Link to comment Share on other sites More sharing options...
brentstrandy Posted May 17, 2016 Author Share Posted May 17, 2016 @brejep - I like it! Thank you. @o0Corps0o - hmmm... Interesting idea, but I can't picture how it would work. Link to comment Share on other sites More sharing options...
Recommended Posts