How to change using code with followComponent method in older version of flame to new one

33 Views Asked by At

I am the beginner in flame. I am using code from one of the tutorials. And there is code it provided:

import 'dart:ui';
import 'package:flame/game.dart';
import 'components/player.dart';
import '../helpers/direction.dart';
import 'components/world.dart';

class RayWorldGame extends FlameGame {
  final Player _player = Player();
  final World _world = World();

  @override
  Future<void> onLoad() async {
    await add(_world);
    add(_player);

    _player.position = _world.size / 2;
    camera.followComponent(_player,
        worldBounds: Rect.fromLTRB(0, 0, _world.size.x, _world.size.y));
  }

  void onJoypadDirectionChanged(Direction direction) {
    _player.direction = direction;
  }
}

I`ve got an error:

`Error: The method 'followComponent' isn't defined for the class 'CameraComponent'.`

So, how can i change it, if i want to use the latest verson of flame?

1

There are 1 best solutions below

0
spydon On

That is quite an old tutorial, I would recommend following one that is more up to date. DevKage has some good ones on YouTube, and there are some in out docs, and lots of them posted in a dedicated channel on Discord.

But to your question, for this to work with the current version of Flame I suggest that you use the built-in camera and world. Then it will look like this:

class RayWorldGame extends FlameGame {
  final Player _player = Player();

  @override
  Future<void> onLoad() async {
    world.add(_player);
    camera.follow(_player);
  }

  void onJoypadDirectionChanged(Direction direction) {
    _player.direction = direction;
  }
}

By default the world doesn't have any static size, so adding the world size for bounds etc doesn't make any sense, you can add your own bounds though if you wish.