Files
flame/packages/flame_forge2d/example/lib/composition_sample.dart
Lukas Klingsbo 12fd07e5ae Possibility to initialize all PositionComponents from onLoad (#1113)
* Fix ParallaxComponent constructor

* Fix sizing bug parallax_component

* Unify TextComponent and TextBoxComponent

* Fix tests

* Update PositionComponent docs

* Add changelog entry

* Apply suggestions from code review

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

* Fix analyze issue

* Apply suggestions from code review

Co-authored-by: Luan Nico <luanpotter27@gmail.com>

* Fix line length in components.md

Co-authored-by: Erick <erickzanardoo@gmail.com>
Co-authored-by: Luan Nico <luanpotter27@gmail.com>
2021-11-18 11:42:12 +01:00

70 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 HasTappables {
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(
text: 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;
}
}