【问题标题】:Efficient way to work with multiple arrays in AS3在 AS3 中处理多个数组的有效方法
【发布时间】:2014-02-12 19:23:06
【问题描述】:

我正在用 Flash/AS3 创建一个益智游戏。它使用大约 30 到 40 组单词。我想做的是在游戏开始时加载一组随机单词,在使用它们之后,我想加载另一组随机单词,依此类推。解决这个问题的最佳方法是什么?我应该使用基于随机数的 switch 语句选择多个数组,还是应该将它们全部放入一个多维数组中?每组单词最多可以包含 2000 个单词。

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    最好使用 xml 创建词组并轻松拉取它们。所以,我的意思是,使用多维数组很有效。

    var wordgroups:Array = new Array();
    wordgroups.push(new Array("word1", "word3", "word5"), new Array("word2", "word4", "word6")); //you can easily create whole this multidimensional array with xml
    
    wordgroups.shuffle(); //shuffles array (flashpunk library have this function, you may check it out and clone it's function if you can)
    
    var selectedwordgroup:Array;
    selectedwordgroup = wordgroups.pop(); //you can easily take a word group from that array and it removes selected wordgroup from the "wordgroups" array automatically with pop() function.
    

    XML 的东西呢

    这是一个示例 *.xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <wordstuff>
       <wordgroups>
          <group>word1,word2,word3,word4,word5,word6</group>
          <group>word7,word8,word9,word10,word11,word12</group>
          <group>word13,word14,word15,word16,word17,word18</group>
       </wordgroups>
    </wordstuff>
    

    这是一个从 xml 中提取数据的示例(您要将 xml 内容嵌入到游戏中,因此,您不需要任何 url 加载器对象,您可以像嵌入图像文件一样嵌入它进入你的游戏,有一点不同) 然后你可以用这个嵌入的 xml 创建数组

    var pulledgroups:Array = new Array();
    
    var my_xml:XML = WORDSTUFF //that's embedded file
    var wordgroups:XMLList = my_xml.wordgroups.group;
    var group:XML;
    for each (group in wordgroups)
    {
       var pulledwordgroup:Array = group.split(",");
       pulledgroups.push(pulledwordgroup);
    }
    

    THAT'S ALL,这个单一的代码段,同时拉取所有数据

    【讨论】:

    • 但你最好不要使用“shuffle”的东西,你的方式会带来更好的性能,我的意思是使用随机数并从词组中获取随机数组...
    • 我如何从 XML 创建这些数组?
    • 这很简单,我会在我的答案中添加一个小例子,只需一分钟
    • 完成了...检查一下...还有嵌入问题,您需要:[Embed(source="wordstuff.xml", mimeType="application/octet-stream")]public const WORDSTUFF:Class;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多