Your First Flutter Flame Game

Mar 6 2024 · Dart 3, Flutter 3.10.1, Android Studio 2021.3.1 or higher, Visual Studo Code 1.7.4 or higher

Part 3: Collision Detection & Overlays

16. Break Down Meteorites

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 15. Attack Saucers Next episode: 17. Add a Heads-Up Display

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Before you dive into the code again, let’s overview how meteorites should collide in the game.

Demo

Meteorite

Open meteorite.dart and add the same mixins to Meteorite as with other components. Again, CollisionCallbacks enables collision detection callbacks for your component and HasGameRef gives your component access to the parent MeteormaniaGame it belongs to.

with CollisionCallbacks, HasGameRef<MeteormaniaGame> {
@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
  if (other is Bullet || other is Spaceship) {
    if (meteoriteSize == MeteoriteSize.big) {
      game.splitMeteorite(position);
    }

    removeFromParent();
  }
  super.onCollision(intersectionPoints, other);
}

Bullet

Back in Bullet. Check for collisions with components of type Meteorite.

if (other is Meteorite) {
  game.meteoriteHit(other.isBig);
  removeFromParent();
}

MeteoriteGame

Now go to MeteormaniaGame.

void splitMeteorite(Vector2 position) {
}
final meteorites = List.generate(manager.meteoriteFragments, (i) {
});
final directionAngle = 2 * pi * Random().nextDouble();
return Meteorite.small(
  directionAngle: directionAngle,
)
  ..anchor = Anchor.center
  ..position = position;
_world.addAll(meteorites);
void meteoriteHit(bool isBigMeteorite) {
}
FlameAudio.play('sfx/destroy_meteorite.mp3');
if (isBigMeteorite) {
  manager.addPoints(2);
  manager.bigMeteoriteDestroyed();
} else {
  manager.addPoints(1);
  manager.smallMeteoriteDestroyed();
}

Interlude

If you were able to shoot down all enemies, then you might have noticed that nothing happens. The player is now stuck without enemies and has nothing to shoot at.

Demo 2

nextLevel()

Jump back to MeteormaniaGame and add a new function called nextLevel

void nextLevel() {
}
manager.newWave();
addEnemies();
if (manager.isLevelOver) {
  nextLevel();
}
if (manager.isLevelOver) {
  nextLevel();
}
if (manager.isLevelOver) {
  nextLevel();
}
if (isBigMeteorite) {
  manager.bigMeteoriteDestroyed();
} else {
  manager.smallMeteoriteDestroyed();
}