Jump to content

Making states work


Draxy
 Share

Recommended Posts

 

Noob here,

Having a hard time with making game states to work - I've been following tutorials and still no luck.

I'm really looking for an overview of what I have so far on states, more so than an error fix (witch is loadState is not defined in index.html line 23)

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>game v1</title>
    <script src="phaser.js"></script>
    <script src="jquery-3.2.1.js"></script>
    <script src="loadState.js"></script>
    <script src="update.js"></script>
    <link rel="stylesheet" href="css/game.css">

  </head>
  <body>
   <div class="gameDiv"></div>

  </body>
  <script type="text/javascript">
    (function() {
      var width = window.innerWidth;
      var height = window.innerHeight;

      var game = new Phaser.Game(width, height, Phaser.AUTO, 'gameDiv');
      game.state.add("loadState",loadState);
      game.state.add("updateState",updateState);
      game.state.start("loadState");
    })();
  </script>

</html>

loadState.js:

var loadState = {
  preload: function() {
    game.load.spritesheet("background" , "assets/backgroundgif2.png", 800, 336);
    game.load.image("ground","assets/red.png");
    game.load.spritesheet("blue_player", "assets/player_sprites/playertwo standing.png", 96, 96);

  },

  create: function(){
    game.scale.fullScreenScaleMode = Phaser.ScaleManager.EXACT_FIT;
    game.physics.startSystem(Phaser.Physics.ARCADE);
    platforms = game.add.group();
    platforms = game.add.physicsGroup();

    var background = game.add.sprite(0, 0, 'background');
    background.animations.add("background", [0,1,2,3,4,5,6,7,8], 10, true);
    background.animations.play("background");
    background.width = game.width;
    background.height = game.height;
    game.world.sendToBack(background);  //used this to place background behind platforms

    var ground = platforms.create(0, game.world.height - 64, 'ground');
    platforms.enableBody = true;
    platforms.setAll('body.immovable', true);
    ground.scale.setTo(2, 2);
    ground.alpha = 0; //used this to make the ground transparent

    blue_player = game.add.sprite(-10, game.world.height -490, 'blue_player');
    game.physics.arcade.enable(blue_player);
    blue_player.body.bounce.y = 0.2;
    blue_player.body.gravity.y = 300;
    blue_player.body.collideWorldBounds = true;
    blue_player.animations.add('idle', [0, 1, 2], 2, true);
    blue_player.animations.play("idle");

    game.state.start("updateState");
  }
};

and updateState.js:

I have also had var updateState error as undefined, witch does not happen in the tutorials I've used. Though this does not happen now with the code you see here. 

console.log("update is working");

var updateState = {
  update: function() {
    game.physics.arcade.collide(blue_player, platforms);
  }
};

Thanks for reading, hope you can set me straight.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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