Why isn't the player colliding with ground?

Ad

Why is the player not colliding with the ground? I mean it literally passes through it. I'm pretty sure I'm missing a collision detection line, but it seems I can't find it :(.

JSBin (Couldn't use JSFiddle since it doesn't support phaser). Click edit on top right corner to edit code.

Code:

// Initialize Phaser, and create a 400x490px game
var game = new Phaser.Game(400, 490, Phaser.CANVAS, 'gameDiv');
var CountDown = {
  preload: function() {
  },
  update: function() {
  },
  render: function() {
  }
}
var player;
var ground;
var mainState = {

  preload: function() {
    game.stage.backgroundColor = '#ccccb3';
    game.load.image('player', 'http://s3.postimg.org/jur41voi7/Doodle.png')
    game.load.image('ground', 'http://s4.postimg.org/c5zfgpjml/platform.png')
    game.load.image('platforms', 'http://s10.postimg.org/evjcsqt9x/sprite_plat.jpg');
  },

  create: function() {
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.world.setBounds(0, 0, 400, 9999);
    ground = game.add.sprite(0, 9980, 'ground')
    ground.scale.setTo(6, 1)

    player = game.add.sprite(100, 9960, 'player');
    player.scale.setTo(0.5);

    game.physics.arcade.enable(player);
    player.body.bounce.y = 0.6
    player.body.gravity.y = 500;
    player.body.collideWorldBounds = true;
    player.body.checkCollision.up = false;
    player.body.checkCollision.left = false;
    player.body.checkCollision.right = false;
    this.game.inputEnabled = true;
    this.game.input.useHandCursor = true; //FOR SMARTPHONES, TABLETS
    game.camera.follow(player, Phaser.Camera.FOLLOW_PLATFORMER);
    this.cursors = game.input.keyboard.createCursorKeys();
  },
  update: function() {
    game.physics.arcade.collide(player, ground);
    if (this.cursors.left.isDown) {
      //  Move to the left
      player.body.velocity.x = -150;
    } else if (this.cursors.right.isDown) {
      //  Move to the right
      player.body.velocity.x = 150;
    } else {
      player.body.velocity.x = 0;
    }
    if (this.cursors.up.isDown && player.body.touching.down) {
      player.body.velocity.y = -350;
    }
  },

  // Restart the game
  platformsCreate: function() {
  }
};

var Menu = {
  preload: function() {
  },
  create: function() {
  },
  update: function() {
  },
  render: function() {
  }
};

var Game_Over = {
  preload: function() {
  },
  create: function() {
  },
  update: function() {
  },
  render: function() {
  },
  onDown: function() {
  }
};
// Add and start the 'main' state to start the game
game.state.add('CountDown', CountDown)
game.state.add('main', mainState);
game.state.add('Menu', Menu);
game.state.add('Game_Over', Game_Over);
game.state.start('main');
Ad

Answer

Ad

This is for people who cant get pass this. You need to enable physics. So this:

game.physics.enable(ground, Phaser.Physics.ARCADE);

and then so when player jumps on it it dosent move down you need to make it immovable so this:

ground.body.immovable = true;
Ad
source: stackoverflow.com
Ad