Crafty
Crafty
is both an object, and a function for selecting entities.
-Its many methods and properties are discussed individually.
-Below is the documentation for use as a selector.
public EntitySelection Crafty( String selector)
- selector
A string representing which entities to select
-
public Entity Crafty( Number selector )
- selector
An entity's id
-
Select a set of or single entities by components or an entity's ID.
-Crafty uses syntax similar to jQuery by having a selector engine to select entities by their components.
-If there is more than one match, the return value is an Array-like object listing the ID numbers of each matching entity. If there is exactly one match, the entity itself is returned. If you're not sure how many matches to expect, check the number of matches via Crafty(...).length. Alternatively, use Crafty(...).each(...), which works in all cases.
-Note: You can treat an entity as if it was a selection of length 1 -- it implements all the same methods.
-Example
Crafty("MyComponent")
+
+
+ Crafty
+
+
+
+ Crafty
是一种对象,同时也是一种选择实体的方法。它的许多方法和属性单独讨论。下面是把它作为选择器使用时的文档。
+
+
+ public EntitySelection Crafty( String selector)
+
+ - selector
+ 代表所选实体的字符串
+
+
+ public Entity Crafty( Number selector )
+
+ - selector
+ 实体 id
+
+
+
+ 使用组件或实体 ID 选中一组或单个实体。
+ Crafty 的语法与 jQuery 相似,通过选择器引擎根据实体的组件选择实体。
+ 如果匹配到多个结果,返回值是类数组对象,对象中列出了所有匹配实体的 ID 数字。如果只匹配到一个结果,返回实体本身。 如果你不清楚匹配结果数量, 使用 Crafty(...).length 来检查匹配数量。 或者,使用 Crafty(...).each(...) 方法,它在所有情况下都适用。
+
+
+
+ 注意:你可以把一个实体看做长度 1 的选择 —— 它实现了所有相同的方法。
+
+
+
+ 示例
+
+ Crafty("MyComponent")
Crafty("Hello 2D Component")
- Crafty("Hello, 2D, Component")
The first selector will return all entities that have the component MyComponent
. The second will return all entities that have Hello
and 2D
and Component
whereas the last will return all entities that have at least one of those components (or).
- Crafty("*")
Passing *
will select all entities.
- Crafty(1)
Passing an integer will select the entity with that ID
.
-To work directly with an array of entities, use the get()
method on a selection.
-To call a function in the context of each entity, use the .each()
method.
-The event related methods such as bind
and trigger
will work on selections of entities.
-See Also
-
+ Crafty(
+ "Hello, 2D, Component")
+ 第一个选择器或返回所有含有 MyComponent
组件的实体。第二个会返回所有同时含有 Hello
、2D
和 Component
三个组件的对象。最后一个则会返回含有三个组件中至少一个的所有实体(或).
Crafty("*")
传递 *
会选中所有实体。
Crafty(1)
传递整数会选中对应 ID
的实体.
对选择结果使用 get()
方法,直接操作实体数组。
+ 使用 .each()
方法,在每个实体的上下文中调用函数。
绑定了如 bind
或 trigger
等方法的事件,会在选中的实体上生效。