Jump to content

Loading Images


Recommended Posts

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

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']);


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.

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.


  • Recently Browsing   0 members

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