\( \newcommand{\N}{\mathbb{N}} \newcommand{\R}{\mathbb{R}} \newcommand{\C}{\mathbb{C}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\P}{\mathcal P} \newcommand{\B}{\mathcal B} \newcommand{\F}{\mathbb{F}} \newcommand{\E}{\mathcal E} \newcommand{\brac}[1]{\left(#1\right)} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\matrixx}[1]{\begin{bmatrix}#1\end {bmatrix}} \newcommand{\vmatrixx}[1]{\begin{vmatrix} #1\end{vmatrix}} \newcommand{\lims}{\mathop{\overline{\lim}}} \newcommand{\limi}{\mathop{\underline{\lim}}} \newcommand{\limn}{\lim_{n\to\infty}} \newcommand{\limsn}{\lims_{n\to\infty}} \newcommand{\limin}{\limi_{n\to\infty}} \newcommand{\nul}{\mathop{\mathrm{Nul}}} \newcommand{\col}{\mathop{\mathrm{Col}}} \newcommand{\rank}{\mathop{\mathrm{Rank}}} \newcommand{\dis}{\displaystyle} \newcommand{\spann}{\mathop{\mathrm{span}}} \newcommand{\range}{\mathop{\mathrm{range}}} \newcommand{\inner}[1]{\langle #1 \rangle} \newcommand{\innerr}[1]{\left\langle #1 \right \rangle} \newcommand{\ol}[1]{\overline{#1}} \newcommand{\toto}{\rightrightarrows} \newcommand{\upto}{\nearrow} \newcommand{\downto}{\searrow} \newcommand{\qed}{\quad \blacksquare} \newcommand{\tr}{\mathop{\mathrm{tr}}} \newcommand{\bm}{\boldsymbol} \newcommand{\cupp}{\bigcup} \newcommand{\capp}{\bigcap} \newcommand{\sqcupp}{\bigsqcup} \newcommand{\re}{\mathop{\mathrm{Re}}} \newcommand{\im}{\mathop{\mathrm{Im}}} \newcommand{\comma}{\text{,}} \newcommand{\foot}{\text{。}} \)

Sunday, October 13, 2019

phaser and react

Surprisingly phaser and react can work perfectly, for future reference I record the following:

We start off creating a Game component which is almost the place we position our canvas:
export function Game() {
  useEffect(() => {
    const config = {
      type: Phaser.AUTO,
      parent: "phaser-game",
      backgroundColor: 0xedf8f9,
      width: 800,
      height: 600,
      physics: {
        default: "arcade",
        arcade: {
          gravity: {
            y: 0
          }
        }
      },
      scene: [Example1, Example2, Example3, CVScene, TryAgain]
    };
    game = new Phaser.Game(config);

    function resize() {
      let canvas = document.querySelector("canvas");

      let windowWidth = window.innerWidth;
      let windowHeight = window.innerHeight;
      let windowRatio = windowWidth / windowHeight;
      let gameRatio = game.config.width / game.config.height;
      let container = document.getElementById("TopBar");
      container.style.width = 800 + "px";

      if (windowWidth <= 800) {
        if (windowRatio < gameRatio) {
          canvas.style.width = windowWidth + "px";
          canvas.style.height = windowWidth / gameRatio + "px";
          container.style.width = windowWidth + "px";
        } else {
          canvas.style.width = windowHeight * gameRatio + "px";
          canvas.style.height = windowHeight + "px";
          container.style.width = windowHeight * gameRatio + "px";
        }
      }
    }
    resize();
    window.addEventListener("resize", resize, false);
  });

  return <div></div>;
}
We need to enclose our Game component by a div element with id "phaser-game", by doing so our canvas will be built inside there. The resize function make our canvas responsive to the screen width. Also, Example1, Example2, Example3 are different scenes of the form:
import Phaser from "phaser";
export default class Example2 extends Phaser.Scene {
  constructor() {
    super({ key: "Example2" });
  }

  create() {}

  update() {}
}

Next the following are routine way of creating images, text, sprite, animations, etc:
this.load.spritesheet("door", door, {
  frameWidth: 270,
  frameHeight: 306
});

this.add.text(x, y, "text_to_show", { color: "0x00000" });

this.anims.create({
  key: "char2",
  frames: [{ key: "characters", frame: 13 }],
  frameRate: 10
});

this.door2Graphic = this.add
  .graphics()
  .fillStyle(0x00ff00, 0)
  .fillRect(435, 100, 145, 250);

this.tweens.add({
  targets: this.CV_container,
  duration: 500,
  x: { from: 220, to: -1000 },
  ease: "Linear"
});

game.scene.resume("Example1");

No comments:

Post a Comment