Adding method to remove components from the list

This commit is contained in:
Erick Zanardo
2020-06-21 10:18:04 -03:00
parent 943cb44900
commit cd5ba3b906
6 changed files with 44 additions and 24 deletions

View File

@ -24,12 +24,6 @@ class Palette {
class Square extends PositionComponent with HasGameRef<MyGame> {
static const SPEED = 0.25;
@override
void resize(Size size) {
x = size.width / 2;
y = size.height / 2;
}
@override
void render(Canvas c) {
prepareCanvas(c);
@ -53,16 +47,41 @@ class Square extends PositionComponent with HasGameRef<MyGame> {
}
}
class MyGame extends BaseGame with TapDetector {
class MyGame extends BaseGame with DoubleTapDetector, TapDetector {
final double squareSize = 128;
bool running = true;
MyGame() {
add(Square());
add(Square()
..x = 100
..y = 100);
}
@override
void onTap() {
void onTapUp(details) {
final touchArea = Rect.fromCenter(
center: details.localPosition,
width: 20,
height: 20,
);
bool handled = false;
components.forEach((c) {
if (c is PositionComponent && c.toRect().overlaps(touchArea)) {
handled = true;
remove(c);
}
});
if (!handled) {
addLater(Square()
..x = touchArea.left
..y = touchArea.top);
}
}
@override
void onDoubleTap() {
if (running) {
pauseEngine();
} else {