flex游戏发动机(PushButton)-查找实体,查找组件
flex游戏引擎(PushButton)-查找实体,查找组件
Looking Up Entities
Looking up an entity by name is simple. You can do it from code, or via the level file.
1.
<!-- An empty, named entity. -->
2.
<
entity
name
=
"NamedEntity"
>
3.
<!-- Components would go here, although an empty entity is valid. -->
4.
</
entity
>
1.
// How to look it up from code:
2.
var
namedEntity:IEntity = PBE.lookupEntity(
"NamedEntity"
);
1.
<!-- How to reference an entity from XML. -->
2.
<
entity
name
=
"ExampleEntity"
>
3.
<
component
name
=
"NameExampleComponent"
>
4.
<!-- EntityReference is a field of type IEntity. The entityName attribute indicates that we want to set it to a reference to the named entity.-->
5.
<
EntityReference
entityName
=
"NamedEntity"
/>
6.
</
component
>
7.
</
entity
>
Looking Up Components
You can look up components by name in several ways. You can look them up in code or XML. You can also look them up via the NameManager, or via methods on IEntity.
1.
<!-- A named entity, with some named components. -->
2.
<
entity
name
=
"NamedEntity"
>
3.
<!-- The type of these components are irrelevant for the examples in this section. -->
4.
<
component
type
=
"ExampleComponent"
name
=
"A"
/>
5.
<
component
type
=
"AnotherComponent"
name
=
"B"
/>
6.
<
component
type
=
"AnotherComponent"
name
=
"C"
/>
7.
</
entity
>
01.
// Looking up a component by type and entity name. This returns component "A".
02.
var
componentA:ExampleComponent = PBE.lookupComponentByType(
"NamedEntity"
, ExampleComponent) as ExampleComponent;
03.
04.
// Look up a component by entity name and component name. This returns component "B".
05.
var
componentB:AnotherComponent = PBE.lookupComponentByName(
"NamedEntity"
,
"B"
) as AnotherComponent;
06.
07.
// You can also look up components on an entity (either your owner as shown
08.
// here, or a reference gotten via the NameManager or another means). This
09.
// code snippet would have to be run from a component in NamedEntity, as
10.
// otherwise owner would not reference the right thing.
11.
var
componentA:ExampleComponent = owner.lookupComponentByName(
"A"
);
You can also look up another component by type, as mentioned in the component chapter.
01.
<!-- How to reference a component from XML. -->
02.
<
entity
name
=
"ExampleEntity"
>
03.
04.
<!-- This is a component that we will reference by name in the other component. -->
05.
<
component
type
=
"AnotherComponent"
name
=
"D"
/>
06.
<
component
name
=
"ComponentNameExampleComponent"
>
07.
<!-- ComponentReference is a field that has a component type (ExampleComponent in this case). The entityName attribute indicates that we want to look up the first component with matching type on the named entity.-->
08.
<
ComponentReference
entityName
=
"NamedEntity"
/>
09.
<!-- AnotherComponentReference is a field that has a component type (AnotherComponent in this case). entityName is as above, but componentName lets you indicate the specific component you want, based on name. -->
10.
<
AnotherComponentReference
entityName
=
"NamedEntity"
componentName
=
"C"
/>
11.
12.
<!-- Finally, you can reference components on the same entity as you by only using the componentName attribute. -->
13.
<
YetAnotherComponentReference
componentName
=
"D"
/>
14.
</
component
>
15.
</
entity
>