Jump to content

Can't load state (Uncaught ReferenceError)


pain99
 Share

Recommended Posts

Hiii :)

 

Sorry for this question but i am new to programming, i just started learning JavaScript (and programming) a week ago and learning phaser three days ago.

 

I want to load some states but it doesn't work, the console says: 'Uncaught ReferenceError: bootState is not defined.'

 

I only coded the basic things, just to see if it works, but it doesn't.

 

Here are the files.

 

index.html:

<!DOCTYPE html><html>    <head>                <script src="http://cdnjs.cloudflare.com/ajax/libs/phaser/2.0.6/phaser.min.js"></script>        <script src="game.js"></script>        <script src="boot.js"></script>        <script src="load.js"></script>            </head>        <body>                <div id="gameDiv"></div>            </body></html>

boot.js:

var bootState = {    preload: function(){                game.load.image('progressBar', 'assets/progressBar.png');            },        create: function(){                game.stage.backgrounColor = '#3498db';        game.physics.startSystem(Phaser.Physics.ARCADE);                game.state.start('load');            }};

load.js:

var loadState = {    preload: function(){                var loadingLabel = game.add.text(game.world.centerX, 150, 'loading...', {font: '30px Arial', fill: '#ffffff'});        loadingLabel.anchor.setTo(0.5, 0.5);                var progressBar = game.add.sprite(game.world.centerX, 200, 'progressBar');        loadingLabel.anchor.setTo(0.5, 0.5);        game.load.setPreloadSprite(progressBar);                game.load.image('player', 'assets/player.png');            },        create: function(){                //game.state.start('menu');                }};

game.js:

var game = new Phaser.Game(500, 320, Phaser.AUTO, 'gameDiv');game.state.add('boot', bootState);game.state.add('load', loadState);game.state.start('boot');

I don't know what i'm doing wrong.  :(  Can anyone help me? :rolleyes:

And sorry for bad my english.  :P

Link to comment
Share on other sites

No problem - just remember that at its most basic (ignoring fancy advanced things like hoisting, and all of the program flow that results from functions, if statements and so on) JavaScript is executed left to right, top to bottom, just like a book. If a value is required, you need to make sure it exists first.

Link to comment
Share on other sites

  • 2 years later...
On 2.9.2014 at 1:22 PM, lewster32 said:

The problem is just that game.js is run first, and the states aren't defined at that point. Ttry changing your HTML so it loads the states first, then game last:


        <script src="boot.js"></script>        <script src="load.js"></script>        <script src="game.js"></script>

Helped me, ty mate. :-)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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