user-components-start-update-methods.rst 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. .. include:: ../_header.rst
  2. Implementing behaviors with the Phaser events
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Many |UserComponents|_ you will create will implement certain behavior of an object. This behavior should be initialized and later updated at every step of the game loop.
  5. Game engines like `Unity <https://unity.com>`_, that support a similar design pattern, `use a Start and Update methods <https://docs.unity3d.com/Manual/CreatingAndUsingScripts.html>`_ to implement the behavior of the component.
  6. The Phaser_ framework does not provide a similar approach, components are not part of the Phaser_ API. However, Phaser_ provides a lot of events that you can handle and implement the gameplay. For example, you can simulate the Unity scripting using the |UserComponents|_ and the Phaser_ events. You can create an **EventComponent** class that handles the events:
  7. .. code::
  8. class EventComponent {
  9. /**
  10. * @param {Phaser.GameObjects.GameObject} gameObject
  11. */
  12. constructor(gameObject) {
  13. this.scene = gameObject.scene;
  14. // first time the scene is updated, call the `start` method
  15. this.scene.events.once(Phaser.Scenes.Events.UPDATE, this.start, this);
  16. // each time the scene is updated, call the `update` method
  17. this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
  18. // if the object is destroyed, call the `destroy` method
  19. gameObject.on(Phaser.GameObjects.Events.DESTROY, this.destroy, this);
  20. }
  21. start() {
  22. // to be overridden in derived classes
  23. }
  24. update() {
  25. // to be overridden in derived classes
  26. }
  27. destroy() {
  28. // the object is destroyed, so we remove all the event handlers
  29. this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.start, this);
  30. this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
  31. }
  32. }
  33. Then, you can set the **EventComponent** class as super class to all your components:
  34. .. image:: ../images/scene-editor-user-components-super-class-07302020.webp
  35. :alt: Setting a super class to components.
  36. Now, you can override the **start**, **update**, and **destroy** methods in the component classes.
  37. .. code::
  38. class HorizontalMove extends EventComponent {
  39. ...
  40. update() {
  41. this.gameObject.x += this.deltaX;
  42. }
  43. }
  44. The ``EventComponent`` class is just an example of what you can do. It has a weak point: not all components need to listen to all the events. It is a waste of resources adding scene listeners in all the cases.
  45. Since v3.13.0, the editor can "write" for you a base class that could be a better solution. However, you can change its code and adapt it to your specific domain. Check the next section for more details.