Efficiently navigating arrays and selected strings

45 Views Asked by At

I am a noob playing around with actionscript but i feel this question is a basic coding questionMy project is similar to this picture.

I have four quadrant areas (Red, blue, yellow, and green) that I am adding text buttons to each area with a single word in each button. There are 16 words in each section that are added from 4 arrays that have the preset words (redWordArray, greenWordArray, yellowWordArray, blueWordArray). When clicked, the text button glows using a glow filter and the word gets added to another array for data collecting. For instance, a red word when clicked gets added to a red array (redChosenArray). When the word is clicked again, it removes the glow filter and is removed from the chosen array.

I am finding that my performance is slow and I am wondering if I am adding and deleting words correctly and efficiently. These are my functions for adding the glow filters and the selected word to the array. I would love your insights for best coding practices as I am sure it is a mess!

Thank you!

 

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}

0

There are 0 best solutions below