# \[Phaser 3\] Spaceinvaders game, please feedback

**URL:** <https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155>\
**Category:** Showcase\
**Created:** [February 9, 2019, 10:08pm UTC](https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155 "2019-02-09T22:08:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bit33](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@bit33](https://phaser.discourse.group/u/bit33)\
**Post date:** [February 9, 2019, 10:08pm UTC](https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155/1 "2019-02-09T22:08:24Z")

</div>

Hi,

Made my first Phaser 3 game: Spaceinvaders. I am fairly new to Javascript and new to Phaser. I really like to have some feedback on:

- API usage
- best practices
- codding standards
- tips

Play: [here](https://bit33.io/spaceinvaders/)  
Source: [github](https://github.com/bit33/spaceinvaders)

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [February 10, 2019, 5:14am UTC](https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155/2 "2019-02-10T05:14:05Z")

</div>

Very smooth. I like it. Level 3 is already very hard 🙂  
As for coding, maybe add some comments, and try to keep your files small. GameScene.js has a lot going on, easy to lose the overview…

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [February 10, 2019, 12:03pm UTC](https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155/3 "2019-02-10T12:03:10Z")

</div>

Hi @bit33,  
After seeing your code, there are two things that in my humble opinion could be improved (GameScene.js):

1. I think you can improve performance by using [pools](http://labs.phaser.io/index.html?dir=pools/&q=) instead of repeatedly creating and destroying objects. This prevents the garbage collector from running during the game as little as possible.  
In your case I mean the aliens and the bullets.  
fireBullet() creates a new bullet in each shot and alienHitEvent() destroys a bullet and an alien in each hit.
2. Something similar to the previous.  
The update() executes, through handleCursor(), the CreateCursorKeys() method, which creates an object in each frame (30/60 fps).  
You can create the object “cursors” only once in the function create() and make it accessible to the other functions of the scene:  
`this.cursors = this.input.keyboard.createCursorKeys();`

In more complex games these changes would be more relevant.  
Congratulations for your first game with Phaser !! 👍.

Regards.

---

<div class="post-metadata">

**Author:** ![bit33](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@bit33](https://phaser.discourse.group/u/bit33)\
**Post date:** [February 11, 2019, 11:36am UTC](https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155/4 "2019-02-11T11:36:30Z")

</div>

Thanks [jjcapellan](https://phaser.discourse.group/u/jjcapellan),

I tried to refactor my code to use pools/groups for my gameobjects. But I can not make it behave the same way as it does when I still used .destroy(). In stead of destroy I now use:

alien.setActive(false);  
alien.setVisible(false);

But the other active aliens will still bumb into the old one and don’t move properly. So I tried moving them offScreen:

aliens.setPosition(2000,2000);

But now they end up in the bottom corner and still get velocity when I call group.setVelocityX(20);

So what is the best way to replace the alien.destroy() call then using pools?

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [February 11, 2019, 2:49pm UTC](https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155/5 "2019-02-11T14:49:12Z")

</div>

The aliens group elements have a body, so you should use [disableBody](https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Arcade.Sprite.html#disableBody__anchor) :

```javascript
alien.disableBody (true, true);

```

This is equivalent to:

```javascript
alien.body.enable = false;
alien.setActive (false);
alien.setVisible (false);

```

For the same reason, setPosition() produces unexpected results. An appropriate method would be [body.reset](https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Arcade.Body.html#reset__anchor):

```javascript
alien.body.reset (x, y);

```

Good luck !!

---

<div class="post-metadata">

**Author:** ![bit33](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@bit33](https://phaser.discourse.group/u/bit33)\
**Post date:** [February 11, 2019, 11:40pm UTC](https://phaser.discourse.group/t/phaser-3-spaceinvaders-game-please-feedback/1155/6 "2019-02-11T23:40:08Z")

</div>

Thanks [jjcapellan](https://phaser.discourse.group/u/jjcapellan),

that was just what I needed. Have refactored the code, now it recycles all gameobjects, extracted some classes out of main game scene to make the scene code more compact and comprehensive.
