通过 id[0]、id[1]、id[2] 引用组件(可能通过使用 Repeater 或 DataGroup)
我正在尝试将游戏从纯 AS3 移植到 Flex4。
首先,我想对代码进行尽可能少的更改。
所以我有一个包含 3 个用户头像的游戏场:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comps="*">
<comps:User id="user0" x="20" y="20"/>
<comps:User id="user1" x="250" y="20"/>
<comps:User id="user2" x="125" y="250"/>
</s:Application>
它工作正常,但我有以下问题:
我更愿意通过 user[ 0 ]、user[ 1 ]、user[ 2 ] 引用这些自定义组件。
而不是像上面代码中那样由 user0、user1、user2 执行。
如何实现这一目标?我可能需要使用 Repeater 或 DataGroup (有什么区别),但是如何处理不同的坐标?
我是否应该引入一个坐标数组(或 ArrayCollection?或 ArrayList?这让我在 Flex 中感到困惑)并以某种方式使用它?
更新:
我尝试了以下方法:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comps="*">
<fx:Declarations>
<s:ArrayCollection id="myAC" source="{myDP}"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
public var myDP:Array = [
{ x: 20, y: 20 },
{ x: 250, y: 20 },
{ x: 120, y: 250 },
];
]]>
</fx:Script>
<mx:VBox>
<mx:Repeater id="r" dataProvider="{myAC}">
<comps:User id="user" x="{r.currentItem.x}" y="{r.currentItem.y}"/>
</mx:Repeater>
</mx:VBox>
</s:Application>
但是组件绘制在错误的位置(在垂直行中)...
I'm trying to port a game from pure AS3 to Flex4.
At first I'd like to make as little changes in my code as possible.
So I have a playing field with 3 user avatars:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comps="*">
<comps:User id="user0" x="20" y="20"/>
<comps:User id="user1" x="250" y="20"/>
<comps:User id="user2" x="125" y="250"/>
</s:Application>
It works ok, but I have the following problem:
I'd prefer to refer to those custom components by user[ 0 ], user[ 1 ], user[ 2 ].
And not by user0, user1, user2 as it is in the code above.
How to achieve this? I probably need to use Repeater or DataGroup (what's the difference), but how do I handle the differing coordinates?
Should I introduce an array (or ArrayCollection? or ArrayList? this confuses me in Flex) of coordinates and somehow use it?
UPDATE:
I've tried the following:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comps="*">
<fx:Declarations>
<s:ArrayCollection id="myAC" source="{myDP}"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
public var myDP:Array = [
{ x: 20, y: 20 },
{ x: 250, y: 20 },
{ x: 120, y: 250 },
];
]]>
</fx:Script>
<mx:VBox>
<mx:Repeater id="r" dataProvider="{myAC}">
<comps:User id="user" x="{r.currentItem.x}" y="{r.currentItem.y}"/>
</mx:Repeater>
</mx:VBox>
</s:Application>
but the components are drawn at wrong positions (in a vertical row)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该创建用户元素的数组或 ArrayCollection;从概念上讲是这样的:
您可以编写创建用户对象所需的任何逻辑。从概念上讲,这正是我使用 Flextras Calendar 来创建日子的方法。根据显示的日历月份,我们决定创建多少天,然后创建它们并将它们存储在数组中。您可以使用任何逻辑来创建适合您的游戏的用户。
Repeater 和 DataGroup 都不是数组。他们不会为您创建数组或 dataProvider,并且不允许您像数组一样访问元素。
中继器基本上是循环的 MXML 版本,我相信应该不惜一切代价避免它们。 MXML 并不是一种好的循环语言。
DataGroup 旨在根据您指定的数据(dataProvider)及其布局类(垂直、水平或平铺)显示内容。在我看来,你想要一个自定义布局;所以我不确定是否建议使用数据组进行显示。
是的,我会使用数组。 ArrayCollection 只是数组的包装器,它提供 API 来过滤和排序数据。 ArrayList 只是一个实现 IList 接口的数组,因此它可以用作 DataGroup 或其他 ListBase 类(例如 DataGrid)的 dataProvider。
您发布的代码不是我推荐的方法。如果在 ActionScript 中执行此操作,您将可以更精细地控制组件的定位和布局。您可能会受益于阅读Flex 组件生命周期。< /a> 了解 UIComponent 如何在屏幕上绘制自己;以及如何融入其中以“让事情发挥作用”。在这种情况下,“让东西发挥作用”与你的定位有关。
我知道你是在帖子开头而不是结尾时这么说的,但我想问“为什么?” Flex 并不是为游戏而设计的;它是为游戏而设计的。我不确定我是否会推荐将其作为游戏引擎。如果您想创建菜单或高分列表,我可以看到它可能对您有利。但是,您应该能够轻松地并排使用 AS3 代码和 Flex 代码,而无需重新编写/修改游戏。
You should create an array, or ArrayCollection, of user elements; conceptually something like this:
You can write whatever logic you need to create the user objects. This is, conceptually, exactly what I do with the Flextras Calendar to create the days. Based on the calendar month that is being displayed, we decide how many days to create, and create them and store them in an array. You can use whatever logic to create your users that is appropriate to your game.
Neither a Repeater, nor a DataGroup, are arrays. They won't create an array, or dataProvider, for you, and would not allow you to access your elements as if they were an array.
A repeater is basically the MXML version of a loop and I believe they should be avoided at all costs. MXML is just not a good language for looping.
A DataGroup is intended to display things based on the data you specify (dataProvider) and it's layout class (Vertical, or Horizontal, or Tile). It sounds to me like you want a custom layout; so I'm not sure I'd recommend using a DataGroup for display.
Yes, I would use an array. An ArrayCollection is just a wrapper for an array that provides APIs to filter and sort the data. An ArrayList is just an array that implements the IList interface so it can be used as a dataProvider to a DataGroup or other ListBase classes, such as the DataGrid.
The code you posted isn't an approach I'd recommend. You'll have much more granular control over the component's positioning and layout if you do this in ActionScript. You may benefit from reading up on the Flex Component LifeCycle. to understand how the UIComponents draw themselves on the screen; and how you can hook into that to "make stuff work." In this case, the "make stuff work relates to your positioning.
I know you said this at the beginning of your post and not at the end, but I wanted o ask the question "Why?" Flex wasn't designed for games; and I'm not sure I'd recommend sing it as a game engine. I can see where it may benefit you if you want to create menus or high score lists. But, you should be able to use your AS3 code and Flex code side by side easily enough w/o having to re-write/rework the game.