feat: ComponentKey API (#2566)

Adds a new key api on FCS, which will allow users to get a component from the tree, without needing to iterate over all the children or a parent descendants.
This commit is contained in:
Erick
2023-06-20 09:36:15 -03:00
committed by GitHub
parent 667a169811
commit b3efb612cb
41 changed files with 409 additions and 18 deletions

View File

@ -1,4 +1,5 @@
import 'package:examples/commons/ember.dart';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/services.dart';
@ -17,19 +18,23 @@ class KeyboardExample extends FlameGame with KeyboardEvents {
// Direction in which amber is moving.
final Vector2 _direction = Vector2.zero();
late final Ember _ember;
@override
Future<void> onLoad() async {
_ember = Ember(position: size / 2, size: Vector2.all(100));
add(_ember);
add(
Ember(
key: ComponentKey.named('ember'),
position: size / 2,
size: Vector2.all(100),
),
);
}
@override
void update(double dt) {
super.update(dt);
final ember = findByKeyName<Ember>('ember');
final displacement = _direction.normalized() * _speed * dt;
_ember.position.add(displacement);
ember?.position.add(displacement);
}
@override