SpawnComponent constructor

SpawnComponent({
  1. required double period,
  2. PositionComponent factory(
    1. int amount
    )?,
  3. List<PositionComponent> multiFactory(
    1. int amount
    )?,
  4. Shape? area,
  5. bool within = true,
  6. bool selfPositioning = false,
  7. bool autoStart = true,
  8. bool spawnWhenLoaded = false,
  9. Random? random,
  10. ComponentKey? key,
})

The SpawnComponent is a non-visual component which can spawn PositionComponents randomly within a set area. If area is not set it will use the size of the nearest ancestor that provides a size. period will set the static time interval for when it will spawn new components. If you want to use a non static time interval, use the SpawnComponent.periodRange constructor. If you want to set the position of the spawned components yourself inside of the factory, set selfPositioning to true. You can either provide a factory that returns one component or a multiFactory which returns a list of components. In this case the amount parameter will be increased by the number of returned components.

Implementation

SpawnComponent({
  required double period,
  PositionComponent Function(int amount)? factory,
  List<PositionComponent> Function(int amount)? multiFactory,
  this.area,
  this.within = true,
  this.selfPositioning = false,
  this.autoStart = true,
  this.spawnWhenLoaded = false,
  Random? random,
  super.key,
})  : assert(
        !(selfPositioning && area != null),
        "Don't set an area when you are using selfPositioning=true",
      ),
      assert(
        (factory != null) ^ (multiFactory != null),
        'You need to provide either a factory or a multiFactory, not both.',
      ),
      _period = period,
      multiFactory = multiFactory ?? _wrapFactory(factory!),
      _random = random ?? randomFallback;