javascript - 移相器重叠不起作用

标签 javascript phaser-framework

为什么我的 Phaser.js 的重叠有时有效,有时无效。 我已经为我的 Angular 色和障碍启用了街机。 这是代码。

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;
}

非常感谢

最佳答案

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

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

试试这个:

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;
}

关于javascript - 移相器重叠不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42433715/

相关文章:

javascript - 未捕获的 TypeError : this. _tweens[i].update 不是 Phaser.TweenManager.update 的函数

javascript - Phaser.io 和 TypeScript

javascript - Mapbox GL 合并过滤器问题

javascript - 在 Angular 中导入纯 JS 代码 - 7

javascript - 为什么我的代码不使用 jquery 更改光标?

javascript - 如何从单独的域请求 JSON 数据并自己使用它

javascript - Phaser缓存加载问题

javascript - 没有 FormData 的 AngularJs 文件上传

audio - canvas - 动画和播放声音的可点击、可触摸的矩形

javascript - 多人游戏金币