-
[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:58 AM.
-
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 concatSentence( s1:String, s2:String, s3:String, s4:String ) : String { return s1 + ", " + s2 + " " + s3 + " " + s4 + "."; }
function crazyGrammer( ) : Void { for( var i:Number = 0; i < aSet1.length; i++ ) { for( var j:Number = 0; j < aSet2.length; j++ ) { for( var k:Number = 0; k < aSet3.length; k++ ) { for( var l:Number = 0; l < aSet4.length; l++ ) { aSentenceArray.push( concatSentence( aSet1[ i ], aSet2[ j ], aSet3[ k ], aSet4[ l ] ) ); } } } } } crazyGrammer( );
function getUniqueSentence( ) : String { if( aSentenceArray.length > 0 ) { return aSentenceArray.splice( random( aSentenceArray.length ), 1 ).toString() } else { return "No more sentences!"; } }
onMouseDown = function( ) { trace( getUniqueSentence() ) }
RipX
Last edited by RipX; 07-20-2008 at 12:22 PM.
-
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
-
Well call the getUniqueSentence( ) function 5 times then :|
PHP Code:
var aNew5SentenceArray:Array = new Array( );
for ( var i:Number = 0; i< 5; i++ ) { aNew5SentenceArray.push( getUniqueSentence( ) ); }
[ edit ]
I take it that you understand that this function always returns a different sentence!?
[ / edit ]
RipX
-
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
-
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
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|