我有一个精灵组合

this.photons = this.physics.add.group({
      key: "photon",
      repeat: 11,
      setXY: { x: 50, y: 50, stepX: 32 },
    });

    this.photons.children.iterate(function (child) {
      child.body.bounce.set(1);
      child.setVelocity(Phaser.Math.Between(300, 500),20);
      child.body.collideWorldBounds = true;
    });

编辑: 子元素们以this.physics.add.collider(this.photons, this.photons);比1的速度相互碰撞

编辑:摄像机代码

const cursors = this.input.keyboard.createCursorKeys();

    const controlConfig = {
      camera: this.cameras.main,
      left: cursors.left,
      right: cursors.right,
      up: cursors.up,
      down: cursors.down,
      acceleration: 0.06,
      drag: 0.0005,
      maxSpeed: 1.0,
    };

    this.controls = new Phaser.Cameras.Controls.SmoothedKeyControl(
      controlConfig
    );

我如何才能让每个子元素面对他们的矢量的方向,改变每一次碰撞?

推荐答案

这取决于它们应该在什么地方相撞.如果世界受到约束,你将不得不:

  1. photons上设置属性onWorldBounds

     this.photons.children.iterate(function (child) {
         ...
         child.body.onWorldBounds = true;
     });
    
  2. setup a world event listener with: this.physics.world.on('worldbounds', ...), in the callback you can change the rotation of the sprite
    link to the documentation

Update:
You can use vector-calculation to calculate the rotation

document.body.style = 'margin:0;';

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    physics: {
        default: 'arcade',
        arcade: {            
            gravity: { y: 0 },
        }
    },
    scene: { create }
}; 

function create () {
    this.add.text(10,10, 'DEMO TEXT')
        .setScale(1.5)
        .setOrigin(0)
        .setStyle({fontStyle: 'bold', fontFamily: 'Arial'});

    let graphics  = this.make.graphics();
    graphics.fillStyle(0xffffff);
    graphics.fillTriangle(0, 0, 10, 5, 0, 10);
    graphics.generateTexture('img', 10, 10);
    
    this.photons = this.physics.add.group({
      key: "img",
      repeat: 2,
      setXY: { x: 50, y: 50, stepX: 32 },
    });

    this.photons.children.iterate(function (child) {
      child.body.bounce.set(1);
      child.setVelocity(Phaser.Math.Between(0, 100),30);
      let initialAngle = (new Phaser.Math.Vector2(child.body.velocity)).angle();
      child.setRotation(initialAngle);
      child.body.collideWorldBounds = true;
      child.body.onWorldBounds = true;
    });
    
    this.physics.world.on('worldbounds', (photon) => {
        let newAngle = (new Phaser.Math.Vector2(photon.velocity)).angle();
        photon.gameObject.setRotation(newAngle);
    });
    
    this.physics.add.collider(this.photons, this.photons, (p1, p2) => {
        let newAngle = (new Phaser.Math.Vector2(p1.body.velocity)).angle();
        p1.setRotation(newAngle);
        newAngle = (new Phaser.Math.Vector2(p2.body.velocity)).angle();
        p2.setRotation(newAngle);
    });
}

new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>

Javascript相关问答推荐

积分计算和 colored颜色 判断错误

根据总价格对航班优惠数组进行排序并检索前五个结果- Angular HTTP请求

使用JavaScript在ionic Web应用程序中更新.pane和.view的背景 colored颜色

从实时数据库(Firebase)上的子类别读取数据

使用javascript将Plotly Expandable Sparkline转换为HighCharter Plot在bslib卡中

Msgraph用户邀请自定义邮箱模板

Spring boot JSON解析错误:意外字符错误

为什么promise对js中的错误有一个奇怪的优先级?

为什么useState触发具有相同值的呈现

我可以使用使用node.js创建的本地主机来存储我网站上提交的所有数据吗?没有SQL或任何数据库.只有HTML语言

编辑文本无响应.onClick(扩展脚本)

在WordPress中使用带有WPCode的Java代码片段时出现意外令牌错误

当标题被点击时,如何使内容出现在另一个div上?

当从其他文件创建类实例时,为什么工作线程不工作?

使用API调用的VUE 3键盘输入同步问题

rxjs在每次迭代后更新数组的可观察值

为什么这个最小Angular 的Licial.dev设置不起作用?

如何创建一个for循环,用于计算仪器刻度长度并将其放入一个HTML表中?

用内嵌的含selenium的Java脚本抓取网站

如何在函数组件中保留对计时器的引用