在 AS3 循环中应该如何编写?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该真正重新考虑数组集合中的结构和命名约定,以便更容易迭代。您想要迭代的内容从 1 开始并包含前导零。我认为最好将所有答案和结果作为元组包含在一个简单的索引数组中。
不过,如果这不是一个选择,您可以使用以下内容:
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: