Crafty Core
Events
- NewEntityName [entity name: String]
- After setting new name for entity
- NewComponent [Component: String]
- when a new component is added to the entity
- RemoveComponent [Component: String]
- when a component is removed from the entity
- Remove
- when the entity is removed by calling .destroy()
Set of methods added to every single entity.
.setName
public this .setName(String name)
- name
- A human readable name for debugging purposes.
Example
this.setName("Player");
.addComponent
public this .addComponent(String componentList)
- componentList
- A string of components to add separated by a comma
,
public this .addComponent(String Component1[, .., String ComponentN])
- Component#
- Component ID to add.
Adds a component to the selected entities or entity.
Components are used to extend the functionality of entities. This means it will copy properties and assign methods to augment the functionality of the entity.
There are multiple methods of adding components. Passing a string with a list of component names or passing multiple arguments with the component names.
If the component has a function named init it will be called.
Example
this.addComponent("2D, Canvas");
this.addComponent("2D", "Canvas");
.toggleComponent
public this .toggleComponent(String ComponentList)
- ComponentList
- A string of components to add or remove separated by a comma
,
public this .toggleComponent(String Component1[, .., String componentN])
- Component#
- Component ID to add or remove.
Add or Remove Components from an entity.
Example
var e = Crafty.e("2D,DOM,Test");
e.toggleComponent("Test,Test2"); //Remove Test, add Test2
e.toggleComponent("Test,Test2"); //Add Test, remove Test2
var e = Crafty.e("2D,DOM,Test");
e.toggleComponent("Test","Test2"); //Remove Test, add Test2
e.toggleComponent("Test","Test2"); //Add Test, remove Test2
e.toggleComponent("Test"); //Remove Test
.requires
public this .requires(String componentList)
- componentList
- List of components that must be added
Makes sure the entity has the components listed. If the entity does not have the component, it will add it.
See Also
.removeComponent
public this .removeComponent(String Component[, soft])
- component
- Component to remove
- soft
- Whether to soft remove it (defaults to
true)
Removes a component from an entity. A soft remove (the default) will only
refrain .has() from returning true. Hard will remove all
associated properties and methods.
Example
var e = Crafty.e("2D,DOM,Test");
e.removeComponent("Test"); //Soft remove Test component
e.removeComponent("Test", false); //Hard remove Test component
.has
public Boolean .has(String component)
Returns true or false depending on if the
entity has the given component.
For better performance, simply use the .__c object
which will be true if the entity has the component or
will not exist (or be false).
.attr
public this .attr(String property, * value)
- property
- Property of the entity to modify
- value
- Value to set the property to
public this .attr(Object map)
- map
- Object where the key is the property to modify and the value as the property value
Events
- Change [Data: {key: value}]
- when properties change
Use this method to set any property of the entity.
Example
this.attr({key: "value", prop: 5});
this.key; //value
this.prop; //5
this.attr("key", "newvalue");
this.key; //newvalue
.toArray
public this .toArray(void)
This method will simply return the found entities as an array.
.timeout
public this .timeout(Function callback, Number delay)
- callback
- Method to execute after given amount of milliseconds
- delay
- Amount of milliseconds to execute the method
The delay method will execute a function after a given amount of time in milliseconds.
Essentially a wrapper for setTimeout.
Example
Destroy itself after 100 milliseconds
this.timeout(function() {
this.destroy();
}, 100);
.bind
public this .bind(String eventName, Function callback)
- eventName
- Name of the event to bind to
- callback
- Method to execute when the event is triggered
Attach the current entity (or entities) to listen for an event.
Callback will be invoked when an event with the event name passed is triggered. Depending on the event, some data may be passed via an argument to the callback function.
The first argument is the event name (can be anything) whilst the second argument is the callback. If the event has data, the callback should have an argument.
Events are arbitrary and provide communication between components. You can trigger or bind an event even if it doesn't exist yet.
Example
this.attr("triggers", 0); //set a trigger count
this.bind("myevent", function() {
this.triggers++; //whenever myevent is triggered, increment
});
this.bind("EnterFrame", function() {
this.trigger("myevent"); //trigger myevent on every frame
});
See Also
.unbind
public this .unbind(String eventName[, Function callback])
- eventName
- Name of the event to unbind
- callback
- Function to unbind
Removes binding with an event from current entity.
Passing an event name will remove all events bound to that event. Passing a reference to the callback will unbind only that callback.
See Also
.trigger
public this .trigger(String eventName[, Object data])
- eventName
- Event to trigger
- data
- Arbitrary data that will be passed into every callback as an argument
Trigger an event with arbitrary data. Will invoke all callbacks with
the context (value of this) of the current entity object.
Note: This will only execute callbacks within the current entity, no other entity.
The first argument is the event name to trigger and the optional second argument is the arbitrary event data. This can be absolutely anything.
.setter
public this .setter(String property, Function callback)
- property
- Property to watch for modification
- callback
- Method to execute if the property is modified
Will watch a property waiting for modification and will then invoke the given callback when attempting to modify.
Note: Support in IE<9 is slightly different. The method will be executed after the property has been set
.destroy
public this .destroy(void)
Will remove all event listeners and delete all properties as well as removing from the stage

@craftyjs
#crafty@freenode.net
google groups
louis@craftyjs.com
github