将Phaser的精灵旋转到它前进的方向。

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

Phaser change sprite rotation to the direction its going in

问题

我有这个精灵组

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);进行碰撞检测。

编辑:相机代码

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

如何使每个子对象都面向其向量的方向,在每次碰撞时更改?

英文:

I have this sprite group

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

Edit:
the children are colliding with each other by this.physics.add.collider(this.photons, this.photons);

Edit: Camera Code

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

How can I make every child face the direction of their vector, changing every collision?

答案1

得分: 1

这取决于它们应该碰撞的内容。如果只是世界边界,您需要:

  1. 在_photons_上设置属性 onWorldBounds
this.photons.children.iterate(function (child) {
    ...
    child.body.onWorldBounds = true;
});
  1. 使用以下代码设置世界事件监听器:this.physics.world.on('worldbounds', ...)

在回调中,您可以更改精灵的旋转角度。文档链接

更新:
您可以使用矢量计算来计算旋转角度。

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

文档链接

英文:

It depends on what they should be colliding on. if only the world bound, you would have to:

  1. set the property onWorldBounds on the photons

     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

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

document.body.style = &#39;margin:0;&#39;;
var config = {
type: Phaser.AUTO,
width: 536,
height: 183,
physics: {
default: &#39;arcade&#39;,
arcade: {            
gravity: { y: 0 },
}
},
scene: { create }
}; 
function create () {
this.add.text(10,10, &#39;DEMO TEXT&#39;)
.setScale(1.5)
.setOrigin(0)
.setStyle({fontStyle: &#39;bold&#39;, fontFamily: &#39;Arial&#39;});
let graphics  = this.make.graphics();
graphics.fillStyle(0xffffff);
graphics.fillTriangle(0, 0, 10, 5, 0, 10);
graphics.generateTexture(&#39;img&#39;, 10, 10);
this.photons = this.physics.add.group({
key: &quot;img&quot;,
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(&#39;worldbounds&#39;, (photon) =&gt; {
let newAngle = (new Phaser.Math.Vector2(photon.velocity)).angle();
photon.gameObject.setRotation(newAngle);
});
this.physics.add.collider(this.photons, this.photons, (p1, p2) =&gt; {
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);

<!-- language: lang-html -->

&lt;script src=&quot;//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定