Adding Arcade physics to a sprite

Hey !

I’m using Phaser 3 with Arcade Physics and I use tilemaps so to recreate the sprites I use the createFromObjects() function.

The problem is this function returns a list of sprites without physics, the same thing that when you do this.add.sprite() (without .physics) and I need the setVelocityX() function.

So how can I add it to the sprite ?

ennemies = map.createFromObjects("ennemies", "ennemy", {key: "ennemy", frame: 1});
ennemies[i].setVelocityX(-speed);
phaser_tmx.js:73 Uncaught TypeError: ennemies[i].setVelocityX is not a function

You should be able to call this.physics.world.enable(enemies) to give them all physics bodies.

See https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Arcade.World.html#enable__anchor

Since you are setting the enemy’s velocity, that means they should be created with dynamic bodies, which is the default behavior when no second parameter is provided to the enable method.

4 Likes
ennemies = map.createFromObjects("ennemies", "ennemy", {key: "ennemy", frame: 1});
this.physics.world.enable(ennemies);
for (var i = 0; i < ennemies.length; i++) {
ennemies[i].setVelocityX(-speed);
1 Like

I have a similar problem. I’m able to add a body with this.physics.world.enable(this.checkpoints,0); but even after this there is no enableBody or disableBody functions in the Sprite (since it’s still not an ArcadeSprite). I’m trying to make overlap handler that uses disableBody can’t find any way around this annoying problem. Can I make those functions available or is there a workaround to doing disableBody?

        this.physics.add.overlap(this.player, this.checkpoints, function(player:Sprite, checkpoint:Sprite) 
{ checkpoint.disableBody(true); }

scene.physics.world.enable() adds an Arcade Physics Body to your checkpoints and therefore they should have the method disableBody.

I think disableBody is not properly declared in the TypeScript defs. Try to add a ts-ignore comment:

this.physics.add.overlap(this.player, this.checkpoints, (player: Sprite, checkpoint: Sprite) => {
  // @ts-ignore
  checkpoint.disableBody(true)
})

I don’t think there’s any way to make an Arcade.Sprite from createFromObjects(). It only makes ordinary Sprites. You can add bodies to them but they won’t have the Arcade Physics components like enableBody(), disableBody().

You could copy the components you want into those Sprites manually.

Methods like setVelocityX() have an equivalent on the body so you can use that instead.

There’s probably a Phaser way to do this, but

Object.assign(
  sprite,
  Phaser.Physics.Arcade.Components.Enable,
  Phaser.Physics.Arcade.Components.Velocity // etc.
);

Thank you, seems like physics.world.enable should do that copying, but since it doesn’t, I ended looking what disableBody does and just copying the necessary lines to where I need them. Seems to work well.

this.physics.add.overlap(this.player, this.checkpoints, (player: Sprite, checkpoint: Sprite) => {    
  checkpoint.body.enable = false;
  checkpoint.body.gameObject.active = false;
  //checkpoint.body.gameObject.visible = false;
})