flutterflame

What is the difference between adding components to the world and the FlameGame?


So I'm trying to learn Flame and there is this concept of adding components to the World and adding components directly to FlameGame. I kind of understood that the world works with the camera somehow. But the topics are still not clear to me. So this is my first step towards understanding this. I've read the docs but didn't really understand much. I would really appreciate if someone can take the time to explain this concept of world, camera and how adding children to the world differs from adding them to the FlameGame. Thank you.

import 'dart:async';

import 'package:flame/components.dart';
import 'package:flame/game.dart';

import 'components/components.dart';
import 'config.dart';

class BrickBreaker extends FlameGame {
  BrickBreaker()
      : super(
          camera: CameraComponent.withFixedResolution(
            width: gameWidth,
            height: gameHeight,
          ),
        );

  double get width => size.x;
  double get height => size.y;

  @override
  FutureOr<void> onLoad() async {
    super.onLoad();

    camera.viewfinder.anchor = Anchor.topLeft;

    // adding to world, but why not add directly to the current component?
    // add(PlayArea())
    world.add(PlayArea());
  }
}

Solution

  • The difference is that the camera is looking at the World, so if you're moving around the camera (viewfinder) you will move around in the world, but if you're moving around the camera and have added your components to the FlameGame they will be static.

    I'd recommend not adding any components to the FlameGame if you don't know exactly what you are doing. If you want static components like HUDs then you want to add them to the viewport with camera.viewport.add(...) and if you want static components behind the world, then add them to the backdrop with camera.backdrop.add(...).