Skip to content

Flambe core

Mark Knol edited this page May 15, 2013 · 29 revisions

Structure Entities Displaylist

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.

Entities

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

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().

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, not frames. For Flash-users, there is no ENTER_FRAME event.

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.

Add a colored box inside a component class

var backgroundFill:FillSprite = new FillSprite(0x154d79, System.stage.width, System.stage.height);
owner.addChild(new Entity().add(backgroundFill));

Loop through components of an Entity

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;
}

Grab a component from a type inside an Entity

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.');
       }
   }
}

Get the entity within a component class

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
   }
}
Clone this wiki locally