|
| 1 | + |
| 2 | +BasicGame.Preloader = function (game) { |
| 3 | + |
| 4 | + this.background = null; |
| 5 | + this.preloadBar = null; |
| 6 | + |
| 7 | + this.ready = false; |
| 8 | + |
| 9 | +}; |
| 10 | + |
| 11 | +BasicGame.Preloader.prototype = { |
| 12 | + |
| 13 | + preload: function () { |
| 14 | + |
| 15 | + // These are the assets we loaded in Boot.js |
| 16 | + // A nice sparkly background and a loading progress bar |
| 17 | + this.background = this.add.sprite(0, 0, 'preloaderBackground'); |
| 18 | + this.preloadBar = this.add.sprite(300, 400, 'preloaderBar'); |
| 19 | + |
| 20 | + // This sets the preloadBar sprite as a loader sprite, basically |
| 21 | + // what that does is automatically crop the sprite from 0 to full-width |
| 22 | + // as the files below are loaded in. |
| 23 | + this.load.setPreloadSprite(this.preloadBar); |
| 24 | + |
| 25 | + // Here we load most of the assets our game needs |
| 26 | + this.load.image('titlepage', 'images/title.jpg'); |
| 27 | + this.load.atlas('playButton', 'images/play_button.png', 'images/play_button.json'); |
| 28 | + this.load.audio('titleMusic', ['audio/main_menu.mp3']); |
| 29 | + this.load.bitmapFont('caslon', 'fonts/caslon.png', 'fonts/caslon.xml'); |
| 30 | + // + lots of other required assets here |
| 31 | + |
| 32 | + }, |
| 33 | + |
| 34 | + create: function () { |
| 35 | + |
| 36 | + // Once the load has finished we disable the crop because we're going to sit in the update loop for a short while |
| 37 | + this.preloadBar.cropEnabled = false; |
| 38 | + |
| 39 | + }, |
| 40 | + |
| 41 | + update: function () { |
| 42 | + |
| 43 | + // You don't actually need to do this, but I find it gives a much smoother game experience. |
| 44 | + // Basically it will wait for our audio file to be decoded before proceeding to the MainMenu. |
| 45 | + // You can jump right into the menu if you want and still play the music, but you'll have a few |
| 46 | + // seconds of delay while the mp3 decodes - so if you need your music to be in-sync with your menu |
| 47 | + // it's best to wait for it to decode here first, then carry on. |
| 48 | + |
| 49 | + // If you don't have any music in your game then put the game.state.start line into the create function and delete |
| 50 | + // the update function completely. |
| 51 | + |
| 52 | + if (this.cache.isSoundDecoded('titleMusic') && this.ready == false) |
| 53 | + { |
| 54 | + this.ready = false; |
| 55 | + this.game.state.start('MainMenu'); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | +}; |
0 commit comments