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 2: Effects & User Input

12. Challenge: Add Movement to Saucer

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: 11. Rotate the Spaceship Next episode: 13. Play Sound Effects

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: 12. Challenge: Add Movement to Saucer

Use what you’ve learned during this course so far to define the movement of Saucer. Add a new vector for the velocity at which the component will move. Then, update the component’s position so it moves to the right.

It should only move to the right. You can accomplish by increasing the x coordinate from position.

Also, remember to call removeFromParent once the component is out of the screen to avoid performance issues.

Pause the video and try the challenge… Then keep watching for the solution.

Solution

Saucer

Start by opening Saucer and adding a new property called velocity.

final Vector2 velocity = Vector2.zero();

Then, override update.

@override
void update(double dt) {
  super.update(dt);
}

Update velocity and position to move the Saucer to the right.

velocity.x = GameConstants.saucerSpeed;
position += velocity * dt;

Finally, call removeFromParent when the component goes out of the screen.

if (position.x + saucerWidth / 2 < 0 ||
    position.x - saucerWidth / 2 > GameConstants.cameraWidth) {
  removeFromParent();
}

Build and run the game. When the game starts you now see the enemy Saucer move until it disappears out of the screen.