- Home /
For Loop Array Problem
Good morrow, good morrow,
I have this piece of code looping through the star scores and then producing either a gold, silver or bronze for the amount of stars collected for each level. The code seems only to work on the last item in the array. So all of the levels have the value of the 54th array item. In this case every level echo's out a gold trophy. This output is controlled by another bit of code which prints an array of the level, score and trophy.
trophy = new String[54];
starScores = new float[54];
for(var n : int = 0; n<starScores.length; n++){
starScores[n] = PlayerPrefs.GetFloat("s"+(n+1));
for(var j : int = 0; j<54; j++){
if(starScores[n] >= 5 && starScores[n] < 10){
trophy[j] = "gold";
} if(starScores[n] == 3 && starScores[n] < 5){
trophy[j] = "silver";
} if(starScores[n] == 0 && starScores[n] < 3){
trophy[j] = "bronze";
}
}
}
totalStarScore = starScores[0] + starScores[1] + starScores[2] + starScores[3] + starScores[4] + starScores[5] + starScores[6] + starScores[7] + starScores[8] + starScores[9] + starScores[10] + starScores[11] + starScores[12] + starScores[13] + starScores[14] + starScores[15] + starScores[16] + starScores[17];
array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
trophy[4],trophy[5],trophy[6],trophy[7],
trophy[8],trophy[9],trophy[10],trophy[11],
trophy[12],trophy[13],trophy[14],trophy[15],
trophy[16],trophy[17],trophy[18],trophy[19],
trophy[20],trophy[21],trophy[22],trophy[23],
trophy[24],trophy[25],trophy[26],trophy[27],
trophy[28],trophy[29],trophy[30],trophy[31],
trophy[32],trophy[33],trophy[34],trophy[35],
trophy[36],trophy[37],trophy[38],trophy[39],
trophy[40],trophy[41],trophy[42],trophy[43],
trophy[44],trophy[45],trophy[46],trophy[47],
trophy[48],trophy[49],trophy[50],trophy[51],
trophy[52],trophy[53]];
Any idea of the problem within this code i'm guessing its got something to do with the two for loops! Any help would be much appreciated. Thank You.
array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
trophy[4],trophy[5],trophy[6],trophy[7],
trophy[8],trophy[9],trophy[10],trophy[11],
trophy[12],trophy[13],trophy[14],trophy[15],
trophy[16],trophy[17],trophy[18],trophy[19],
trophy[20],trophy[21],trophy[22],trophy[23],
trophy[24],trophy[25],trophy[26],trophy[27],
trophy[28],trophy[29],trophy[30],trophy[31],
trophy[32],trophy[33],trophy[34],trophy[35],
trophy[36],trophy[37],trophy[38],trophy[39],
trophy[40],trophy[41],trophy[42],trophy[43],
trophy[44],trophy[45],trophy[46],trophy[47],
trophy[48],trophy[49],trophy[50],trophy[51],
trophy[52],trophy[53]];
This could be cleaned up with a loop :
for (var l:int = 0; l < 54; l ++)
{
array4[l] = trophy[l];
}
or even just simply :
array4 = new String[54];
array4 = trophy;
Answer by hirenkacha · Sep 13, 2012 at 12:01 PM
trophy = new String[54];
starScores = new float[54];
for(var n : int = 0; n<starScores.length; n++){
starScores[n] = PlayerPrefs.GetFloat("s"+(n+1));
if(starScores[n] >= 5 && starScores[n] < 10){
trophy[n] = "gold";
} if(starScores[n] >= 3 && starScores[n] < 5){
trophy[n] = "silver";
} if(starScores[n] >= 0 && starScores[n] < 3){
trophy[n] = "bronze";
}
}
totalStarScore = starScores[0] + starScores[1] + starScores[2] + starScores[3] + starScores[4] + starScores[5] + starScores[6] + starScores[7] + starScores[8] + starScores[9] + starScores[10] + starScores[11] + starScores[12] + starScores[13] + starScores[14] + starScores[15] + starScores[16] + starScores[17];
array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
trophy[4],trophy[5],trophy[6],trophy[7],
trophy[8],trophy[9],trophy[10],trophy[11],
trophy[12],trophy[13],trophy[14],trophy[15],
trophy[16],trophy[17],trophy[18],trophy[19],
trophy[20],trophy[21],trophy[22],trophy[23],
trophy[24],trophy[25],trophy[26],trophy[27],
trophy[28],trophy[29],trophy[30],trophy[31],
trophy[32],trophy[33],trophy[34],trophy[35],
trophy[36],trophy[37],trophy[38],trophy[39],
trophy[40],trophy[41],trophy[42],trophy[43],
trophy[44],trophy[45],trophy[46],trophy[47],
trophy[48],trophy[49],trophy[50],trophy[51],
trophy[52],trophy[53]];
I think this should work for you. No need of second for loop. Check rectified if conditions for silver and bronze also. Try and let me know if working fine.. thanks.
Superb Thankyou! I swear I tried that! But yes the answer was simple in the end! Thankyou @alucardj also your input shorten my code big time!
Your answer
Follow this Question
Related Questions
How to correctly shorten this script using arrays and iterations 2 Answers
For all elements above a certain index 1 Answer
Need help using GUI.Button/ input storage 1 Answer
Convert Array into One String (Js) 1 Answer
Help me Convert JS to C# 2 Answers