PositionComponent class

A Component implementation that represents an object that can be freely moved around the screen, rotated, and scaled.

The PositionComponent class has no visual representation of its own (except in debug mode). It is common, therefore, to derive from this class, implementing a specific rendering logic. For example:

class MyCircle extends PositionComponent {
  MyCircle({required double radius, Paint? paint, Vector2? position})
    : _radius = radius,
      _paint = paint ?? Paint()..color=Color(0xFF80C080),
      super(
        position: position,
        size: Vector2.all(2 * radius),
        anchor: Anchor.center,
      );

  double _radius;
  Paint _paint;

  @override
  void render(Canvas canvas) {
    super.render(canvas);
    canvas.drawCircle(Offset(_radius, _radius), _radius, _paint);
  }
}

The base PositionComponent class can also be used as a container for several other components. In this case, changing the position, rotating or scaling the PositionComponent will affect the whole group as if it was a single entity.

The main properties of this class is the transform (which combines the position, angle of rotation, and scale), the size, and the anchor. Thus, the PositionComponent can be imagined as an abstract picture whose of a certain size. Within that picture a point at location anchor is selected, and that point is designated as a "logical center" of the picture. Then, a sequence of transforms is applied: the picture is moved so that the anchor becomes at point position on the screen, then the picture is rotated for angle radians around the anchor point, and finally scaled by scale also around the anchor point.

The size property of the PositionComponent is used primarily for tap and collision detection. Thus, the size should be set equal to the approximate bounding rectangle of the rendered picture. If you do not specify the size of a PositionComponent, then it will be equal to zero and the component won't be able to respond to taps.

Inheritance
Implemented types
Implementers
Available extensions

Constructors

PositionComponent({Vector2? position, Vector2? size, Vector2? scale, double? angle, Anchor? anchor, int? priority})

Properties

absoluteAngle double
The resulting angle after all the ancestors and the components own angle has been applied.
no setter
absoluteCenter Vector2
The absolute center of the component.
no setter
absolutePosition Vector2
The anchor's position in absolute (world) coordinates.
no setter
absoluteScale Vector2
The resulting scale after all the ancestors and the components own scale has been applied.
no setter
absoluteScaledSize Vector2
The resulting size after all the ancestors and the components own scale has been applied.
no setter
absoluteTopLeftPosition Vector2
The absolute top left position regardless of whether it is a child or not.
no setter
anchor Anchor
Anchor point for this component. An anchor point describes a point within the rectangle of size size. This point is considered to be the logical "center" of the component. This can be visualized as the point where Flame "grabs" the component. All transforms occur around this point: the position is where the anchor point will end up after the component is translated; the rotation and scaling also happen around this anchor point.
getter/setter pair
angle double
Rotation angle (in radians) of the component. The component will be rotated around its anchor point in the clockwise direction if the angle is positive, or counterclockwise if the angle is negative.
getter/setter pair
center Vector2
The position of the center of the component's bounding rectangle in the parent's coordinates.
getter/setter pair
children ComponentSet
The children of the current component.
no setterinherited
debugColor Color
The color that the debug output should be rendered with.
getter/setter pairinherited
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.
no setterinherited
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.
getter/setter pairinherited
debugPaint Paint
The debugColor represented as a Paint object.
no setterinherited
debugTextPaint TextPaint
Returns a TextPaint object with the debugColor set as color for the text.
no setterinherited
hasChildren bool
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
hasPendingLifecycleEvents bool
no setterinherited
height double
The height of the component in local coordinates. Note that the object may visually appear larger or smaller due to application of scale.
getter/setter pair
isLoaded bool
Whether this component has completed its onLoad step.
no setterinherited
isMounted bool
Whether this component is currently added to a component tree.
no setterinherited
lifecycle → _LifecycleManager
no setterinherited
loaded Future<void>
A future that will complete once this component has finished loading.
no setterinherited
mounted Future<void>
A future that will complete once the component is mounted on its parent
no setterinherited
parent Component?
The current parent of the component, or null if there is none.
no setterinherited
position ↔ NotifyingVector2
The position of this component's anchor on the screen.
getter/setter pairoverride
positionType PositionType
What coordinate system this component should respect (i.e. should it observe camera, viewport, or use the raw canvas).
getter/setter pairinherited
priority int
Render priority of this component. This allows you to control the order in which your components are rendered.
getter/setter pairinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scale ↔ NotifyingVector2
The scale factor of this component. The scale can be different along the X and Y dimensions. A scale greater than 1 makes the component bigger, and less than 1 smaller. The scale can also be negative, which results in a mirror reflection along the corresponding axis.
getter/setter pair
scaledSize Vector2
The "physical" size of the component. This is the size of the component as seen from the parent's perspective, and it is equal to size * scale. This is a computed property and cannot be modified by the user.
no setter
shouldRemove bool
Whether this component should be removed or not.
getter/setter pairinherited
size ↔ NotifyingVector2
The logical size of the component. The game assumes that this is the approximate size of the object that will be drawn on the screen. This size will therefore be used for collision detection and tap handling.
getter/setter pair
topLeftPosition Vector2
The top-left corner's position in the parent's coordinates.
getter/setter pair
transform → Transform2D
final
transformMatrix Matrix4
The total transformation matrix for the component. This matrix combines translation, rotation and scale transforms into a single entity. The matrix is cached and gets recalculated only as necessary.
no setter
width double
The width of the component in local coordinates. Note that the object may visually appear larger or smaller due to application of scale.
getter/setter pair
x double
X position of this component's anchor on the screen.
getter/setter pair
y double
Y position of this component's anchor on the screen.
getter/setter pair

Methods

absolutePositionOf(Vector2 point) Vector2
Convert local coordinates of a point point inside the component into the global (world) coordinate space.
absolutePositionOfAnchor(Anchor anchor) Vector2
Similar to absolutePositionOf(), but applies to any anchor point within the component.
absoluteToLocal(Vector2 point) Vector2
Transform point from the global (world) coordinate space into the local coordinates. This function is the inverse of absolutePositionOf().
add(Component component) Future<void>?
Schedules component to be added as a child to this component.
inherited
addAll(Iterable<Component> components) Future<void>
A convenience method to add multiple children at once.
inherited
addToParent(Component parent) Future<void>?
Adds this component to the provided parent (see add for details).
inherited
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.
inherited
changeParent(Component newParent) → void
Changes the current parent for another parent and prepares the tree under the new root.
inherited
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.
inherited
contains(Component c) bool
Whether the children list contains the given component.
inherited
containsPoint(Vector2 point) bool
Test whether the point (given in global coordinates) lies within this component. The top and the left borders of the component are inclusive, while the bottom and the right borders are exclusive.
override
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.
inherited
descendants({bool includeSelf = false, bool reversed = false}) Iterable<Component>
Recursively enumerates all nested children.
inherited
distance(PositionComponent other) double
Measure the distance (in parent's coordinate space) between this component's anchor and the other component's anchor.
eventPosition(PositionInfo info) Vector2
inherited
findGame() → Game?
inherited
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.
inherited
firstChild<T extends Component>() → T?
Returns the first child that matches the given type T.
inherited
flipHorizontally() → void
Flip the component horizontally around its anchor point.
flipHorizontallyAroundCenter() → void
Flip the component horizontally around its center line.
flipVertically() → void
Flip the component vertically around its anchor point.
flipVerticallyAroundCenter() → void
Flip the component vertically around its center line.
handleResize(Vector2 size) → void
inherited
lastChild<T extends Component>() → T?
Returns the last child that matches the given type T.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onGameResize(Vector2 size) → void
Called whenever the size of the top-level Canvas changes.
inherited
onLoad() Future<void>?
Late initialization method for Component.
inherited
onMount() → void
Called when the component is added to its parent.
inherited
onRemove() → void
Called right before the component is removed from the game.
inherited
positionOf(Vector2 point) Vector2
Convert local coordinates of a point point inside the component into the parent's coordinate space.
positionOfAnchor(Anchor anchor) Vector2
Similar to positionOf(), but applies to any anchor point within the component.
processPendingLifecycleEvents() → void
Attempt to resolve any pending lifecycle events on this component.
inherited
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.
inherited
remove(Component component) → void
Removes a component from the component tree, calling onRemove for it and its children.
inherited
removeAll(Iterable<Component> components) → void
Removes all the children in the list and calls onRemove for all of them and their children.
inherited
removeFromParent() → void
Remove the component from its parent in the next tick.
inherited
render(Canvas canvas) → void
inherited
renderDebugMode(Canvas canvas) → void
override
renderTree(Canvas canvas) → void
override
reorderChildren() → void
Call this if any of this component's children priorities have changed at runtime.
inherited
setByRect(Rect rect) → void
Mutates position and size using the provided rect as basis. This is a relative rect, same definition that toRect use (therefore both methods are compatible, i.e. setByRect ∘ toRect = identity).
setMounted() → void
inherited
toAbsoluteRect() Rect
The bounding rectangle of the component in global coordinate space.
toLocal(Vector2 point) Vector2
Transform point from the parent's coordinate space into the local coordinates. This function is the inverse of positionOf().
toRect() Rect
Returns the bounding rectangle for this component.
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.
inherited
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.
inherited

Operators

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