Search code examples
flutterdartflame

Flutter and Flame - Unable to move parallax effect to separate class [New to flutter]


I'm trying to develop a little 2d platform game with flutter and flame and I already got stuck with refactoring my code....

I want to put as much as possible in separate classes and also the background (parallax effect). This is a part of my main file with the effect included.

class PlatformGame extends BaseGame with TapDetector {
  Player _player;

  ParallaxComponent _parallaxComponent;
  Size screenSize;
  double tileSize;

  PlatformGame() {
    _parallaxComponent = ParallaxComponent(
      [
        ParallaxImage('bg/bg1.png', fill: LayerFill.height),
        ParallaxImage('bg/bg2.png'),
        ParallaxImage('bg/bg3.png'),
        ParallaxImage('bg/bg4.png'),
        ParallaxImage('bg/bg5.png'),
        ParallaxImage('bg/bg6.png', fill: LayerFill.none),
      ],
      baseSpeed: Offset(100, 0),
      layerDelta: Offset(20, 0),
    );

    add(_parallaxComponent);
    _player = Player();
    add(_player);
  }
}

I tried to just create a new class called 'background' and move all the code inside there.

class Background extends ParallaxComponent {
  Background(List<ParallaxImage> images) : super(images) {

    //Here should be the Images for the parallax 

  }  
}

But I got stuck there. No matter what im trying, I always get errors. Obviously, I'm not able to handle the constructor correctly. I've read a lot but didn't get any ideas. Can someone help me out?

The Player ist in a separate class and works fine.

Thanks Martin


Solution

  • You can write your class as following :

    
    class Background extends ParallaxComponent {
      Background(List<ParallaxImage> images)
          : super(images, baseSpeed: Offset(100, 0), layerDelta: Offset(20, 0));
    }