A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [F8] URGENT!!!! Looping through array

  1. #1
    Member
    Join Date
    Jul 2008
    Location
    Čakovec, Croatia
    Posts
    34

    Unhappy [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 
    05i++) {
            var 
    chooseArr random(3) + 1;
            var 
    chooseElement random(4);
            
    chosenSentences[i] = this["niz" chooseArr][chooseElement];
            
    trace("chosenSentences original: "+chosenSentences[i]);
            
                var 
    0;
                while (
    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 0chosenSentences.lengthk++) {
            
    _root["plocica" k].pojam chosenSentences[k];
        } 

    // End of function
    stop();

    onLoad = function() {
        
    chooseSentences();

    Last edited by avukovi4; 07-20-2008 at 11:56 AM.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    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++;
    	}
    }

  3. #3
    Member
    Join Date
    Jul 2008
    Location
    Čakovec, Croatia
    Posts
    34

    Smile 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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center