Jump to content

How to properly load scripts


kabuto178
 Share

Recommended Posts

Quote

Game.load.script('level1.js', './states/Level1.js');

Game.state.add('level1', level1);

currently thats how I am trying to load the level1 state js file into my current state, however when I try to call game.state.start('level1'); it fails to find the mentioned state. I am doing something wrong but unable to find any documentation to address this exact issue. Or should I just stick to including the state files in the script tags of the game html page?

Link to comment
Share on other sites

  • 1 year later...

Hi,

Try changing to:

game.load.script('Level1', 'states/Level1.js');
game.state.add('Level1', Level1);

I'm very much a Phaser newbie, and basing my answer on https://github.com/MattMcFarland/phaser-menu-system/blob/master/game/main.js

This is what I have (which works):

index.html

<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8" />

    <style type="text/css">
        html, body, #canvas {
            height: 100%;
        }
        body {
            margin: 0 auto;
            padding: 0;
        }
    </style>

    <title>H.U.D.</title>

    <!-- Engine -->

        <!-- Version 2 -->
        <script type="text/javascript" src="node_modules/phaser-ce/build/phaser.min.js"></script>

        <!-- Version 3 -->
        <!-- <script type="text/javascript" src="node_modules/phaser/dist/phaser.min.js"></script> -->

    <!-- Plug-ins -->
    <script src="node_modules/@orange-games/phaser-input/build/phaser-input.js"></script>

    <!-- Boot state -->
    <script type="text/javascript" src="assets/states/boot.js"></script>

</head>

<body>

    <!-- Manual Control -->
    <script>
    </script>

    <!-- Draw -->
    <div id="canvas"></div>

</body>
</html>

boot.js

// Global Variables
var

    // Phaser Game Engine
    game = new Phaser.Game(

        // Width x Height
        window.innerWidth, window.innerHeight, // This is 1/2 of what Photoshop says it is

        // Renderer
        Phaser.AUTO,

        // Parent
        'canvas',

    ),

    // Used to detect resizing
    lastWidth   =   window.innerWidth,
    lastHeight  =   window.innerHeight,

    // Define Boot state
    Boot                    =   function () {};

// Setup
Boot.prototype              =   Object.create( Phaser.State.prototype );
Boot.prototype.constructor  =   Boot();

// Functions

Boot.prototype.preload = function () {

    game.load.script( 'Splash', 'assets/states/splash.js' );

}; // END preload()

Boot.prototype.create = function () {

    game.stage.backgroundColor = "#AA4488";
    game.debug.text( 'State: BOOT', 0, 25 );

    game.state.add( 'Splash', Splash, false );
    setTimeout( function () { game.state.start("Splash"); }, 1000 );

}; // END create()

// Add state, and start
game.state.add( 'Boot', Boot, true );

splash.js

// Setup state
var Splash                      =   function () {};
Splash.prototype                =   Object.create( Phaser.State.prototype );
Splash.prototype.constructor    =   Splash();

// Functions

Splash.prototype.create = function () {

    game.stage.backgroundColor = "#4488AA";
    game.debug.text( 'State: SPLASH SCREEN', 0, 25 );

}; // END create()

Best regards.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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