* Game as a component * Fix component stories * Effects are now components * Update effects docs * Handle swap of parent * Fix reAddChildren * Wait for children to be added * BaseComponent and PositionComponent to be non-abstract * Simplify HasGameRef * Revert so that onLoad can be null * Fix example description * Effects as components * Remove gameRef from addChildren * Fix hasGameRef * Start migrating effects * Updated comments of effect fields * Fix comments * Continue to fix sequence and combined effects * Upgrade ordered_set * Fix position_component_test * BaseComponent -> Component * Fix combined and sequence effects * Await components to be added in tests * Remove unnecessary game.update in tests * Fix some tests related to composition * BaseGame should be used in examples * Fix CombinedEffect test * Keyboard code to be based on Component * Fix keyboard tests * Fix analyze problems * Fix sequence_effect * Fix combined_effect_test * Store peak state instead of end state * Fix sequence_effect tests * Update tutorial * Fix tutorial 1 * Remove SimplePositionComponentEffect * Remove unused test variable * Update docs * Removed onMount * Remove onMount * Add missing dartdoc * Fix dart docs * Add super.update where needed * Move reAddChildren to component * Reorganize method order in game widget * preOffset -> initialDelay, postOffset -> peakDelay * Introduce component.onParentChange * Remove tests in wrong file * Fix composed component test * Add game lifecycle test * Use BaseGame for mouse cursor test * Oxygen should (?) not call super.update * Use BaseGame in keyboard_test * Fix onLoad to be properly cached * Re-add unintentionally removed override * Fix info for collision detection tests * Add test for correct lifecycle on parent change * Fix particles example * Add component lifecycle diagram to the docs * Add docs for the game lifecycle * onRemove should be called when a game is removed from the widget * Fix analyze errors * prepare should be called from the component itself, not its parent * Fix dartdoc * onParentChange -> onMount * onMount should have void as return type * Simplify the loaderFuture in GameWidget * Fix mock_canvas * Fix rebase problem * Remove asComponent * Less complex _loaderFuture * Add super.update to no_fcs parallax example * Fix async tests * Revert _loaderFuture * Fix analyze issues * await gameWithCollidables * Keep epsilon small where it can be * tappable methods should return bool * Game lifecycle is now the same as for Component * Remove mustCallSuper from component.update * Make onLoadCache protected * @internal on onLoadCache * Cache/Memoize debugPaint and debugTextPaint * Fix imports * Fix comments * Always call super.onLoad so that mixins can override it * Add forgotten super.onLoad * Bump coverage percentage * HasCollidables should override update * Fix Game comments * Fix some dartdoc * Apply suggestions from code review Co-authored-by: Erick <erickzanardoo@gmail.com> * Game + Loadable as mixins * Update packages/flame/lib/src/game/game_widget/game_widget.dart Co-authored-by: Luan Nico <luanpotter27@gmail.com> * Update loadable docs * Fix comments * Move fps_counter * Fix keyboard example * Fix dartdoc * Remove tutorials temporarily * Fix game lowlevel graph * Fix resize issue Co-authored-by: Erick <erickzanardoo@gmail.com> Co-authored-by: Luan Nico <luanpotter27@gmail.com>
9.0 KiB
Util
On this page you can find documentation for some utility classes and methods.
Device Class
This class can be accessed from Flame.device and it has some methods that can be used to control
the state of the device, for instance you can change the screen orientation and set whether the
application should be fullscreen or not.
Flame.device.fullScreen()
When called, this disables all SystemUiOverlay making the app full screen.
When called in the main method, it makes your app full screen (no top nor bottom bars).
Note: It has no effect when called on the web.
Flame.device.setLandscape()
This method sets the orientation of the whole application (effectively, also the game) to landscape
and depending on operating system and device setting, should allow both left and right landscape
orientations. To set the app orientation to landscape on a specific direction, use either
Flame.device.setLandscapeLeftOnly or Flame.device.setLandscapeRightOnly.
Note: It has no effect when called on the web.
Flame.device.setPortrait()
This method sets the orientation of the whole application (effectively, also the game) to portrait
and depending on operating system and device setting, it should allow for both up and down portrait
orientations. To set the app orientation to portrait for a specific direction, use either
Flame.device.setPortraitUpOnly or Flame.device.setPortraitDownOnly.
Note: It has no effect when called on the web.
Flame.device.setOrientation() and Flame.device.setOrientations()
If a finer control of the allowed orientations is required (without having to deal with
SystemChrome directly), setOrientation (accepts a single DeviceOrientation as a parameter) and
setOrientations (accepts a List<DeviceOrientation> for possible orientations) can be used.
Note: It has no effect when called on the web.
Timer
Flame provides a simple utility class to help you handle countdowns and timer state changes like events.
Countdown example:
import 'dart:ui';
import 'package:flame/game.dart';
import 'package:flame/text_config.dart';
import 'package:flame/timer.dart';
import 'package:flame/vector2.dart';
class MyGame extends Game {
final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
final countdown = Timer(2);
MyGame() {
countdown.start();
}
@override
void update(double dt) {
countdown.update(dt);
if (countdown.finished) {
// Prefer the timer callback, but this is better in some cases
}
}
@override
void render(Canvas canvas) {
textConfig.render(
canvas,
"Countdown: ${countdown.current.toString()}",
Vector2(10, 100),
);
}
}
Interval example:
import 'dart:ui';
import 'package:flame/game.dart';
import 'package:flame/text_config.dart';
import 'package:flame/timer.dart';
import 'package:flame/vector2.dart';
class MyGame extends Game {
final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
Timer interval;
int elapsedSecs = 0;
MyGame() {
interval = Timer(
1,
callback: () => elapsedSecs += 1,
repeat: true,
);
interval.start();
}
@override
void update(double dt) {
interval.update(dt);
}
@override
void render(Canvas canvas) {
textConfig.render(canvas, "Elapsed time: $elapsedSecs", Vector2(10, 150));
}
}
Timer instances can also be used inside a FlameGame game by using the TimerComponent class.
TimerComponent example:
import 'package:flame/timer.dart';
import 'package:flame/components/timer_component.dart';
import 'package:flame/game.dart';
class MyFlameGame extends FlameGame {
MyFlameGame() {
add(
TimerComponent(
Timer(
10,
callback: () => print("10 seconds elapsed"),
repeat: true,
)
..start()
)
);
}
}
Extensions
Flame bundles a collection of utility extensions, these extensions are meant to help the developer with shortcuts and conversion methods, here you can find the summary of those extensions.
They can all be imported from package:flame/extensions.dart
Canvas
Methods:
scaleVector: Just likecanvas scalemethod, but takes aVector2as an argument.translateVector: Just likecanvas translatemethod, but takes aVector2as an argument.renderPoint: renders a single point on the canvas (mostly for debugging purposes).renderAtandrenderRotated: if you are directly rendering to theCanvas, you can use these functions to easily manipulate coordinates to render things on the correct places. They change theCanvastransformation matrix but reset afterwards.
Color
Methods:
darken: Darken the shade of the color by an amount between 0 to 1.brighten: Brighten the shade of the color by an amount between 0 to 1.
Factories:
fromRGBHexString: Parses an RGB color from a valid hex string (e.g. #1C1C1C).fromARGBHexString: Parses an ARGB color from a valid hex string (e.g. #FF1C1C1C).
Image
Methods:
pixelsInUint8: Retrieves the pixel data as aUint8List, in theImageByteFormat.rawRgbapixel format, for the image.getBoundingRect: Get the bounding rectangle of theImageas aRect.size: The size of anImageasVector2.darken: Darken each pixel of theImageby an amount between 0 to 1.brighten: Brighten each pixel of theImageby an amount between 0 to 1.
Offset
Methods;
toVector2; Creates anVector2from theOffset.toSize: Creates aSizefrom theOffset.toPoint: Creates aPointfrom theOffset.toRect: Creates aRectstarting in (0,0) and its bottom right corner is the [Offset].
Rect
Methods:
toOffset: Creates anOffsetfrom theRect.toVector2: Creates aVector2starting in (0,0) and goes to the size of theRect.containsPointWhether thisRectcontains aVector2point or not.intersectsSegment; Whether the segment formed by twoVector2s intersects thisRect.intersectsLineSegment: Whether theLineSegmentintersects theRect.toVertices: Turns the four corners of theRectinto a list ofVector2.toMathRectangle: Converts thisRectinto amath.Rectangle.toGeometryRectangle: Converts thisRectinto aRectanglefrom flame-geom.
math.Rectangle
Methods:
toRect: Converts this mathRectangleinto an uiRect.
Size
Methods:
toVector2; Creates anVector2from theSize.toOffset: Creates aOffsetfrom theSize.toPoint: Creates aPointfrom theSize.toRect: Creates aRectstarting in (0,0) with the size ofSize.
Vector2
This class comes from the vector_math package and we have some useful extension methods on top of
what is offered by that package.
Methods:
toOffset: Creates aOffsetfrom theVector2.toPoint: Creates aPointfrom theVector2.toRect: Creates aRectstarting in (0,0) with the size ofVector2.toPositionedRect: Creates aRectstarting from [x, y] in theVector2and has the size of theVector2argument.lerp: Linearly interpolates theVector2towards another Vector2.rotate: Rotates theVector2with an angle specified in radians, it rotates around the optionally definedVector2, otherwise around the center.scaleTo: Changes the length of theVector2to the length provided, without changing direction.moveToTarget: Smoothly moves a Vector2 in the target direction by a given distance.
Factories:
fromInts: Create aVector2with ints as input.
Operators:
&: Combines twoVector2s to form a Rect, the origin should be on the left and the size on the right.%: Modulo/Remainder of x and y separately of twoVector2s.
Matrix4
This class comes from the vector_math package. We have created a few extension methods on top
of what is already offered by vector_math.
Methods:
translate2: Translate theMatrix4by the givenVector2.transform2: Create a newVector2by transforming the givenVector2using theMatrix4.transformed2: Transform the inputVector2into the outputVector2.
Getters:
m11: The first row and first column.m12: The first row and second column.m13: The first row and third column.m14: The first row and fourth column.m21: The second row and first column.m22: The second row and second column.m23: The second row and third column.m24: The second row and fourth column.m31: The third row and first column.m32: The third row and second column.m33: The third row and third column.m34: The third row and fourth column.m41: The fourth row and first column.m42: The fourth row and second column.m43: The fourth row and third column.m44: The fourth row and fourth column.
Factories:
scale: Create a scaledMatrix4. Either by passing aVector4orVector2as it's first argument, or by passing x y z doubles.