【问题标题】:Why isn't the player colliding with ground?为什么玩家不是与地面碰撞的?
【发布时间】:2016-03-30 11:58:04
【问题描述】:

为什么玩家没有与地面发生碰撞?我的意思是它从字面上穿过它。我很确定我错过了碰撞检测线,但似乎我找不到它:(。

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

【问题讨论】:

    标签: javascript collision-detection phaser-framework


    【解决方案1】:

    这是给那些不能通过这个的人的。您需要启用物理。所以这个:

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

    然后,当玩家跳上它时,它不会向下移动,你需要让它不动,所以这样:

    ground.body.immovable = true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多