Jump to content

Add matter physics body to GameObjects.Sprite


PlaninJamin
 Share

Recommended Posts

Hey all, I want to add a matter physics body to a Phaser sprite. I'm not too familiar with ES6, but I want to use this code, which is used to add an Arcade physics body to a sprite

export default class Actor extends Phaser.GameObjects.Sprite {
  constructor(config) {
    super(config.scene, config.x, config.y, config.key);
    config.scene.physics.world.enable(this);
    config.scene.add.existing(this);
  }
}

 

and do the equivalent using a Matter physics body type

export default class Actor extends Phaser.GameObjects.Sprite {
  constructor(config) {
    super(config.scene, config.x, config.y, config.key);
    // config.scene.matter.world.enable(this);
    config.scene.add.existing(this);
    console.log(this.body);

    // this.body.isStatic = true;
  }

}

then declare it in my main phaser file (or any other file). (*config.scene.matter.world.enable(this) doesn't work for matter physics, but will compile without this line*)

import 'phaser';
import Actor from './Actor';

var actor;

function create () {
  actor = new Actor ({
    scene: this,
    key: 'logo',
    x: 500,
    y: 500
  });
}

 

But when I attempt to manipulate the program crashes out with an error, and the Physics body type returns null, even though a matter physics body is still created. So how can I add a matter physics body to a Phaser sprite? thanks in advance :)

Link to comment
Share on other sites

doesn't compile. Phaser.GameObjects.Sprite still creates an body, but Matter.Sprite is undefined

Uncaught TypeError: Cannot read property 'queueDepthSort' of undefined
    at Actor.GameObject [as constructor] (project.bundle.js:521)
    at new MatterSprite (project.bundle.js:91467)
    at new Actor (project.bundle.js:160577)
    at Scene.create (project.bundle.js:109729)
    at SceneManager.create (project.bundle.js:66916)
    at SceneManager.loadComplete (project.bundle.js:66810)
    at LoaderPlugin.emit (project.bundle.js:2680)
    at LoaderPlugin.loadComplete (project.bundle.js:146302)
    at LoaderPlugin.fileProcessComplete (project.bundle.js:146258)
    at ImageFile.onProcessComplete (project.bundle.js:4574)

 

Link to comment
Share on other sites

  • 4 months later...
  • 1 month later...

It's actually simple for matter or impact, you just need to pass world instead of scene.

super(scene.matter.world, x, y, sprite, frame, option); // if you are using matter
// OR
super(scene.impact.world, x, y, sprite, frame); // if you are using impact

But if you are using Arcade, that creates a problem. you can't just extend Phaser.Physics.Arcade.Sprite.  You have to add this line of code.

scene.physics.world.enableBody(this, 0);

P.S. To solve this kind of problem I created a development setup to custom create classes. IF you like you can check out my setup at github.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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