在 AS3 循环中应该如何编写?

发布于 2024-10-09 19:45:03 字数 1447 浏览 0 评论 0原文

我有一个 php 函数,它向 FlashBuilder 引入一个简单的数组集合(称为 QuestionSeries),其中包含各种值,包括“答案”和“结果”,我需要以可在 FlashBuilder 4 饼图中使用的格式,另一个数组集合(称为 ChartData) ) 看起来像:

ChartData = [0->[Answer->{Answer},Result->{Result}] , 1->[Answer->{Answer},Result->{Result }] , 2->[Answer->{Answer},Result->{Result}] , ... ]

但是,我需要调用的 [{Answer},{Result}] 对的数量取决于 QuestionSeries 数组集合中存在的数字,尽管该数字在 QuestionSeries 中由值“Num_Options”给出。

所以,目前我有一些脚本,看起来像这样:

    protected function graphsetup():void{   

    if(QuestionSeries.Num_Options>=1)
    {                   
        ChartData.addItem({Answer:QuestionSeries.Ans01, Result:QuestionSeries.Ans01_n})
    }
    if(QuestionSeries.Num_Options>=2)
    {                   
        ChartData.addItem({Answer:QuestionSeries.Ans02, Result:QuestionSeries.Ans02_n})
    }
    if(QuestionSeries.Num_Options>=3)
    {                   
        ChartData.addItem({Answer:QuestionSeries.Ans03, Result:QuestionSeries.Ans03_n})
    }
     ...

目前还可以,因为我只有 2 到 6 个答案,但是 a)即使在目前,这显然也不是最佳的 b)在某个时候我可能有最多 100 个答案。所以我基本上想将其放入一个聪明的 for 或 while 循环中,但是坦率地说,我的 ActionScripting 技能还不够好,我希望有人能够为我指明正确的方向?

我很挣扎,因为我无法说出 "QuestionSeries.Ans[i]"...,这是我最初尝试的关键。

我认为一种方法可能是对原始 QuestionSeries 数组进行排序(所有必需的值都位于 Ans[i] 和 Ans[i]_n 下,因此当按索引按字母顺序排序时它们相邻),然后从其末尾弹出或移动元素,但在动作脚本能力方面相当无能......我无法让它发挥作用。

预先非常感谢,我真的很感激任何人就这个话题可能要说的任何话。

乔什

I have a php function that brings in a simple arraycollection (called QuestionSeries) to FlashBuilder, with a variety of values including "Answers" and "Results" which I need in a format useable in a FlashBuilder 4 pie chart, another arraycollection (called ChartData) which looks something like:

ChartData = [0->[Answer->{Answer},Result->{Result}] , 1->[Answer->{Answer},Result->{Result}] , 2->[Answer->{Answer},Result->{Result}] , ... ]

However, the number of [{Answer},{Result}] pairs that I need to call in is dependent on the number that exist in the QuestionSeries arraycollection, although this number is given in QuestionSeries by a value, "Num_Options".

So, currently I have a bit of script that looks something like:

    protected function graphsetup():void{   

    if(QuestionSeries.Num_Options>=1)
    {                   
        ChartData.addItem({Answer:QuestionSeries.Ans01, Result:QuestionSeries.Ans01_n})
    }
    if(QuestionSeries.Num_Options>=2)
    {                   
        ChartData.addItem({Answer:QuestionSeries.Ans02, Result:QuestionSeries.Ans02_n})
    }
    if(QuestionSeries.Num_Options>=3)
    {                   
        ChartData.addItem({Answer:QuestionSeries.Ans03, Result:QuestionSeries.Ans03_n})
    }
     ...

Which is ok at the moment, as I only ever have between 2 and 6 answers but a) this obviously isn't optimal even at the moment and b) at some point I may have anything up to 100 answers. So I basically want to put it into a clever for- or while-loop, however my ActionScripting skills aren't quite up to scratch quite frankly and I was hoping someone might be able to point me in the right direction?

I struggle because I can't state "QuestionSeries.Ans[i]"..., which was the crux of my initial attempt.

I think one approach may be sort the original QuestionSeries array (all the required values come under Ans[i] and Ans[i]_n so they sit adjacent when sorted alphabetically by index) and then pop or shift elements off the end of it, but being fairly inept in terms of actionscript ability... I couldn't make it work.

Thanks a lot in advance, I'd really appreciate anything anyone might have to say on this topic.

Josh

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

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

发布评论

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

评论(1

沒落の蓅哖 2024-10-16 19:45:03

我认为您应该真正重新考虑数组集合中的结构和命名约定,以便更容易迭代。您想要迭代的内容从 1 开始并包含前导零。我认为最好将所有答案和结果作为元组包含在一个简单的索引数组中。

不过,如果这不是一个选择,您可以使用以下内容:

protected function addLeadingZero(number:int):String {
    if (number > 10) return number.toString();
    return "0" + number.toString();
}

protected function graphSetup():void {
    for (var i:int, l:int = int(QuestionSeries.Num_Options); i < l; i++) {
        ChartData.addItem({
            Answer:QuestionSeries["Ans" + addLeadingZero(i + 1)], 
            Result:QuestionSeries["Ans" + addLeadingZero(i + 1) + "_n"]
        });
    };
}

I think you should really rethink about the structure and the naming convention in your arraycollection for easier iteration. The things you want to iterate over begin from 1 and contain leading zeroes. And I think it would be better to contain all your answers and results as tuples in a simple indexed array.

Still if that's not an option, you can use something along the lines of:

protected function addLeadingZero(number:int):String {
    if (number > 10) return number.toString();
    return "0" + number.toString();
}

protected function graphSetup():void {
    for (var i:int, l:int = int(QuestionSeries.Num_Options); i < l; i++) {
        ChartData.addItem({
            Answer:QuestionSeries["Ans" + addLeadingZero(i + 1)], 
            Result:QuestionSeries["Ans" + addLeadingZero(i + 1) + "_n"]
        });
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文