Component class Null safety

Components are the basic building blocks for a FlameGame.

Components are quite similar to widgets in Flutter, or to GameObjects in Unity. Any entity within the game can be represented as a Component, especially if that entity has some visual appearance, or if it changes over time. For example, the player, the enemies, flying bullets, clouds in the sky, buildings, rocks, etc -- all can be represented as components. Some components may also represent more abstract entities: effects, behaviors, data stores, groups.

Components are designed to be organized into a component tree, where every component belongs to a single parent and owns any number of children components. A Component must be added to the component tree in order for it to become fully operational. Typically, the FlameGame is the root of the component tree.

The components are added into the component tree using the add method, or addToParent; and then later can be removed using remove or removeFromParent. Note that neither adding nor removing are immediate, typically these operations complete by the next game tick.

Each component goes through several lifecycle stages during its lifetime, at each stage invoking certain user-defined "lifecycle methods":

  • onGameResize when the component is first added into the tree;
  • onLoad immediately after;
  • onMount when done loading;
  • onRemove if the component is later removed from the component tree.

The onLoad is only invoked once during the lifetime of the component, which means you can treat it as "async constructor". When onLoad is invoked, we guarantee that the game instance can be found via findGame, and that this game instance already has layout (i.e. knows the size of the canvas).

The onMount is invoked when the component is done loading, and when its parent is properly mounted. If a component is removed from the tree and later added to another component, the onMount will be called again. For every call to onMount there will be a corresponding call to onRemove.

While the component is mounted, the following user-overridable methods are invoked:

  • update on every game tick;
  • render after all components are done updating;
  • updateTree and renderTree are more advanced versions of the update and render callbacks, but they rarely need to be overridden by the user;
  • onGameResize every time the size game's Flutter widget changes.

The update and render are two most important methods of the component's lifecycle. On every game tick, first the entire component tree will be updated, and then all the components will be rendered.

You may also need to override containsLocalPoint if the component needs to respond to tap events or similar; the componentsAtPoint may also need to be overridden if you have reimplemented renderTree.

Implementers

Constructors

Component({Iterable<Component>? children, int? priority})

Properties

children ComponentSet
The children components of this component.
read-only
debugColor Color
The color that the debug output should be rendered with.
read / write
debugCoordinatesPrecision int?
How many decimal digits to print when displaying coordinates in the debug mode. Setting this to null will suppress all coordinates from the output.
read-only
debugMode bool
Returns whether this Component is in debug mode or not. When a child is added to the Component it gets the same debugMode as its parent has when it is prepared.
read / write
debugPaint Paint
The debugColor represented as a Paint object.
read-only
debugTextPaint TextPaint
Returns a TextPaint object with the debugColor set as color for the text.
read-only
hasChildren bool
read-only
hashCode int
The hash code for this object.
read-only, inherited
hasPendingLifecycleEvents bool
read-only
isLoaded bool
Whether this component has completed its onLoad step.
read-only
isLoading bool
Whether the component is currently executing its onLoad step.
read-only
isMounted bool
Whether this component is currently added to a component tree.
read-only
isRemoving bool
Whether the component is scheduled to be removed.
read-only
lifecycle → _LifecycleManager
@protected, read-only
loaded Future<void>
A future that completes when this component finishes loading.
read-only
mounted Future<void>
A future that will complete once the component is mounted on its parent.
read-only
parent Component?
Who owns this component in the component tree.
read / write
positionType PositionType
What coordinate system this component should respect (i.e. should it observe camera, viewport, or use the raw canvas).
read / write
priority int
Render priority of this component. This allows you to control the order in which your components are rendered.
read / write
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

add(Component component) Future<void>?
Schedules component to be added as a child to this component.
addAll(Iterable<Component> components) Future<void>
A convenience method to add multiple children at once.
addToParent(Component parent) Future<void>?
Adds this component as a child of parent (see add for details).
ancestors({bool includeSelf = false}) Iterable<Component>
An iterator producing this component's parent, then its parent's parent, then the great-grand-parent, and so on, until it reaches a component without a parent.
changeParent(Component newParent) → void
Changes the current parent for another parent and prepares the tree under the new root.
changePriorityWithoutResorting(int priority) → void
Usually this is not something that the user would want to call since the component list isn't re-ordered when it is called. See FlameGame.changePriority instead.
componentsAtPoint(Vector2 point, [List<Vector2>? nestedPoints]) Iterable<Component>
An iterable of descendant components intersecting the given point. The point is in the local coordinate space.
contains(Component c) bool
Whether the children list contains the given component.
containsLocalPoint(Vector2 point) bool
Checks whether the point is within this component's bounds.
containsPoint(Vector2 point) bool
Same as containsLocalPoint, but for a "global" point.
createComponentSet() ComponentSet
This method creates the children container for the current component. Override this method if you need to have a custom ComponentSet within a particular class.
descendants({bool includeSelf = false, bool reversed = false}) Iterable<Component>
Recursively enumerates all nested children.
eventPosition(PositionInfo info) Vector2
findGame() Game?
findParent<T extends Component>() → T?
Returns the closest parent further up the hierarchy that satisfies type=T, or null if no such parent can be found.
firstChild<T extends Component>() → T?
Returns the first child that matches the given type T, or null if there are no such children.
handleResize(Vector2 size) → void
lastChild<T extends Component>() → T?
Returns the last child that matches the given type T, or null if there are no such children.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
onGameResize(Vector2 size) → void
Called whenever the size of the top-level Canvas changes.
onLoad() Future<void>?
Late initialization method for Component.
onMount() → void
Called when the component is added to its parent.
onRemove() → void
Called right before the component is removed from the game.
processPendingLifecycleEvents() → void
Attempt to resolve any pending lifecycle events on this component.
propagateToChildren<T extends Component>(bool handler(T), {bool includeSelf = false}) bool
This method first calls the passed handler on the leaves in the tree, the children without any children of their own. Then it continues through all other children. The propagation continues until the handler returns false, which means "do not continue", or when the handler has been called with all children.
remove(Component component) → void
Removes a component from the component tree.
removeAll(Iterable<Component> components) → void
Removes all the children in the list and calls onRemove for all of them and their children.
removeFromParent() → void
Remove the component from its parent in the next tick.
render(Canvas canvas) → void
renderDebugMode(Canvas canvas) → void
renderTree(Canvas canvas) → void
reorderChildren() → void
Call this if any of this component's children priorities have changed at runtime.
setMounted() → void
toString() String
A string representation of this object.
inherited
update(double dt) → void
This method is called periodically by the game engine to request that your component updates itself.
updateTree(double dt) → void
This method traverses the component tree and calls update on all its children according to their priority order, relative to the priority of the direct siblings, not the children or the ancestors.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

childrenFactory ComponentSetFactory
Component.childrenFactory is the default method for creating children containers within all components. Replace this method if you want to have customized (non-default) ComponentSet instances in your project.
read / write
staticGameInstance Game?
@internal, read / write