0

我是一个玩动作脚本的菜鸟,但我觉得这个问题是一个基本的编码问题我的项目类似于这张图片

我有四个象限区域(红色、蓝色、黄色和绿色),我正在向每个区域添加文本按钮,每个按钮中都有一个单词。每个部分有 16 个单词是从 4 个具有预设单词的数组(redWordArray、greenWordArray、yellowWordArray、blueWordArray)中添加的。单击时,文本按钮会使用发光过滤器发光,并且单词会被添加到另一个数组中以进行数据收集。例如,单击时将红色单词添加到红色数组(redChosenArray)。再次单击该单词时,它会删除辉光滤镜并从所选数组中删除。

我发现我的表现很慢,我想知道我是否正确有效地添加和删除单词。这些是我将辉光滤镜和所选单词添加到数组中的函数。我会喜欢你对最佳编码实践的见解,因为我确信它是一团糟!

谢谢!

 

function selectWord(event:MouseEvent):void { var tempWord:String = event.currentTarget.mood.text; var tempArray:Array; if (event.currentTarget.clicked == false) { event.currentTarget.filters = filterArray; event.currentTarget.clicked = true; tempArray = addToArray(tempWord) tempArray.push(tempWord); trace(redChosen); trace(blueChosen); trace(yellowChosen); trace(greenChosen); trace(""); }else if(event.currentTarget.clicked == true) { event.currentTarget.filters = emptyFilterArray; event.currentTarget.clicked = false; removeMoodWord(tempWord); trace(redChosen); trace(blueChosen); trace(yellowChosen); trace(greenChosen); trace(""); } } function addToArray(moodWord:String):Array { var wordFound:Boolean = false; var allWords:int = 16; var chosenArray:Array; while (!wordFound) { for (var h:int = 0; h < allWords; h++) { if (moodWord == redWords[h]) { chosenArray = redChosen; wordFound = true; }else if (moodWord == yellowWords[h]) { chosenArray = yellowChosen wordFound = true; }else if (moodWord == greenWords[h]) { chosenArray = greenChosen wordFound = true; }else if (moodWord == blueWords[h]) { chosenArray = blueChosen wordFound = true; } } } return chosenArray; } function removeMoodWord(moodWord:String):void { if (redChosen.indexOf(moodWord) >= 0) { redChosen.splice(redChosen.indexOf(moodWord), 1); }else if (blueChosen.indexOf(moodWord) >= 0) { blueChosen.splice(blueChosen.indexOf(moodWord), 1); }else if (yellowChosen.indexOf(moodWord) >= 0) { yellowChosen.splice(yellowChosen.indexOf(moodWord), 1); }else if (greenChosen.indexOf(moodWord) >= 0) { greenChosen.splice(greenChosen.indexOf(moodWord), 1); } i fee}

4

0 回答 0