royibernthal Posted May 8, 2014 Share Posted May 8, 2014 I'm trying to create a preloader for my game. I figured I'd have to make 2 states: PreloaderState - I load the preloader sprite so it can appear in GameState, when loaded I move to GameState.GameState - In preload() I set the preloader sprite I loaded in PreloaderState. However when I try to CREATE the preloader sprite the whole screen turns black (an error occured), without creating a preloader sprite everything works fine and the game is started. Is it not possible to add a sprite instance to the game in a state's preload()? What am I doing wrong? Here are the states: /*Preloader State*/var PreloaderState = function(game) {};PreloaderState.prototype.preload = function(){game.load.image('preloader', 'images/preloader.jpg');};PreloaderState.prototype.create = function(){game.state.start('game');}/*Game State*/var GameState = function(game) {};GameState.prototype.preload = function(){var preloader = game.sprite.add(0, 0, 'preloader'); //Creates an error, code after this line is not reachedgame.load.setPreloaderSprite(preloader);} Link to comment Share on other sites More sharing options...
Pedro Alpera Posted May 8, 2014 Share Posted May 8, 2014 You can see a preloader working in the basic phaser template: https://github.com/photonstorm/phaser/tree/master/resources/Project%20Templates/Basic Link to comment Share on other sites More sharing options...
rich Posted May 8, 2014 Share Posted May 8, 2014 Have you tried using the Project Templates for this? Link to comment Share on other sites More sharing options...
xdiepx Posted May 8, 2014 Share Posted May 8, 2014 var preloader = game.sprite.add(0, 0, 'preloader'); i think you have the game.sprite.add the wrong way round. should be: var preloader = game.add.sprite(0, 0, 'preloader'); Link to comment Share on other sites More sharing options...
royibernthal Posted May 8, 2014 Author Share Posted May 8, 2014 xdiepx - right, a stupid mistake on my behalf. Another one I noticed - I wrote setPreloaderSprite when it should be setPreloadSprite. Fixed. Pedro Alpera and rich - I was not aware of the existence of templates actually, it helped me realize many other things about structure and features I wasn't aware of, thanks. Link to comment Share on other sites More sharing options...
xdiepx Posted May 8, 2014 Share Posted May 8, 2014 You are welcome Link to comment Share on other sites More sharing options...
Recommended Posts