# Adding Arcade physics to a sprite

**URL:** <https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472>\
**Category:** Phaser 3\
**Created:** [December 25, 2018, 11:14pm UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472 "2018-12-25T23:14:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheDevKiller](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheDevKiller](https://phaser.discourse.group/u/TheDevKiller)\
**Post date:** [December 25, 2018, 11:14pm UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/1 "2018-12-25T23:14:08Z")

</div>

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 ?

```javascript
ennemies = map.createFromObjects("ennemies", "ennemy", {key: "ennemy", frame: 1});
ennemies[i].setVelocityX(-speed);

```

```javascript
phaser_tmx.js:73 Uncaught TypeError: ennemies[i].setVelocityX is not a function

```

---

<div class="post-metadata">

**Author:** ![snowbillr](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/snowbillr/32/332_2.png) [@snowbillr](https://phaser.discourse.group/u/snowbillr)\
**Post date:** [December 26, 2018, 4:15am UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/2 "2018-12-26T04:15:53Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![TheDevKiller](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheDevKiller](https://phaser.discourse.group/u/TheDevKiller)\
**Post date:** [December 26, 2018, 4:02pm UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/3 "2018-12-26T16:02:49Z")

</div>

```javascript
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);
```

---

<div class="post-metadata">

**Author:** ![Mikko\_Torniainen](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/mikko_torniainen/32/682_2.png) [@Mikko\_Torniainen](https://phaser.discourse.group/u/Mikko_Torniainen)\
**Post date:** [February 17, 2019, 11:18pm UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/4 "2019-02-17T23:18:58Z")

</div>

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); }
```

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yannick/32/732_2.png) [@yannick](https://phaser.discourse.group/u/yannick)\
**Post date:** [February 18, 2019, 1:18am UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/5 "2019-02-18T01:18:18Z")

</div>

`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:

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

```

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 18, 2019, 6:44am UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/6 "2019-02-18T06:44:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 18, 2019, 6:47am UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/7 "2019-02-18T06:47:25Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Mikko\_Torniainen](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/mikko_torniainen/32/682_2.png) [@Mikko\_Torniainen](https://phaser.discourse.group/u/Mikko_Torniainen)\
**Post date:** [February 18, 2019, 8:31am UTC](https://phaser.discourse.group/t/adding-arcade-physics-to-a-sprite/472/8 "2019-02-18T08:31:50Z")

</div>

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;
})
```
