forked from useflashpunk/FlashPunk
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
noonat edited this page Apr 8, 2011
·
5 revisions
These wiki docs are for new features in this fork. Look here for more FlashPunk help.
I've added a minimal Signal class to FlashPunk. It is similar in behavior to the classes in as3-signals, but more lightweight.
You might use a signal like so:
import net.flashpunk.Signal;
// Define a signal somewhere
public var onExplode:Signal = new Signal;
// Add a listener somewhere else
foo.onExplode.add(function(damage:Number):void {
// do something
trace("nooooo splosions!");
});
// Dispatch the signal to call all the listeners
foo.onExplode.dispatch(42);Components allow you to break reusable behavior out of your entity. You can share logic across many types of entities without needing them to all inherit from one class. The Health component is a good example of this kind of shared behavior.
You can add a component to any entity:
import net.flashpunk.components.Health;
var entity:Entity = world.create(Entity);
entity.addComponent('health', new Health(32));You can check for components to do things to entities:
var health:Health = entity.getComponent('health') as Health;
if (health) health.hurt(42);Components tend to work well with signals:
health.onHurt.add(function(damage:Number, hurter:Entity):void {
trace("ow!! hurt for", damage, "damage!");
});
health.onKilled.add(function(killer:Entity):void {
trace("ughhh... he's dead, jim ;_;");
});The entity methods:
// Adds the component to the entity with the given name,
// replacing any existing components with that name.
entity.addComponent(name, component):void;
// Get the component for the given name. You'll have to cast this
// to the appropriate component type, like I did with Health above.
// Returns null if no such component exists.
var component:Component = entity.getComponent(name);
// Remove the component with the given name.
entity.removeComponent(name);The component structure:
class net.flashpunk.Component
{
// If the Component should update.
var active:Boolean = true;
// The Component's parent Entity.
var entity:Entity = null;
// If the Component should render at its position relative to its
// parent Entity's position.
var relative:Boolean = true;
// If the Component should render.
var visible:Boolean = true;
// Constructor. Calls reset().
function Component();
// Override this, called when the Component is added to an entity.
function added():void;
// Override this, called when the parent Entity is added to the world.
function addedToWorld():void;
// Override this, called when the Component is removed from an Entity.
function removed(entity:Entity):void;
// Override this, called when the entity is removed from the world.
function removedFromWorld(world:World):void;
// Render the component. This is like Graphics.render().
function render(target:BitmapData, point:Point, camera:Point):void;
// Override this, called when the Component should reset itself back
// to a default state. Called by Component() and Entity.created().
function reset():void;
// Update the Component, like Entity.update().
function update():void;
}