在Flash AS3中查找数组中对象的索引

发布于 2025-01-08 04:56:43 字数 612 浏览 0 评论 0原文

可能的重复:
flash as3 - 如何找到数组中对象的索引

我有一个类似于以下内容的对象数组:

[
    {
        start : 0.000,
        end   : 0.100
    },
    {
        start : 0.100,
        end   : 0.200
    },
    {
        start : 0.200,
        end   : 0.300
    }
]

是否有一种快速方法可以让我逐帧查询此对象以找出值(在本例中为音频播放的当前时间)谎言?

与我的同事聊天后,他们建议我将其枚举到一个大型索引值数组中,并引用它们所关联的索引,例如

[000] = 0
[001] = 0
...
[100] = 1
...
[200] = 2

Possible Duplicate:
flash as3 - how do I find an object's index in an array

I have an array of objects similar to:

[
    {
        start : 0.000,
        end   : 0.100
    },
    {
        start : 0.100,
        end   : 0.200
    },
    {
        start : 0.200,
        end   : 0.300
    }
]

Is there a quick way for me to query this object frame after frame to find out where a value (in this case the current time of a audio playback) lies?

After chatting with my coworkers, they have suggested I enumerate this into a large array of indexed values with references to which index they are associated with, for instance

[000] = 0
[001] = 0
...
[100] = 1
...
[200] = 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2025-01-15 04:56:43

索引策略将是最快的,但是您不需要这样做吗?:

["0.000"] = {};
["0.100"] = {};

或者,您可以在第一个示例中循环并选择相关对象,假设您的数组中不超过 1000 个对象应该足够快:

function find(start:Number):int
{
    for each(var i:Object in yourArray)
    {
        if(i.start == start) return yourArray.indexOf(i);
    }
}

这应该返回具有指定开始时间的对象的索引:

trace(find(0.1)); // 1

The indexing strategy would be the fastest, however don't you need to do it more like this?:

["0.000"] = {};
["0.100"] = {};

Alternatively you can loop through and select the relevant object in your first example, assuming that there's no more than say 1000 objects in your Array it should be more than fast enough:

function find(start:Number):int
{
    for each(var i:Object in yourArray)
    {
        if(i.start == start) return yourArray.indexOf(i);
    }
}

This should return the index of an object with the specified start time:

trace(find(0.1)); // 1
堇年纸鸢 2025-01-15 04:56:43

也许更好的解决方案是使用 Dictionary (链接)

字典是存储关联数据的快速且简单的方法。

虽然我不完全确定您要解决什么问题,所以其他数据类型可能更适合您。我只是让您知道您可以使用的另一个选择。

Maybe a better solution would be to use a Dictionary (link).

Dictionaries are fast and easy ways to store associative data.

Though I'm not entirely sure what problem you're trying to solve, so other data types may be more appropriate for you. I'm just letting you know of another option you have available to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文