Set of methods added to every single entity.
public this .setName(String name)
this.setName("Player");
public this .addComponent(String componentList)
,
public this .addComponent(String Component1[, .., String ComponentN])
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.
this.addComponent("2D, Canvas");
this.addComponent("2D", "Canvas");
public this .toggleComponent(String ComponentList)
,
public this .toggleComponent(String Component1[, .., String componentN])
Add or Remove Components from an entity.
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
public this .requires(String componentList)
Makes sure the entity has the components listed. If the entity does not have the component, it will add it.
public this .removeComponent(String Component[, soft])
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.
var e = Crafty.e("2D,DOM,Test");
e.removeComponent("Test"); //Soft remove Test component
e.removeComponent("Test", false); //Hard remove Test component
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
).
public this .attr(String property, * value)
public this .attr(Object map)
Use this method to set any property of the entity.
this.attr({key: "value", prop: 5});
this.key; //value
this.prop; //5
this.attr("key", "newvalue");
this.key; //newvalue
public this .toArray(void)
This method will simply return the found entities as an array.
public this .timeout(Function callback, Number delay)
The delay method will execute a function after a given amount of time in milliseconds.
Essentially a wrapper for setTimeout
.
Destroy itself after 100 milliseconds
this.timeout(function() {
this.destroy();
}, 100);
public this .bind(String eventName, Function callback)
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.
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
});
public this .unbind(String eventName[, Function callback])
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.
public this .trigger(String eventName[, Object data])
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.
public this .each(Function method)
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.
Destroy every second 2D entity
Crafty("2D").each(function(i) {
if(i % 2 === 0) {
this.destroy();
}
});
public this .setter(String property, Function callback)
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
public this .destroy(void)
Will remove all event listeners and delete all properties as well as removing from the stage