Adding Component#onMount

This commit is contained in:
Erick Zanardo
2020-03-05 21:18:02 -03:00
parent 578f06123f
commit d6f2c5e960
5 changed files with 21 additions and 7 deletions

View File

@ -1,6 +1,7 @@
## [next] ## [next]
- Add loop for AnimationComponent.sequenced() - Add loop for AnimationComponent.sequenced()
- TextComponent optimization (thanks @Gericop) - TextComponent optimization (thanks @Gericop)
- Adding Component#onMount
## 0.18.1 ## 0.18.1
- Expose stepTime paramter from the Animation class to the animation component - Expose stepTime paramter from the Animation class to the animation component

View File

@ -37,6 +37,8 @@ The `destroy` method can be implemented to return true and warn the `BaseGame` t
The `isHUD` method can be implemented to return true (default false) to make the `BaseGame` ignore the `camera` for this element. The `isHUD` method can be implemented to return true (default false) to make the `BaseGame` ignore the `camera` for this element.
The `onMount` method can be overridden to run initializations code for the component, when this method is called, BaseGame has make sure that all the mixin which would change this component behaviour, is already resolved.
There are also other implementations: There are also other implementations:
* The `AnimationComponent` takes an `Animation` object and renders a cyclic animated sprite (more details about Animations [here](/doc/images.md#Animation)) * The `AnimationComponent` takes an `Animation` object and renders a cyclic animated sprite (more details about Animations [here](/doc/images.md#Animation))

View File

@ -3,6 +3,7 @@ import 'dart:ui';
import 'package:flame/anchor.dart'; import 'package:flame/anchor.dart';
import 'package:flame/components/component.dart'; import 'package:flame/components/component.dart';
import 'package:flame/components/mixins/has_game_ref.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flame/palette.dart'; import 'package:flame/palette.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -15,14 +16,9 @@ class Palette {
static const PaletteEntry blue = PaletteEntry(Color(0xFF0000FF)); static const PaletteEntry blue = PaletteEntry(Color(0xFF0000FF));
} }
class Square extends PositionComponent { class Square extends PositionComponent with HasGameRef<MyGame> {
static const SPEED = 0.25; static const SPEED = 0.25;
Square(double size) {
width = height = size;
anchor = Anchor.center;
}
@override @override
void resize(Size size) { void resize(Size size) {
x = size.width / 2; x = size.width / 2;
@ -43,10 +39,18 @@ class Square extends PositionComponent {
angle += SPEED * t; angle += SPEED * t;
angle %= 2 * math.pi; angle %= 2 * math.pi;
} }
@override
void onMount() {
width = height = gameRef.squareSize;
anchor = Anchor.center;
}
} }
class MyGame extends BaseGame { class MyGame extends BaseGame {
final double squareSize = 128;
MyGame() { MyGame() {
add(Square(64.0)); add(Square());
} }
} }

View File

@ -54,6 +54,12 @@ abstract class Component {
/// It can be any integer (negative, zero, or positive). /// It can be any integer (negative, zero, or positive).
/// If two components share the same priority, they will probably be drawn in the order they were added. /// If two components share the same priority, they will probably be drawn in the order they were added.
int priority() => 0; int priority() => 0;
/// Called when the component has been added and preperad by the game instance
///
/// This can be used to make initializations on your component as when this method is called
/// things like resize, and mixins are already set for the component.
void onMount() {}
} }
/// A [Component] implementation that represents a component that has a specific, possibly dynamic position on the screen. /// A [Component] implementation that represents a component that has a specific, possibly dynamic position on the screen.

View File

@ -165,6 +165,7 @@ abstract class BaseGame extends Game with TapDetector {
void add(Component c) { void add(Component c) {
preAdd(c); preAdd(c);
components.add(c); components.add(c);
c.onMount();
} }
/// Registers a component to be added on the components on the next tick. /// Registers a component to be added on the components on the next tick.