Git Product home page Git Product logo

phaser-react's Introduction

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;

phaser-react's People

Contributors

kingcosmic avatar mcfarlanedev avatar dependabot[bot] avatar vsolovev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.