-
Notifications
You must be signed in to change notification settings - Fork 6
Flambe core
Flambe mostly uses composition over inheritance. Flambe uses an entity/component system rather than sprawling inheritance hierarchies. The composition pattern also comes up repeatedly in other parts of Flambe's design.
You can nest Entities inside an Entity (addChild
-function) but you can also add Components (add
-function). You cannot extend a Entity. The Entities define the hierarchy and order of the 'displaylist'/rendering order.
Components are bits of data and logic that can be added to entities. Every component has 4 important functions: onAdded()
, onUpdate(dt:Float)
, onRemoved()
, dispose()
.
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, not frames.
For Flash-users, there is no ENTER_FRAME event.
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.
var backgroundFill:FillSprite = new FillSprite(0x154d79, System.stage.width, System.stage.height);
owner.addChild(new Entity().add(backgroundFill));
To iterate over the hierarchy, use the parent
, firstChild
, next
and firstComponent
fields. For example:
var child = entity.firstComponent;
while (child != null)
{
var next = child.next;
// do something with child here
child.fuuuuu();
child = next;
}
You can assume there is a class of given type in the Entity, but to be sure its there, do a null-check. // alternatively you can also choose to check with entity.has(Sprite)
. In this example an Entity has a Sprite. We try to grab it back out of the Entity.
package;
class MyGame
{
public function new()
{
var entity = new Entity();
entity.add( new Sprite() ); // add a sprite.
// .. later in code..
var sprite = entity.get(Sprite);
if (sprite != null)
{
trace('sprite found in entity: ' + sprite);
}
else
{
trace('Warn: No sprite found.');
}
}
}
You can grab the owner of the component, this is the Entity. The owner is available in the onAdded
- function, not in the constructor.
package;
class MyComponent extends Component
{
override public function onAdded()
{
trace( this.owner ); // Entity
}
}
Documentation guide for Flambe - Targeted to version 4.0+
Flambe | Installation | Demo projects | Showcase | API Reference | Forum
Flambe is MIT licensed and available for free. Feel free to contribute!
- Home / Installation
- Entity / Components
- Core
- Assets
- Publish your game
- Other
- Editors
- Plugins, tools, extensions
- Help
- More Flambe