craftyjs.github.com

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.

Back to top

.setName

public this .setName(String name)

name
A human readable name for debugging purposes.

Example

this.setName("Player");
Back to top

.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");
Back to top

.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
Back to top

.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

Back to top

.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
Back to top

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

Back to top

.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
Back to top

.toArray

public this .toArray(void)

This method will simply return the found entities as an array.

Back to top

.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);
Back to top

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

Unlike DOM events, Crafty events are exectued synchronously.

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

Back to top

.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

Back to top

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

Unlike DOM events, Crafty events are exectued synchronously.

Back to top

.each

public this .each(Function method)

method
Method to call on each iteration

Iterates over found entities, calling a function for every entity.

The function will be called for every entity and will pass the index in the iteration as an argument. The context (value of this) of the function will be the current entity in the iteration.

Example

Destroy every second 2D entity

Crafty("2D").each(function(i) {
    if(i % 2 === 0) {
        this.destroy();
    }
});
Back to top

.clone

Creates an exact, numeric copy of the current matrix

public {Matrix2D} clone();

Back to top

.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

Back to top

.destroy

public this .destroy(void)

Will remove all event listeners and delete all properties as well as removing from the stage

Back to top

.getVersion

public this .getVersion()

Example

Crafty.getVersion(); //'0.5.4'