A Flash Developer Resource Site

Results 1 to 7 of 7

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

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

    Angry [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:58 AM.

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    I quickly worte this for you!

    PHP Code:
    var aSet1:Array = new Array( "Hello""Howdy""Yo");
    var 
    aSet2:Array = new Array( "How""Where""What" );
    var 
    aSet3:Array = new Array( "is""do""can" );
    var 
    aSet4:Array = new Array( "it""I" );

    var 
    aSentenceArray:Array = new Array( );

    function 
    concatSentences1:Strings2:Strings3:Strings4:String ) : String {
        return 
    s1 ", " s2 " " s3 " " s4 ".";
    }

    function 
    crazyGrammer( ) : Void {
        for( var 
    i:Number 0aSet1.lengthi++ ) {
            for( var 
    j:Number 0aSet2.lengthj++ ) {
                for( var 
    k:Number 0aSet3.lengthk++ ) {
                    for( var 
    l:Number 0aSet4.lengthl++ ) {
                        
    aSentenceArray.pushconcatSentence(  aSet1], aSet2], aSet3], aSet4] ) );
                    }
                }
            }
        }
    }
    crazyGrammer( );

    function 
    getUniqueSentence( ) : String {
        if( 
    aSentenceArray.length ) {
            return 
    aSentenceArray.splicerandomaSentenceArray.length ), ).toString()
        } else {
            return 
    "No more sentences!";
        }
    }

    onMouseDown = function( ) {
        
    tracegetUniqueSentence() )

    RipX
    Last edited by RipX; 07-20-2008 at 12:22 PM.

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

    Smile For RipX

    Tnx for posting Your Reply so quickly it's great, but I don't need only one sentence at time but five different sentences at time (quoted as they are written in those arrays) and pushed in one new array (I've called it chosenSentences), because later I'll write those sentences into five different cards which will be dragged into boxes.

    But for my game I need them to be different and there should be five of them. I'm creating a game for my pupils in which they will drag those cards into three boxes (according to three different categories) and I want to avoid any repeating of sentences in one sequence

    If you can help with that I would really be grafeful!!!!

    Alex

  4. #4
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Well call the getUniqueSentence( ) function 5 times then :|

    PHP Code:
    var aNew5SentenceArray:Array = new Array( );

    for ( var 
    i:Number 0i5i++ ) {
       
    aNew5SentenceArray.pushgetUniqueSentence( ) );

    [ edit ]
    I take it that you understand that this function always returns a different sentence!?
    [ / edit ]


    RipX

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

    Thumbs up For RipX

    Of course I do!!!

    I just have to adjust Your code because I don't need to concat parts of sentences into new ones and get out as many new sentences as possible, but I need to get five different sentences, meaning from those in arrays, but different among themselves (ones they've been populated into new 5-elements array).

    I just want to have five different cards on the stage when movie's loaded.

    Tnx I'll try to adjust it,

    and once again I can't thank You enough

    Alexandra

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

    Red face Needed procedure explanation

    Well, English isn't my mother tongue so don't resent...

    I need next procedure (if possible):

    - choose five sentences from given arrays (without changing them)
    - push them into new array
    - then, if possible, compare those five sentences (in that new array) among themselves and if there are two which are the same repeate (loop) the procedure of choosing sentence as long as all five of them aren't different

    I've tried that with my code and everything is o.k. but from time to time, when a new sentence is chosen, althought it's same as any of previous, the procedure of choosing a new one is repeated only once and not while any of two in a row of five are the same.

    I hope I've succeeded to explain my problem

  7. #7
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    These will not repeat sentences! lol

    I wrote this so that it creates all unique possible combinations of sentences and pushes them into an array. The 'getUniqueSentence( )' method slices one out of the array until there are no more left, because it has been sliced out, it couldn't possibly duplicate the sentence!


    Maybe, I'm misunderstanding something here.. Sorry!

    RipX

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