1.2.6 • Published 4 years ago

phaser3-react v1.2.6

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

Installation

This plugin requires you to have react react-dom and phaser installed aswell

npm

npm install -S phaser3-react react react-dom phaser

yarn

yarn add phaser3-react react react-dom phaser

Usage

Setup

First let us import the package and add the config to your game configs global plugins

we require you have the dom.createContainer property set to true, which requires you to have a parent set in your div.

import phaserReact from "phaser3-react";

const config = {
  parent: 'your games parent divs id',
  dom: { createContainer: true },
  plugins: {
    global: [
      {
        key: 'phaser-react',
        plugin: phaserReact,
        start: true
      }
    ]
  }
}

Mounting the React Component

The reactDom method is attached to the add property of our Phaser Scene, and takes 2 arguments (component and the supplied state) and returns a manager for the component (shown later).

The state is given to your component via props. (example later)

Class Game extends Scene {
  // .....
  create() {
    this.scoreText = this.add.reactDom(ScoreComponent, { score: 0 });
  }
  // .....
}

Setting State from the Phaser scene

It's as simple as calling setState from phaser.

// .....

increaseScore() {
  let newScore = this.scoreText.state.score + 10;
  this.scoreText.setState({ score: newScore })
}

// .....

what methods and properties are on the manager? (I need to make some docs)

properties

state: the state object you've passed (incase you need the values for anything) not set from react.

events: an event emitter system you can shoot events into and listen to in react or phaser if you need to (or other things).

Methods

setState(Object); takes and updaates the eternal state which is passed to your component via props.

destroy(); removes this react component from rendering.

react component example

import React from 'react';

// the things passed to setState() is passed to your component as props, it also recieves the manager.
function Score({ score }) {
  return (
    <p style={{ position: 'absolute', top: '10px', left: '10px' }}>
      Score: {score}
    </p>
  )
}

export default Score;
1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.0.19

4 years ago

1.1.0

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.11

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago