简体   繁体   English

Phaser 3 带圆圈的物理组

[英]Phaser 3 physics group with circle

I'm fairly new to Phaser, and am following this tutorial: https://www.codecaptain.io/blog/game-development/shooting-bullets-phaser-3-using-arcade-physics-groups/696我是 Phaser 的新手,正在学习本教程: https://www.codecaptain.io/blog/game-development/shooting-bullets-phaser-3-using-arcade-physics-groups/696

Except, that instead of the Laser being a Sprite , I just want it to be a simple circle, so I tried the following;除了,而不是LaserSprite ,我只是希望它是一个简单的圆圈,所以我尝试了以下;

export class Laser extends Phaser.Physics.Arcade.Body {
    constructor(scene: Phaser.Scene, x: number, y: number) {
        const c = new Phaser.GameObjects.Ellipse(scene, x, y, 25, 25, 0x00ff00);

        super(scene.physics.world, c);
    }
}

This just throws the following error Uncaught TypeError: child.addToDisplayList is not a function .这只会引发以下错误Uncaught TypeError: child.addToDisplayList is not a function

Am I missing something fairly fundamental here?我在这里错过了一些相当基本的东西吗? I can only seem to be able to use Phaser.Physics.Arcade.Sprite or Phaser.Physics.Arcade.Image .我似乎只能使用Phaser.Physics.Arcade.SpritePhaser.Physics.Arcade.Image Is it not possible to just have a physics object that consist of multiple rectangles or circles?难道不可能只有一个由多个矩形或圆形组成的物理 object 吗?

Your code looks good, but you would still have to add the GameObject to the scene and the physics world , for it to work.您的代码看起来不错,但您仍然需要将GameObject添加到scene物理世界,才能使其正常工作。

You would only have to make minor tweaks for it to work.你只需要做一些小的调整就可以工作。

(Updated Code) Here a short executable demo to show case this: (更新代码)这里有一个简短的可执行演示来展示这个案例:

 document.body.style = 'margin:0;'; class Laser extends Phaser.Physics.Arcade.Body { constructor(scene, x, y) { // add object to the scene, so that it can be displayed const c = scene.add.ellipse( x, y, 25, 25, 0x00ff00); // create the actual body super(scene.physics.world, c); // add Body to world scene.physics.add.existing(c) } } class Laser2 extends Phaser.GameObjects.Ellipse { constructor(scene, x, y) { super(scene, x, y, 25, 25, 0xff0000); //give the GameObject a Physics body scene.physics.add.existing(this) } } var config = { type: Phaser.AUTO, width: 536, height: 183, physics: { default: 'arcade', arcade: { gravity:{ y: 100 }, debug: true } }, scene: { create }, banner: false }; function create () { this.add.text(10,10, 'Falling balls- red are from a Group').setScale(1.5).setOrigin(0).setStyle({fontStyle: 'bold', fontFamily: 'Arial'}); // create a laser let laser = new Laser(this, 50, 50) // create second laser let group = this.physics.add.group({ classType: Laser2}); group.create(150, 0) group.create(170, -100) group.create(180, -50) } new Phaser.Game(config);
 <script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM