有没有更好的方法在Phaser.js中为多个组合物创建物理碰撞器?

huangapple go评论59阅读模式
英文:

Is there a better way to put together physics colliders for multiple groups in Phaser.js?

问题

这样我有7个物理组它们都相互碰撞到目前为止我的碰撞检测器看起来像这样

```javascript
this.physics.add.collider(this.photons, this.bottomQuarks);
this.physics.add.collider(this.photons, this.charmQuarks);
this.physics.add.collider(this.photons, this.downQuarks);
this.physics.add.collider(this.photons, this.strangeQuarks);
this.physics.add.collider(this.photons, this.topQuarks);
this.physics.add.collider(this.photons, this.upQuarks);
this.physics.add.collider(this.bottomQuarks, this.bottomQuarks);
this.physics.add.collider(this.bottomQuarks, this.charmQuarks);
this.physics.add.collider(this.bottomQuarks, this.downQuarks);
this.physics.add.collider(this.bottomQuarks, this.strangeQuarks);
this.physics.add.collider(this.bottomQuarks, this.topQuarks);
this.physics.add.collider(this.bottomQuarks, this.upQuarks);
this.physics.add.collider(this.charmQuarks, this.charmQuarks);
this.physics.add.collider(this.charmQuarks, this.downQuarks);
this.physics.add.collider(this.charmQuarks, this.strangeQuarks);
this.physics.add.collider(this.charmQuarks, this.topQuarks);
this.physics.add.collider(this.charmQuarks, this.upQuarks);
this.physics.add.collider(this.downQuarks, this.downQuarks);
this.physics.add.collider(this.downQuarks, this.strangeQuarks);
this.physics.add.collider(this.downQuarks, this.topQuarks);
this.physics.add.collider(this.downQuarks, this.upQuarks);
this.physics.add.collider(this.strangeQuarks, this.strangeQuarks);
this.physics.add.collider(this.strangeQuarks, this.topQuarks);
this.physics.add.collider(this.strangeQuarks, this.upQuarks);
this.physics.add.collider(this.topQuarks, this.topQuarks);
this.physics.add.collider(this.topQuarks, this.upQuarks);
this.physics.add.collider(this.upQuarks, this.upQuarks);

我觉得一种循环可能会有所帮助,但我不确定如何实现,而且我想添加更多的物理组,所以有没有办法简化物理碰撞器?


<details>
<summary>英文:</summary>

So, I have 7 Physics groups, and they all collide with each other, and so far, my collision detector looks like this:

```javascript
this.physics.add.collider(this.photons, this.bottomQuarks);
    this.physics.add.collider(this.photons, this.charmQuarks);
    this.physics.add.collider(this.photons, this.downQuarks);
    this.physics.add.collider(this.photons, this.strangeQuarks);
    this.physics.add.collider(this.photons, this.topQuarks);
    this.physics.add.collider(this.photons, this.upQuarks);
    this.physics.add.collider(this.bottomQuarks, this.bottomQuarks);
    this.physics.add.collider(this.bottomQuarks, this.charmQuarks);
    this.physics.add.collider(this.bottomQuarks, this.downQuarks);
    this.physics.add.collider(this.bottomQuarks, this.strangeQuarks);
    this.physics.add.collider(this.bottomQuarks, this.topQuarks);
    this.physics.add.collider(this.bottomQuarks, this.upQuarks);
    this.physics.add.collider(this.charmQuarks, this.charmQuarks);
    this.physics.add.collider(this.charmQuarks, this.downQuarks);
    this.physics.add.collider(this.charmQuarks, this.strangeQuarks);
    this.physics.add.collider(this.charmQuarks, this.topQuarks);
    this.physics.add.collider(this.charmQuarks, this.upQuarks);
    this.physics.add.collider(this.downQuarks, this.downQuarks);
    this.physics.add.collider(this.downQuarks, this.strangeQuarks);
    this.physics.add.collider(this.downQuarks, this.topQuarks);
    this.physics.add.collider(this.downQuarks, this.upQuarks);
    this.physics.add.collider(this.strangeQuarks, this.strangeQuarks);
    this.physics.add.collider(this.strangeQuarks, this.topQuarks);
    this.physics.add.collider(this.strangeQuarks, this.upQuarks);
    this.physics.add.collider(this.topQuarks, this.topQuarks);
    this.physics.add.collider(this.topQuarks, this.upQuarks);
    this.physics.add.collider(this.upQuarks, this.upQuarks);

I feel like a loop of sorts might help with this, but I'm not sure how I would Implement that, and I would like to add more physics groups in, so is there anyway to condense the physics colliders?

答案1

得分: 1

当然,你可以将应该发生碰撞的游戏对象/列表放入数组中,就像这样:

this.physics.add.collider(
    [this.photons, this.bottomQuarks, this.charmQuarks, ...],
    [this.photons, this.bottomQuarks, this.charmQuarks, ...]);

你只需选择应该发生碰撞的列表。

链接至文档

英文:

Sure you can just put the gameObjects/list that should collide into arrays, like so:

this.physics.add.collider(
    [this.photons, this.bottomQuarks, this.charmQuarks, ...],
    [this.photons, this.bottomQuarks, this.charmQuarks, ...]);

you just have to only pick the list, that should collide with each other.
(link to the documentation)

huangapple
  • 本文由 发表于 2023年6月13日 12:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定