Phaser重叠不起作用

时间:2017-02-24 08:15:06

标签: javascript phaser-framework

为什么我的Phaser.js有时会重叠工作,而不是有时候。 我为我的角色和障碍启用了街机。 这是代码。

function create(){
  ...//other sprite
  obs = game.add.group();
  obs.enableBody = true;
  obstacle = obs.create(800, game.world.height - 68, 'obstacle');
  obstacle.scale.setTo(0.5,0.5);
  game.physics.arcade.enable(obstacle);
  obstacle.events.onOutOfBounds.add(function(obstacle) {
    obstacle.kill(); 
  }, this);
}

function update(){
  ...
  game.physics.arcade.collide(character, obstacle, gameOver, null, this);
}

function generateObs(){//its where i generate new obstacle, and is my biggest suspect as well
  obstacle = obs.create(800, game.world.height - 68, 'obstacle');
  obstacle.scale.setTo(0.5,0.5);
  game.physics.arcade.enable(obstacle);
  obstacle.body.velocity.x = -300;
}

很多

1 个答案:

答案 0 :(得分:0)

在为您提供问题的可能解决方案之前:

  • 没有必要将物理应用于其中创建的元素 已经有物理学的小组。
  • 如果你需要知道两个身体 重叠,但您不需要模拟完全碰撞使用'重叠'。

试试这个:

var obs;

function create(){
  //Active Physics ARCADE
  game.physics.startSystem(Phaser.Physics.ARCADE);
  ...//other sprite
  obs = game.add.group();
  obs.enableBody = true;
  obs.physicsBodyType = Phaser.Physics.ARCADE;

  var obstacle = obs.create(800, game.world.height - 68, 'obstacle');
  obstacle.scale.setTo(0.5,0.5);
  obstacle.events.onOutOfBounds.add(function(obstacle) {
    obstacle.kill(); 
  }, this);
}

function update(){
  ...
  //Use the group, since the callback will automatically pass the group element that overlaps with the player. A group is used to handle multiple elements, otherwise use only one variable :)
  game.physics.arcade.overlap(character, obs, gameOver, null, this);
  /*If you want your obstacle to move in the game you must implement this in the Update function
    You can access an element of the group, for this there are multiple methods one of them is to get the element from the element's exists attribute

    var obstacle = obs.getFirstExists(true);
    if (obstacle) { obstacle.body.velocity.x = -300; }
  */ 
}

function gameOver(character, obstacle) {
  //From here you have access to the element that collides with the player (you can evaluate with a condition if it is the obstacle you want)
  //if (obstacle.name == 'x') { console.log(obstacle); }
  console.log('Game Over!');
}

function generateObs(){
  var obstacle = obs.create(800, game.world.height - 68, 'obstacle');
  obstacle.scale.setTo(0.5,0.5);
  obstacle.body.velocity.x = -300;
}