我是一个正在玩动作脚本的菜鸟,但是我觉得这个问题是一个基本的编码问题My project is similar to this picture。
我有四个象限区域(红色,蓝色,黄色和绿色),正在向每个区域添加文本按钮,每个按钮中只有一个单词。每个部分中有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}