Jump to content

Interacting with Phaser from React


returnKitten
 Share

Recommended Posts

I've been able to successfully initialize a game in my React project using:

    const game = new Phaser.Game({
      type: Phaser.WEBGL,
      //backgroundColor: 0x000000,
      pixelArt: false,
      transparent: true,
      scale: {
          mode: Phaser.Scale.FIT,
          parent: 'engine',
          autoCenter: Phaser.Scale.CENTER_BOTH,
          width: 1200,
          height: 800
      },
      physics: {
          default: 'matter',
          matter: {
              gravity: {
                  y: -1
              },
              debug: true
          }
      },
      scene: {
        preload: this.preload,
        create: this.create,
        update: this.update
      }
    });

But I'm having trouble interacting with it from React app. I initialize a websocket connection in useEffect() and want to add a body to my game every time that websocket connection receives data. I've looked into EventEmitter, but the documentation/examples on EventEmitters don't illustrate any case where the events are being sent from 'outside' Phaser.

From what I understand, there is a "master" EventEmitter which I get from the game object and another EventEmitter connected to the scene. What's the appropriate way to access the EventEmitter connected to my scene using the game object I receive from `new Phaser.Game()`?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...