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

08. Move Meteorites Around

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: 07. Learn About Effects Next episode: 09. Understand User Input

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

Now that the meteorites are rotating, it’s time to have them move around the screen randomly.

meteorite.dart

Start by opening meteorite.dart and adding two new variables to Meteorite. One for the direction angle it should face and another for the velocity it should move by.

double directionAngle;
final Vector2 velocity = Vector2.zero();
{required this.directionAngle}
@override
void update(double dt) {
  super.update(dt)
}
velocity.x = GameConstants.meteoriteSpeed * cos(directionAngle);
velocity.y = GameConstants.meteoriteSpeed * sin(directionAngle);
position += velocity * dt;
if (position.x < -bigSize.toSize().width) {
  position.x = GameConstants.cameraWidth + bigSize.toSize().width;
}
if (position.x > GameConstants.cameraWidth + bigSize.toSize().width) {
  position.x = -bigSize.toSize().width;
}
if (position.y < -bigSize.toSize().height) {
  position.y = GameConstants.cameraHeight + bigSize.toSize().height;
}
if (position.y > GameConstants.cameraHeight + bigSize.toSize().height) {
  position.y = -bigSize.toSize().height;
}

MeteormaniaGame

Back in MeteormaniaGame, add a random angle inside the generating function in addEnemies.

final randomAngle = 2 * pi * Random().nextDouble();
directionAngle: randomAngle,