-
[RESOLVED] Display Largest Variable in an array
AS2.0 Flash 6
Context: Making a guessing game where up to 20 users guess times. A countdown timer script runs at the game start. As time goes by, flash compares the guess with the real time, moves a movieclip (horse1, horse2 etc) along the x axis and displays an integer (time between the guess and ever changing clock [horse1.displaybox1, horse2.displaybox2, horse3.displaybox3, etc]). This is all working.
How to I display the current closest guesser?
I TRIED to avoid arrays, but doubt I can. JUST learning about arrays and for loops, but found this elegant code to find the max number in an array (or with modification could get the min):
maxValue = function (array) {
mxm = array[0];
for (i=0; i<array.length; i++) {
if (array[i]>mxm) {
mxm = array[i];
}
}
return mxm;
};
k = new Array();
k = [1, 5, 33, 21];
trace(maxValue(k)); //I get back the expected result (33)
Hardcoding those numbers in is great in tests, but I want those numbers to be the displayboxes:
k = new Array();
k = [horse1.displaybox1, horse2.displaybox2, horse3.displaybox3, etc];
trace(maxValue(k));
The displayboxes have variable names, and I've tried giving them instance names, played with this. and _root. and to be honest it's 80% trial and error, and 20% understanding.
I have a feeling this is a simple pathway solution, but I can't find it after days of searching!
Ultimately I'd like to have a dynamic textbox display the leader's name and or picture as the game goes on, and a runner up display would make me a hero!
Best,
Farmer
FYI when I hard-code numbers in:
k = new Array();
k = [1, 5, 33, 21];
trace(maxValue(k));
I get back the expected result (33)
-
Did you already trace the values that you´re putting in the array?
so where you have trace(maxValue(k)); put on the next line: trace(horse1.displaybox1); and the rest of the horses display boxes... what values do you get?
since you're moving the horses over an X-axis (and I don't quite understand your way of working with displayboxes etc.), perhaps an easy way could be:
Actionscript Code:
var k:Array = new Array(); var totalhorses:Number = 20; // the amount of horses in the game for (var i:Number = i; i <= totalhorses; i++){ k.push = _root["horse" + i]._x; } trace(maxValue(k));
By the way: I like the idea for your game! is it already playable??? I'd like to play it sometime!
-
P.S. I would give the displayboxes instancenames and provide them with their values in a way like:
Actionscript Code:
var totalhorses:Number = 20; // the amount of horses in the game for (var i:Number = i; i <= totalhorses; i++){ _root["horse" + i]["displaybox" + i].text = this["horsescore"+ i]; }
where this["horsescore" + i] should be replaced with the variable you've stored the integer in...
-
Clarification time
Basm, Thanks for the reply! Much appreciated.
Originally Posted by basm
Did you already trace the values that you´re putting in the array?
This is kind of the crux of my problem, I don't really know how to plug data INTO arrays. Like I mentioned, hard-coding a series of numbers into the array in actionscript works fine and gives me the desired results, but what I want, is to plug in data that is being generated DURING the game.
The array I have in there so far is a supplementary dud for now! At best I get it to trace "00" or 'undefined'.
Take a look at the frankenstein prototype I have up so far: www.breakdownboys.com/basketball/2minutedrill
It's for making the ends of boring basketball games more exciting. The players guess how much time will ACTUALLY pass after the two-minute mark. Time-outs, fouls, and commercial breaks always extends the action.
I call them horses in the code, but the MC-s are players. What this prototype lacks, is a box at the top that says "The current leader is: (whomever)". The players move to the right of the canvas, and whomever is the closest to the right edge, is the current leader. It's a nice visual now, but with 20 players, it could get ugly, and I want to have something clear at the top that shows who's leading. And it would be REALLY cool to know who is the current runner up! (+ I love learning new tools for the toolbox!)
The 'horses' have display boxes over their heads that I will probably hide in the finished product, but currently display the difference in milisecs from the actual time and the guess. Whichever box is closest to 0 is the current leader, because they guessed the closest at that time.
Hope this is clearer. And thanks again for your brain-time!
+F
-
To return the largest number of an array, surely you could just use the sort() method and then get the last element. Surely more efficient than the loop etc.
So if I had:
PHP Code:
var arr = [12,55,8,3,97,41,2]; function maxValue(array){ var newArr = array.sort(); return newArr[newArr.length-1]; }
Then trace(maxArr(arr)); would return 97 (the highest number in the array).
-
Breakthrough! Almost there...
Thanks lightspeed for weighing in. We're still not on the same page though.
I had a code that would sort just fine, but only when I hard coded the
jumble of numbers, whereas I need one that sorts my variables.
Sure enough I played with the naming and pathways, and rather than worry
about the textboxes, I just took the data from the equations that were
running in the action script.
I'd love to use your more elegant solution, but I hope you meant Acionscript
and not PHP, because A) I would REALLY be out of my league with the PHP
and B) that would explain why I couldn't make it work as you laid it out.
Even in it's own test page. (I'm so new to arrays though that you have to
spell it out for me, I cut and paste what you suggested).
As it stands I got my original to work with some labeling patience.
Actionscript Code:
minValue = function (array) { mn = array[0]; for (i=0; i<array.length; i++) { if (array[i]<mn) { mn = array[i]; } } return mn; };
k = new Array(); k = [stable.horse1.disp1,stable.horse2.disp2,stable.horse3.disp3,]; //the horses are now contained in an mc called stable trace(minValue(k)); leader = (minValue(k));
I'm so excited... now I realize that I want the value CLOSEST to 0. So I have
to figure out some way to get Math.abs(); involved for the absolute of these
variables.
-
Done!
Well I got it all to work. Thanks for the sounding board.
+F
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
|