Jump to content

Phaser.Cache.getImage: Key not found in Cache.


yltang
 Share

Recommended Posts

I am following the Shootout game.

 

There are three states: Boot, Preloader, and Game. Since there is very little code in Boot state, I tried to move the code to the Preloader state.

 

Previously:

var Boot = {  preload: function() {    game.load.image('preload', 'assets/preload.png');  },  create: function() {    game.input.maxPointers = 1;    game.state.start('Preloader');  },  };var Preloader = {  preload: function() {    this.preloadBar = game.add.sprite(120, 260, 'preload');    game.load.setPreloadSprite(this.preloadBar);    ...  },  ... };

 

Now, the Boot state is removed and the code is moved to the Preloader state:

var Preloader = {  preload: function() {    game.load.image('preload', 'assets/preload.png');    this.preloadBar = game.add.sprite(120, 260, 'preload');    game.load.setPreloadSprite(this.preloadBar);    ...  },    create: function() {    game.input.maxPointers = 1;  },  ...};

However, Phaser complains with the error message: Phaser.Cache.getImage: Key "preload" not found in Cache.

 

Why is that?

 

 

 

EDIT:

The following does'nt work, either. The statement "...setPreloadBar(...);" causes Phase internal errors.

var Boot = {  preload: function() {    game.load.image('preload', 'assets/preload.png');  },  create: function() {    this.preloadBar = game.add.sprite(120, 260, 'preload');    game.input.maxPointers = 1;    game.state.start('Preloader');  },  };var Preloader = {  preload: function() {       game.load.setPreloadSprite(Boot.preloadBar);    ...  },  ...}
Link to comment
Share on other sites

Hi, call to load does not load asset - it puts it into list that will be loaded later. And this later happen automatically when preload function is finished (in preload you list all assets you want to load) or when you call load.start().

 

I highly recommend to preserve Boot state separated from Preload state. If your Boot state has only little code, then be happy :)  - best functions are those, that do their work and are short and easy to read.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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