prefab-instance-children.rst 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. .. include:: ../_header.rst
  2. Adding children to a prefab instance
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Since v3.35.0 of Phaser Editor 2D you can add children to a prefab instance. Sure, it should be an instance of a Layer or Container prefab.
  5. For backward compatibility, this behavior is disabled by default in prefabs. You should enable it in the `Children properties section <children-properties.html>`_, setting on the **Allow Append Children** parameter. It means you have to checkmark that parameter in a Container or Layer prefab or nested prefab.
  6. This is a handy feature for creating reusable objects. Let's see the next example.
  7. We want to create a reusable dialog object that we can use for showing *Level Complete* dialog or a *Game Paused* dialog.
  8. We create a **DialogPrefab** with a basic structure: a **background** image and a close button. In addition to the structure, it also has a basic behavior: it animates from the bottom of the screen and if you click the **close** button it closes the dialog:
  9. .. image:: ../images/prefab-children-basic-dialog-20221004.webp
  10. :alt: Basic dialog
  11. Some considerations:
  12. * For allowing adding new children to the prefab instances, we should check the **Allow Append Children** parameter we mentioned before.
  13. * Maybe we want to change the texture of the **close** button in prefab instances, so we make it a nested prefab.
  14. Now let's create the **LevelCompleteDialogPrefab**. It is a new prefab that is a variant of the **DialogPrefab**. It means, the ``LevelCompleteDialogPrefab`` class extends the ``DialogPrefab`` class. In this new dialog, we add the **Level Complete** message and three reward stars and change the texture of the **close** button (to green).
  15. .. image:: ../images/prefab-children-level-complete-dialog-20221004.webp
  16. :alt: The Level Complete dialog prefab
  17. Notice the new **levelCompleteRibbon**, and the **star1**, **star2**, and **star3** objects, are children of an instance of the **DialogPrefab** prefab. In previous versions of the editor, for getting a similar structure, you had to wrap a **DialogPrefab** with a new container and add the new objects to it. But now, you can add new children to the same prefab instance and at the same time, it inherits the behaviors of the **DialogPrefab**. It doesn't require any manual delegation of behaviors.
  18. .. image:: ../images/prefab-children-level-complete-dialog-outline-20221004.webp
  19. :alt: Structure of the Complete Level prefab.
  20. As result, we get the following code of the ``LevelCompletePrefab`` class:
  21. .. code::
  22. class LevelCompleteDialogPrefab extends DialogPrefab {
  23. constructor(scene: Phaser.Scene, x?: number, y?: number) {
  24. super(scene, x ?? 400, y ?? 303);
  25. this.closeButton.setTexture("Button Pack - Green_Button Green - Close");
  26. // levelCompleteRibbon
  27. const levelCompleteRibbon = scene.add.image(0, -30, "Casual Game GUI_Attribute - Ribbon Green");
  28. levelCompleteRibbon.scaleX = 0.5;
  29. levelCompleteRibbon.scaleY = 0.5;
  30. this.add(levelCompleteRibbon);
  31. // star1
  32. const star1 = scene.add.image(-89, 81, "Casual Game GUI_Icon - Star Yellow");
  33. star1.scaleX = 0.5;
  34. star1.scaleY = 0.5;
  35. star1.angle = -10;
  36. this.add(star1);
  37. // star2
  38. const star2 = scene.add.image(-2, 71, "Casual Game GUI_Icon - Star Yellow");
  39. star2.scaleX = 0.5;
  40. star2.scaleY = 0.5;
  41. this.add(star2);
  42. // star3
  43. const star3 = scene.add.image(85, 81, "Casual Game GUI_Icon - Star Yellow");
  44. star3.scaleX = 0.5;
  45. star3.scaleY = 0.5;
  46. star3.angle = 10;
  47. this.add(star3);
  48. }
  49. }
  50. Following the same pattern then we can create a **PauseDialogPrefab**:
  51. .. image:: ../images/prefab-children-game-pause-dialog-20221004.webp
  52. :alt: Game paused prefab