英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论