# Change direction of group sprites based on condition

**URL:** <https://phaser.discourse.group/t/change-direction-of-group-sprites-based-on-condition/11118>\
**Category:** Phaser 3\
**Created:** [January 26, 2022, 11:07am UTC](https://phaser.discourse.group/t/change-direction-of-group-sprites-based-on-condition/11118 "2022-01-26T11:07:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ppathak35](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ppathak35/32/6142_2.png) [@ppathak35](https://phaser.discourse.group/u/ppathak35)\
**Post date:** [January 26, 2022, 11:07am UTC](https://phaser.discourse.group/t/change-direction-of-group-sprites-based-on-condition/11118/1 "2022-01-26T11:07:04Z")

</div>

hello everyone. I’m a beginner in phaser 3 and i tried doing this

```javascript
class Bee extends Phaser.GameObjects.Sprite {
	constructor(config) {
		super(config.scene, config.x, config.y, 'bee');
		config.scene.add.existing(this);
		config.scene.physics.add.existing(this);

		this.initial = [config.x, config.y];
		this.x = config.x;
		this.y = config.y;
		this.speed = 5;
	}

	preUpdate() {
		if (this.y < this.initial[1] - 100) {
			this.speed *= -1;
		}
		if (this.y > this.initial[1] + 100) {
			this.speed *= -1;
		}

		this.y += this.speed;
		this.setY(this.y);
	}
}

```

Basically i want a bee object to go 100 px down and then go up and then keep doing the same. But it doesn’t seems to work.

in my scene i have created a object like this

```javascript
bees = this.physics.add.group();

let bee = new Bee({scene:scene, x: 100, y:200})
bee.setScale(0.5);
bee.allowGravity = false;
bees.add(bee);

```

---

<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:** [January 26, 2022, 4:20pm UTC](https://phaser.discourse.group/t/change-direction-of-group-sprites-based-on-condition/11118/2 "2022-01-26T16:20:19Z")

</div>

What happens?

---

<div class="post-metadata">

**Author:** ![ppathak35](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ppathak35/32/6142_2.png) [@ppathak35](https://phaser.discourse.group/u/ppathak35)\
**Post date:** [January 26, 2022, 5:01pm UTC](https://phaser.discourse.group/t/change-direction-of-group-sprites-based-on-condition/11118/3 "2022-01-26T17:01:04Z")

</div>

it works once, but then the bee sprite just falls down permanently.

---

<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:** [January 26, 2022, 5:08pm UTC](https://phaser.discourse.group/t/change-direction-of-group-sprites-based-on-condition/11118/4 "2022-01-26T17:08:48Z")

</div>

Change to

```javascript
bees.add(bee);
bee.body.allowGravity = false;

```
