Phaser.js - 如何让两个精灵在其中一个精灵碰撞时停止移动

Phaser.js - How to have both sprites stop moving when either of the sprites collides

我正在制作一个游戏,其中有 2 个精灵(一个花花公子和一个破坏球),当按下按键时它们都会移动。那部分工作正常,但我希望它们在它们中的任何一个与侧面的墙壁碰撞时都停止。 (也有重力,所以精灵不断地与地板碰撞。) 所以:

nothing: nothing happens
a collides: STOP!
b collides: STOP!
a AND b collides: STOP!

我有以下代码片段:

//in create()

this.wall = this.physics.add.staticGroup()

this.dude = this.physics.add.image(...getPos([7, 1]), 'dude')
this.dude.setCollideWorldBounds(true)
this.physics.add.collider(this.dude, this.wall)

this.ball = this.physics.add.image(...getPos([7, 22]), 'ball')
this.ball.setCollideWorldBounds(true)
this.physics.add.collider(this.ball, this.wall)

//in update()

if (cursors.left.isDown || this.a.isDown) {
  this.dude.setVelocityX(-300)
  this.ball.setVelocityX(-300)
  //code to manage it should go here...
} else if (cursors.right.isDown || this.d.isDown) {
  this.dude.setVelocityX(300)
  this.ball.setVelocityX(300)
  //and here...
} else {
  this.dude.setVelocityX(0)
  this.ball.setVelocityX(0)
}

if (
  (cursors.up.isDown || this.w.isDown || this.space.isDown) &&
  this.dude.body.blocked.down
) {
  //the ball is not supposed to jump when the player jumps, and should fall according to gravity, but otherwise, it should imitate the player
  this.dude.setVelocityY(-1000)
  setTimeout(() => {
    this.dude.setVelocityY(0)
  }, 250)
}

这里有一些我试过但没有用的方法

• 为碰撞器添加回调函数——碰撞器函数无法区分地板和墙壁,我无法将地板设置为另一个单独的class,因为如果这家伙跳到墙上,它就会变成地板:

我找到了一个可行的方法!

它是将地图存储在一个数组中并从那里读取以检查花花公子或球是否已经坠毁。