-
[F8] URGENT!!!! Looping through array
SHORT DESCRIPTION:
I have 5 cards on the stage (named plocica0 to plocica4) and dynamic text (named pojam) in each.
Also, I have 3 different arrays (named niz1, niz2, niz3) with sentences. I want to populate these cards with sentences from the arrays but without repeating them - meaning each card should have different sentence inside.
The problem is that from time to time the mentioned repeating occurs.
My code:
PHP Code:
var niz1 = Array("Volim te.","Ljubavi moja.","Ti si zečić.","Danas je lijepi dan."); var niz2 = Array("Jučer je bilo lijepo.","Sutra je ponedjeljak.","Ti znaš plivati?","Danas radim."); var niz3 = Array("Dusche dich am Vormittag nicht!","Zeko piše.","Mario fotografira.","Mačke spavaju.");
function chooseSentences() { var chosenSentences = new Array();//container for chosen sentences var chosenIndex = new Array(); var chosenArrElement:Object = new Object(); for (var i = 0; i < 5; i++) { var chooseArr = random(3) + 1; var chooseElement = random(4); chosenSentences[i] = this["niz" + chooseArr][chooseElement]; trace("chosenSentences original: "+chosenSentences[i]); var j = 0; while (j < i) { if (chosenSentences[i] == chosenSentences[j]) { do { var popped:Object = chosenSentences.pop(); trace("popped: "+popped); chooseArr = random(3) + 1; chooseElement = random(4); chosenSentences[i] = this["niz" + chooseArr][chooseElement]; trace("chosenSentences new choice: "+chosenSentences[i]); } while (chosenSentences[i] == chosenSentences[j]); } else { j++; } } //end while (j<i)
chosenArrElement.array = chooseArr; chosenArrElement.element = chooseElement; chosenIndex[i] = [chosenArrElement.array,chosenArrElement.element]; } trace("final: "+chosenSentences); trace("chosenIndex: "+chosenIndex);
//writting into cards: for (var k = 0; k < chosenSentences.length; k++) { _root["plocica" + k].pojam = chosenSentences[k]; }
} // End of function stop();
onLoad = function() { chooseSentences(); }
Last edited by avukovi4; 07-20-2008 at 11:56 AM.
-
Maybe try something like this...
Code:
var j = 0;
while (j < i) {
if (chosenSentences[i] == chosenSentences[j]) {
do {
var popped:Object = chosenSentences.pop();
trace("popped: " + popped);
chooseArr = random(3) + 1;
chooseElement = random(4);
chosenSentences[i] = this["niz" + chooseArr][chooseElement];
trace("chosenSentences new choice: " + chosenSentences[i]);
j = 0;
} while (chosenSentences[i] == chosenSentences[j]);
} else {
j++;
}
}
-
For dawsonk
Tnx!!! I'm grateful for Your reply.
I'll try it as soon as possible (meaning right now ) and let You know how it works!!
Once again tnx!
Alexandra
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|