为什么玩家不与地面相撞?

时间:2015-12-24 01:13:09

标签: javascript collision-detection phaser-framework

为什么玩家不与地面相撞?我的意思是它完全通过它。我很确定我错过了碰撞检测线,但似乎无法找到它:(。

JSBin(无法使用JSFiddle,因为它不支持移相器)。点击右上角的修改编辑代码。

代码:

// 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');

1 个答案:

答案 0 :(得分:0)

这适用于那些无法通过此功能的人。你需要启用物理。所以这个:

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

然后当玩家跳到它上面时它会向下移动你需要让它不可移动所以这个:

ground.body.immovable = true;