SpawnComponent constructor
SpawnComponent({
- required double period,
- PositionComponent factory(
- int amount
- List<
PositionComponent> multiFactory(- int amount
- Shape? area,
- bool within = true,
- bool selfPositioning = false,
- bool autoStart = true,
- bool spawnWhenLoaded = false,
- Random? random,
- 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;