ruk·si

🛸 Phaser
Game Objects

Updated at 2024-04-17 07:51

GameObject is the main low-level building block of a Phaser game. Everything visual or intractable, from images and shapes to particle effects and videos are game objects.

Each game object implements one or more "components". The most common component is transform which means that the object has some position and rotation in the game world.

https://github.com/phaserjs/phaser/tree/master/src/gameobjects/components

Each game object belongs to a single scene. A game object can belong to one or more collections.

You can override the following methods in a game object:

constructor(scene, x, y, ...) // remember to call `super.*`
addedToScene()
preUpdate(time, delta)
update(time, delta)
removedFromScene()
destroy(fromScene)            -- remember to call `super.*`

Group is a pool collection.

  • Nonexclusive membership
  • Can disable or enable all children in one-go.
  • Can hide or show all children in one-go.
  • Can remove all children in one-go.
  • Allows reusing disabled game objects to conserve resources with e.g. get().

https://github.com/phaserjs/examples/blob/master/public/src/game%20objects/group/sprite%20pool.js

Layer is a visual collection. Layer doesn't have a position or size, and doesn't manage member transforms or physics, but only alpha, blend mode, visibility, depth, masking and postprocessing effects. It can be more efficient to apply effects on a layer of objects instead of individual game objects, but layers are also nice simply for organization.

https://github.com/phaserjs/examples/blob/master/public/src/game%20objects/layer/layer%20post%20pipeline.js

Container is a transform collection.

  • Exclusive membership
  • Children move relative to the container.
  • Children are rendered when the container is rendered.
  • Containers can be nested.
  • Containers have more overhead than the other collections.

https://github.com/phaserjs/examples/blob/master/public/src/game%20objects/container/create%20container.js

Containers can be added to Layers, but Layers cannot be added to Containers.

Phaser.Actions has collection actions.

https://github.com/phaserjs/phaser/tree/master/src/actions

make vs add

scene.make is the GameObjectCreator. It creates and returns game object instances, but it doesn't add them to the display or update list.

scene.add is the GameObjectFactory. It creates, adds and returns the created game object which is added to the display or update lists.

Sources