Skip to content

Components

Mark Knol edited this page Jan 17, 2018 · 4 revisions

Components are bits of data and logic

Components can be added to entities. The component consists of a minimal set of data needed for a specific purpose.

In Flambe, every component has 4 important functions:

  • onStart() Called just before this component's first update after being added. This is the best place to put initialization logic that requires accessing other components/entities, since it waits until the rest of the entity hierarchy is accessible. Note that onStart may be delayed until the next frame after adding a component, depending on where in the update step it was added.
  • onAdded() Called after this component has been added to an entity. owner is available after being added. The order of adding matters, in case you want to grab other components from the owner inside this function.
  • onUpdate(deltaTime:Float) Called when this component receives a game update. The deltatime is in seconds.
  • onRemoved() Called just before this component has been removed from its entity.
  • dispose() Removes this component from its owning entity.
  • owner The Entity this component is attached to (can be null).

Component API Docs

Adding lots of components

It's very nice that the add function returns the owner entity. If you want to add a multiple components, this is how you could plug your features.

System.root.addChild(new Entity()
	.add(new FillSprite(0x154d79, 100, 100))
   	.add(new Disposer())
   	.add(new CarEngine())
   	.add(new CarMover())
   	.add(new Path(myPath))
   	.add(new PathFollower())
	.add(new SpeedAdjuster(1.0))
   	.add(new PowerupApplier())
   	.add(new Bot())
);

🕗 The game timer is in every component

The onUpdate-function is called when this component receives a 'game' update, with delta time (The time elapsed since the last 'frame'). Flambe is framerate independent, but targeted at 60 FPS (frames per second). All animation/coded motion should be based on time using this delta time.

💥 Destruction

This dispose function removes this component from its owning entity. You should override the dispose-function in order to dispose objects yourself, to prevent memory leaks.

Clone this wiki locally