flex游戏引擎(PushButton)-查找实体,查找组件

本文介绍了如何通过代码或XML从名称查找实体和组件,包括使用IEntity接口的方法进行查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值