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()
A set of methods added to every single entity.
-Methods
- .addComponent()
- .attr()
- .bind()
- .clone()
- .defineField()
- .destroy()
- .each()
- .get()
- .getId()
- .getName()
- .has()
- .one()
- .removeComponent()
- .requires()
- .setName()
- .setter()
- .timeout()
- .toArray()
- .toggleComponent()
- .trigger()
- .unbind()
- .uniqueBind()
.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.
-For adding multiple components, you can either pass a string with -all the component names (separated by commas), or pass each component name as -an argument.
-If the component has a function named init
it will be called.
If the entity already has the component, the component is skipped (nothing happens).
-Example
this.addComponent("2D, Canvas");
-this.addComponent("2D", "Canvas");
.attr()
Events
- Change [Data = {key: value}]
- when properties change
public this .attr(String property, Any value[, Boolean silent[, Boolean recursive]])
- property
Property of the entity to modify
-
- value
Value to set the property to
-
- silent
If you would like to supress events
-
- recursive
If you would like merge recursively
-
Use this method to set any property of the entity.
-public this .attr(Object map[, Boolean silent[, Boolean recursive]])
- map
Object where each key is the property to modify and the value as the property value
-
- silent
If you would like to supress events
-
- recursive
If you would like merge recursively
-
Use this method to set multiple properties of the entity.
-Setter options:
--
-
silent
: If you want to prevent it from firing events.
-recursive
: If you pass in an object you could overwrite sibling keys, this recursively merges instead of just merging it. This isfalse
by default, unless you are using dot notationname.first
.
-
public Any .attr(String property)
- property
Property of the entity to modify
-
- [Returns]
Value - the value of the property
-
Use this method to get any property of the entity. You can also retrieve the property using this.property
.
Example
this.attr({key: "value", prop: 5});
-this.attr("key"); // returns "value"
-this.attr("prop"); // returns 5
+
+
+ Crafty Core
+
+ 事件
+
+
+ - NewEntityName [entity name = {String}]
+ - 实体设置新名字后
+
+
+ - NewComponent [Component = {String}]
+ - 实体增加新组件时
+
+
+ - RemoveComponent [Component = {String}]
+ - 实体移除组件时
+
+
+ - Remove
+ - 调用 .destroy() 移除组件时
+
+
+
+ 每个单独实体增加的一组方法
+
+ 方法
+
+ - .addComponent()
+ - .attr()
+ - .bind()
+ - .clone()
+ - .defineField()
+ - .destroy()
+ - .each()
+ - .get()
+ - .getId()
+ - .getName()
+ - .has()
+ - .one()
+ - .removeComponent()
+ - .requires()
+ - .setName()
+ - .setter()
+ - .timeout()
+ - .toArray()
+ - .toggleComponent()
+ - .trigger()
+ - .unbind()
+ - .uniqueBind()
+
+ 回到顶部
+ .addComponent()
+ public this .addComponent(String componentList)
+
+ - componentList
+ 以逗号 ,
隔开的组件字符串
+
+
+
+ public this .addComponent(String Component1[, .., String ComponentN])
+
+ - Component#
+ 新增组件的 ID。
+
+
+
+
+ 为选中实体增加一个组件。
+ 组件用来拓展实体功能。它会复制属性、分配方法来增强实体功能。
+ 你可以通过传递包含所有组件名的字符串(以逗号隔开),或者传递各个组件名作为参数来增加多个组件。
+ 如果组件有 init
方法,方法会被调用
+ 如果实体已经有了该组件,该组件会被忽略(无事发生)。
+
+ 示例
this.addComponent("2D, Canvas");
+this.addComponent("2D", "Canvas");
+
+
+ 回到顶部
+ .attr()
+ 事件
+
+
+ - Change [Data = {key: value}]
+ - 属性改变时
+
+
+
+ public this .attr(String property, Any value[, Boolean silent[, Boolean recursive]])
+
+ - property
+ 实体需要修改的属性
+
+
+
+ - value
+ 给属性赋的值
+
+
+
+ - silent
+ 如果你想要抑制事件
+
+
+
+ - recursive
+ 如果你想要递归地合并
+
+
+
+ 使用该方法给实体设置任意属性。
+
+ public this .attr(Object map[, Boolean silent[, Boolean recursive]])
+
+ - map
+ 该对象的键:要修改的属性,值:属性值
+
+
+
+ - silent
+ 如果你想要抑制事件
+
+
+
+ - recursive
+ 如果你想要递归地合并
+
+
+
+
+ 使用该方法给实体设置多个属性。
+ 设置选项:
+
+ silent
:如果你想阻止它启动事件。
+ recursive
:如果你传入的对象会覆盖兄弟键,它会递归地合并,而不是简单合并。 默认是 false
,除非你使用点号 name.first
。
+
+
+ public Any .attr(String property)
+
+ - property
+ 实体需要修改的属性
+
+
+
+ - [Returns]
+ 值 - 属性的值
+
+
+
+ 使用该方法获得实体任意属性。你也可以使用 this.property
取得属性值。
+
+ 示例
this.attr({key: "value", prop: 5});
+this.attr("key"); // 返回 "value"
+this.attr("prop"); // 返回 5
this.key; // "value"
this.prop; // 5
-this.attr("key", "newvalue");
-this.attr("key"); // returns "newvalue"
+this.attr("key", "newvalue");
+this.attr("key"); // 返回 "newvalue"
this.key; // "newvalue"
this.attr("parent.child", "newvalue");
this.parent; // {child: "newvalue"};
-this.attr('parent.child'); // "newvalue"
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 executed synchronously.
-Example
this.attr("triggers", 0); //set a trigger count
-this.bind("myevent", function() {
- this.triggers++; //whenever myevent is triggered, increment
+this.attr('parent.child'); // "newvalue"
+
+ 回到顶部
+ .bind()
+ public this .bind(String eventName, Function callback)
+
+ - eventName
+ 要绑定的事件名
+
+
+
+ - callback
+ 事件触发后要执行的方法
+
+
+
+
+ 附加当前实体来监听事件。
+ 传递的事件触发后会调用回调函数。根据事件的不同,一些数据可以通过参数传递给回调函数。
+ 第一个参数是事件名(可能是任意值),同时第二个参数是一个回调函数。如果事件有数据,回调函数应该有一个参数。
+ 事件是任意的,并提供组件之间的通信。即使它不存在,也可以触发或绑定事件。
+ 与 DOM 事件不同,Crafty 事件是同步执行的。
+
+ 示例
this.attr("triggers", 0); //设置一个触发器计数
+this.bind("myevent", function() {
+ this.triggers++; //每当触发 myevent 时,递增
});
-this.bind("EnterFrame", function() {
- this.trigger("myevent"); //trigger myevent on every frame
-});
Back to top.clone()
public Entity .clone(void)
- [Returns]
Cloned entity of the current entity
-
Method will create another entity with the exact same
-properties, components and methods as the current entity.
-Back to top.defineField()
public this .defineField(String property, Function getCallback, Function setCallback)
- property
Property name to assign getter & setter to
-
- getCallback
Method to execute if the property is accessed
-
- setCallback
Method to execute if the property is mutated
-
Assigns getters and setters to the property.
-A getter will watch a property waiting for access and will then invoke the
-given getCallback when attempting to retrieve.
-A setter will watch a property waiting for mutation and will then invoke the
-given setCallback when attempting to modify.
-Example
var ent = Crafty.e("2D");
-ent.defineField("customData", function() {
+this.bind("EnterFrame", function() {
+ this.trigger("myevent"); // 每帧触发 myevent
+});
+
+
+ 回到顶部
+ .clone()
+ public Entity .clone(void)
+
+ - [Returns]
+ 当前实体的克隆
+
+
+
+ 该方法将创建具有与当前实体相同的属性、组件和方法的另一个实体。
+
+ 回到顶部
+ .defineField()
+ public this .defineField(String property, Function getCallback, Function setCallback)
+
+ - property
+ 要分配 getter 和 setter 的属性名
+
+
+
+ - getCallback
+ 属性被访问执行的方法
+
+
+
+ - setCallback
+ 属性被改变执行的方法
+
+
+
+ 分配 getter 和 setter 给属性。getter 将监视等待访问的属性,然后在尝试检索时调用给定的取值回调函数。setter 将监视等待改变的属性,然后在尝试修改时调用给定的赋值回调函数。
+
+ 示例
var ent = Crafty.e("2D");
+ent.defineField("customData", function() {
return this._customData;
-}, function(newValue) {
+}, function(newValue) {
this._customData = newValue;
});
-ent.customData = "2" // set customData to 2
-Crafty.log(ent.customData) // prints 2
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.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) {
+ent.customData = "2" // 设置 customData 为 2
+Crafty.log(ent.customData) // 打印 2
+
+
+ 回到顶部
+ .each()
+ public this .each(Function method)
+
+ - method
+ -
+
每次迭代调用的方法
+
+
+
+
+ 迭代发现的实体,为每个实体调用函数。
+ 为每个实体调用这个方法,并且把迭代的索引作为参数传递。方法的上下文(this
的值)是迭代中的当前实体。
+
+ 示例
销毁次序为偶数的 2D 实体
+Crafty("2D").each(function(i) {
if(i % 2 === 0) {
this.destroy();
}
-});
Back to top.get()
public Array .get()
- [Returns]
An array of entities corresponding to the active selector
-
public Entity .get(Number index)
- [Returns]
an entity belonging to the current selection
-
- index
The index of the entity to return. If negative, counts back from the end of the array.
-
Example
Get an array containing every "2D" entity
-var arr = Crafty("2D").get()
Get the first entity matching the selector
-// equivalent to Crafty("2D").get()[0], but doesn't create a new array
-var e = Crafty("2D").get(0)
Get the last "2D" entity matching the selector
-var e = Crafty("2D").get(-1)
Back to top.getId()
public Number .getId(void)
- [Returns]
the ID of this entity.
-
For better performance, simply use the this[0] property.
-Example
Finding out the ID
of an entity can be done by returning the property 0
.
+});
+
+ 回到顶部
+ .get()
+ public Array .get()
+
+ - [Returns]
+ 与活动选择器相对应的一组实体
+
+
+
+ public Entity .get(Number index)
+
+ - [Returns]
+ 属于当前选择的实体
+
+
+
+ - index
+ 将要返回的实体的索引。如果为负,从数组末尾计数。
+
+
+
+ 示例
获取包含所有“2D”实体的数组
+var arr = Crafty("2D").get()
获取第一个匹配选择器的实体
+// 等同于 Crafty("2D").get()[0],但是不能创建新数组
+var e = Crafty("2D").get(0)
获取匹配选择器的最后一个“2D”实体
+var e = Crafty("2D").get(-1)
+
+ 回到顶部
+ .getId()
+ public Number .getId(void)
+
+ - [Returns]
+ 实体 ID
+
+
+
+ 为了更好的性能,简单地使用 this[0] 属性。
+
+ 示例
返回属性 0
可以找出实体 ID
。
var ent = Crafty.e("2D");
ent[0]; //ID
- ent.getId(); //also ID
Back to top.getName()
public this .getName(String name)
- [Returns]
A human readable name for debugging purposes.
-
Get the human readable name for debugging purposes.
-Example
var ent = Crafty.e().setName("Player");
-var name = ent.getName();
See Also
Back to top.has()
public Boolean .has(String component)
- component
The name of the component to check
-
- [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.one()
public Number one(String eventName, Function callback)
- eventName
Name of the event to bind to
-
- callback
Method to execute upon event triggered
-
- [Returns]
ID of the current callback used to unbind
-
Works like Crafty.bind, but will be unbound once the event triggers.
-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.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.
-(In the current version of Crafty, this function behaves exactly the same
-as addComponent
. By convention, developers have used requires
for
-component dependencies -- i.e. to indicate specifically that one component
-will only work properly if another component is present -- and used
-addComponent
in all other situations.)
-See Also
Back to top.setName()
public this .setName(String name)
- name
A human readable name for debugging purposes.
-
Set a human readable name for debugging purposes.
-Example
var ent = Crafty.e().setName("Player");
See Also
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.
-This feature is deprecated; use .defineField() instead.
-See Also
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() {
+ ent.getId(); //也是 ID
+
+ 回到顶部
+ .getName()
+ public this .getName(String name)
+
+ - [Returns]
+ 用于调试目的的对人友好的名称。
+
+
+
+ 获取用于调试目的的易读名称。
+
+ 示例
var ent = Crafty.e().setName("Player");
+var name = ent.getName();
+ 另请参阅
+
+
+
+ 回到顶部
+ .has()
+ public Boolean .has(String component)
+
+ - component
+ 要检查的组件的名称
+
+
+
+ - [Returns]
+ true
或 false
取决于实体是否有给定组件。
+
+
+
+
+ 为了更好的性能,简单地使用 .__c
对象,如果实体有给定组件,该对象为 true
,否则,该对象不存在(或者是 false
)。
+
+
+ 回到顶部
+ .one()
+ public Number one(String eventName, Function callback)
+
+ - eventName
+ 要绑定的事件名称
+
+
+
+ - callback
+ 事件出发时执行的函数
+
+
+
+ - [Returns]
+ 用来解绑的当前回调函数的 ID
+
+
+
+ 作用类似 Crafty.bind,但是事件触发后就会解绑。
+
+ 另请参阅
+
+ - .bind
+
+
+
+ 回到顶部
+ .removeComponent()
+ public this .removeComponent(String Component[, soft])
+
+ - component
+ 要移除的组件
+
+
+
+ - soft
+ 是否软移除(默认 true
)
+
+
+
+
+ 从实体移除一个组件。软移除(默认)只能避免 .has()
返回 true。硬移除会移除所有关联属性和方法。
+
+ 示例
var e = Crafty.e("2D,DOM,Test");
+e.removeComponent("Test"); //软移除 Test 组件
+e.removeComponent("Test", false); //硬移除 Test 组件
+
+
+ 回到顶部
+ .requires()
+ public this .requires(String componentList)
+
+ - componentList
+ 必须添加的组件列表
+
+
+
+
+ 确保实体有列出的组件。如果没有,新增组件。
+ (当天 Crafty 版本中,该方法的行为与 addComponent
完全相同。根据惯例,开发者使用 requires
进行组件依赖 —— 例如:具体地指出,只有当另一个组件存在时,一个组件才能正常工作 —— 在其他所有情况下使用 requires
。)
+
+ 另请参阅
+
+ - .addComponent
+
+
+
+ 回到顶部
+ .setName()
+ public this .setName(String name)
+
+ - name
+ 出于调试目的的对人友好的容易理解的名字。
+
+
+
+ 设置出于调试目的的对人友好的容易理解的名字。
+
+ 示例
var ent = Crafty.e().setName("Player");
+ 另请参阅
+
+
+
+ 回到顶部
+ .setter()
+ public this .setter(String property, Function callback)
+
+ - property
+ 被监视是否被修改的属性
+
+
+
+ - callback
+ 属性被修改后的回调函数
+
+
+
+ 将监视等待修改的属性,然后在尝试修改时调用给定的回调函数。
+ 不建议使用这个功能;使用 .defineField()
+
+ 另请参阅
+
+ - .defineField
+
+
+
+ 回到顶部
+ .timeout()
+ public this .timeout(Function callback, Number delay)
+
+ - callback
+ 在给定毫秒数后执行的函数
+
+
+
+ - delay
+ 延迟执行函数的毫秒数
+
+
+
+
+ 延迟方法将在给定的毫秒数后执行一个函数。
+ 本质上是 setTimeout
的包装器。
+
+ 示例
100 毫秒后销毁自身
+this.timeout(function() {
this.destroy();
-}, 100);
Back to top.toArray()
public this .toArray(void)
This method will simply return the found entities as an array of ids. To get an array of the actual entities, use get()
.
-See Also
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.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 executed synchronously.
-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.
-Back to top.uniqueBind()
public Number .uniqueBind(String eventName, Function callback)
- eventName
Name of the event to bind to
-
- callback
Method to execute upon event triggered
-
- [Returns]
ID of the current callback used to unbind
-
Works like Crafty.bind, but prevents a callback from being bound multiple times.
-See Also
-
-
+
.toArray()
+public this .toArray(void)
该方法将简单地将发现的实体返回为一个 id 数组。要获得真实实体列表,使用 get()
。
另请参阅
+-
+
- .get +
.toggleComponent()
+public this .toggleComponent(String ComponentList)
+ -
+
- ComponentList +
以逗号
+,
隔开的将要添加或删除的组件字符串
+
public this .toggleComponent(String Component1[, .., String componentN])
+ -
+
- Component# +
将要添加或删除的组件 ID。
+
+
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
.trigger()
+public this .trigger(String eventName[, Object data])
+ -
+
- eventName +
要触发的事件
+
+
-
+
- data +
将被作为参数传入每个回调函数的任意数据
+
+
触发携带任意数据的事件。将会调用所有带有当前实体对象的上下文(this
的值)的回调函数。
注意:这只会在当前实体内执行回调函数,没有其他实体。
+第一个参数是要出发的事件的名字,可选参数是任意事件数据。这可以是任何东西。
+与 DOM 事件不同,Crafty 事件是同步执行的。
+ +.unbind()
+public this .unbind(String eventName[, Function callback])
+ -
+
- eventName +
要解绑的事件的名称
+
+
-
+
- callback +
要解绑的回调函数
+
+
从当前实体移除绑定的事件。
+传递一个事件名称,将移除所有与该事件绑定的事件。传递回调函数的引用只会解绑该回调函数。
+ + +