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

15. Attack Saucers

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: 14. Understand Collision Between Components Next episode: 16. Break Down Meteorites

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.

Transcript: 15. Attack Saucers

Let’s summarize what you learned in the last lesson:

First, you’ll need hitboxes in all components that you want to detect collisions on.

You can do this by adding a RectangleHitbox in onLoad like-so.

Then, you’ll need a way to access the parent MeteormaniaGame since it’s the brain of the operation. Every action is performed by it.

For that, just add mixin HasGameRef to your component and it’ll have access to the parent MeteormaniaGame instance.

After adding hitboxes, you’ll also need to add the corresponding collision callbacks and override the behaviors to match your intended effects.

This is what gives the components the ability to detect moments when other components collide with themselves.

Flame makes this super easy by providing you CollisionCallbacks. It’s a matter of just adding it to your component and overriding onCollision to detect collisions with other components.

Finally, you’ll need to define the desired behavior in MeteormaniaGame.

You’ll also need to call that function from the different components when they collide so you update the game loop correctly.

Demo

Now, let’s see it in action one more time by adding collisions to Saucer.

Saucer

Let’s start by adding some collisions to Saucer.

Open saucer.dart and add the following mixins to the Saucer.

with CollisionCallbacks, HasGameRef<MeteormaniaGame> {

As you’ve seen before, CollisionCallbacks allows the component to get notified when it collides with other components. And HasGameRef gives access to the game instance so that you can call functions on it and react to collisions.

Now, override onLoad and add a hitbox so that the game can recognize collisions with this component.

add(RectangleHitbox());

Finally, override onCollision and call removeFromParent when it collides with a component of type Bullet.

@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
  if (other is Bullet) {
    removeFromParent();
  }
  super.onCollision(intersectionPoints, other);
}

Bullet

Now it’s time to do the same thing for Bullet.

Open bullet.dart and add the mixins to the component.

with CollisionCallbacks, HasGameRef<MeteormaniaGame> {

Then, inside onLoad, add a RectangleHitbox so that the game can recognize collisions with this component.

add(RectangleHitbox());

Next, override onCollision and check for collisions with Saucer.

@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
  if (other is Saucer) {
    
  }

  super.onCollision(intersectionPoints, other);
}

If it’s colliding with Saucer, then call bonusEnemyHit on game and removeFromParent to remove the component from the UI.

game.bonusEnemyHit();
removeFromParent();

MeteormaniaGame

It’s time to implement bonusEnemyHit function. Create a new function with the corresponding name.

void bonusEnemyHit() {
}

Then, play the explosion sound effect and call addPoints on manager.

FlameAudio.play('sfx/explosion.mp3');
manager.addPoints(5);

Build and run the game. See if you are fast enough to hit bonus points when shooting down the Saucer.