Files
flame/packages/flame_forge2d/example/lib/composition_sample.dart
Lukas Klingsbo 3cb23ef530 TextPaint to use TextStyle instead of TextPaintConfig (#1086)
* `TextPaint` to use `TextStyle` instead of `TextPaintConfig`

* Update packages/flame/lib/src/text.dart

Co-authored-by: Pasha Stetsenko <stpasha@google.com>

* Removed BaseTextConfig and TextPaintConfig

* Update text docs

* Apply suggestions from code review

Co-authored-by: Erick <erickzanardoo@gmail.com>

* Remove generics

* Update TextBoxExample

* Update text examples variable names

* Fix TextPaint in collision_detection example

Co-authored-by: Pasha Stetsenko <stpasha@google.com>
Co-authored-by: Erick <erickzanardoo@gmail.com>
2021-11-13 16:38:06 +01:00

67 lines
1.9 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/palette.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_forge2d/forge2d_game.dart';
import 'package:flutter/material.dart';
import 'package:forge2d/forge2d.dart';
import 'balls.dart';
import 'boundaries.dart';
class CompositionSample extends Forge2DGame with HasTappableComponents {
static const info = '''
This example shows how to compose a `BodyComponent` together with a normal Flame
component. Click the ball to see the number increment.
''';
CompositionSample() : super(zoom: 20, gravity: Vector2(0, -10.0));
@override
Future<void> onLoad() async {
await super.onLoad();
final boundaries = createBoundaries(this);
boundaries.forEach(add);
final center = screenToWorld(camera.viewport.effectiveSize / 2);
add(TapableBall(center));
}
}
class TapableBall extends Ball with Tappable {
late final TextComponent textComponent;
int counter = 0;
final TextStyle _textStyle =
TextStyle(color: BasicPalette.white.color, fontSize: 4);
late final TextPaint _textPaint;
TapableBall(Vector2 position) : super(position) {
originalPaint = BasicPalette.white.paint();
paint = originalPaint;
}
@override
Future<void> onLoad() async {
super.onLoad();
_textPaint = TextPaint(style: _textStyle);
textComponent = TextComponent(counter.toString(), textRenderer: _textPaint);
add(textComponent);
}
@override
void update(double dt) {
super.update(dt);
// This is unfortunately needed since [BodyComponent] will set all its
// children to `debugMode = true` currently, we should come up with a
// nicer solution to this.
textComponent.debugMode = false;
textComponent.text = counter.toString();
}
@override
bool onTapDown(_) {
counter++;
body.applyLinearImpulse(Vector2.random() * 1000);
paint = randomPaint();
return false;
}
}