Flutter 和 Flame - 无法将视差效果移动到单独的类 [Flutter 新手]

问题描述

我正在尝试开发一个带有颤振和火焰的小型 2d 平台游戏,但我已经陷入了重构代码的困境......

我想把尽可能多的放在不同的类和背景中(视差效果)。这是包含效果的主文件的一部分。

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,);

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

我尝试创建一个名为“background”的新类并将所有代码移到其中。

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

    //Here should be the Images for the parallax 

  }  
}

但是我卡在那里了。无论我尝试什么,我总是会出错。显然,我无法正确处理构造函数。我读了很多,但没有得到任何想法。有人可以帮我吗?

播放器在一个单独的类中并且工作正常。

谢谢 马丁

解决方法

您可以将类编写如下:


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