Trouble adding matter physics to a Sprite GameObject

I am trying to add matter physics to a Sprite GameObject using

const testSprite = this.add.sprite(516, 114, "test", 3);
this.testSprite = testSprite
this.matter.add.gameObject(this.testSprite);

but it doesn’t have any physics properties after adding matter physics to it. When I try to use setVelocityX method in sprite’s body, it isn’t working.

this.testSprite.body.setVelocityX(-160); is giving me the following error:

TS2339: Property 'setVelocityX' does not exist on type 'Body | StaticBody | BodyType'.
  Property 'setVelocityX' does not exist on type 'StaticBody'

However, I can see the sprite’s body in the matter objects when I do this.matter.getMatterBodies(); . It just can’t seem to use or find the physics properties from this.testSprite.body. Any help is appreciated.

I think there is a this.testSprite.body but no setVelocityX() method there. (There is such a method in Arcade Physics.)

You probably want to use instead:

const testSprite = this.matter.add.sprite(516, 114, "test", 3);

this.testSprite.setVelocityX(-160);

Thx. I was using the Phaser Editor app which doesn’t support creating physics objects that’s why I wanted just to add matter physics using this.matter.add.gameObject. Doesn’t setVelocityX also exist in Matter Physics? At least that’s what I see on API doc setVelocity - Phaser 3 API Documentation (beta)

If not, I can do it manually as you suggested, just wanted to make this work for convenience and to make use of the editor.

There’s no setVelocityX() method on a Matter Physics body, only on Matter.Image and Matter.Sprite. And matter.add.gameObject() adds a physics body but not any methods.

1 Like