Phaser dashed line

Draw a Dashed line in Phaser

by 201flaviosilva

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.55.2/phaser.min.js" integrity="sha512-kg6fSrg6FkD9Ua02PzuA289KatVkTp6HdiFrSKwnz1bKBeKe5JG54rNeYZeghYO/I4ka49rb9H/9Ezcq9Se3iQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

CSS

canvas {
  border: 1px solod red;
}

JavaScript

class Dashed extends Phaser.GameObjects.Graphics {
  constructor(scene) {
    const options = {
      lineStyle: {
        width: 5,
        color: 0x00ff00,
        alpha: 1
      },
    };
    super(scene, options);
    scene.add.existing(this);

    this.beginPath();
    // this.setLineDash([5, 1]);
    this.lineTo(0, 0);
    this.lineTo(400, 300);
    this.strokePath();
  }
}

class GameScene extends Phaser.Scene {
  constructor() {
    super("GameScene");
  }

  create() {
    this.dashed = new Dashed(this);
  }
}

const config = {
  type: Phaser.Auto,
  width: 400,
  height: 300,
  scene: [GameScene]
};

new Phaser.Game(config);