Sign in to follow this  
MrLane

Phaser 2: Can`t load image after preload() function

Recommended Posts

 
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create});
 
function preload(){
      game.load.image('tardis', "./tardis.jpg");
}
 
function create() {
 
       game.stage.backgroundColor = '#182d3b';
       var sprite1 = game.add.sprite(40, 100, 'tardis');

       //if i move it to preload t works fine

      game.load.image('flower', './flower.jpeg');

 
      game.load.start();
      var sprite2 = game.add.sprite(100, 100, 'flower');
 
      //ERROR:
      //Phaser.Cache.getImage: Key "flower" not found in Cache
}

Share this post


Link to post
Share on other sites

sure @MrLane

the problem is you're trying to add the new image before it's loaded. 

so:

 

game.load.image('flower', './flower.jpeg');
game.load.onLoadComplete.add(newImageLoaded, this);
game.load.start();

function newImageLoaded()
{
var sprite2 = game.add.sprite(100, 100, 'flower');
}
      

 

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.