PDA

View Full Version : Array question?


Flash Numpty
08-29-2003, 09:12 AM
Hi guys, I'm trying to build a simple formulator 'thing'.

I'm working on making a simple horse racing game which has an incorporated form calculating device.

Firstly you enter the number of horses in the race(10 for eg).
Secondly you fill out the last four race results for each of the 10 horses, points are given depending on the result and are added together as a total for each horse. The one with the lowest number should therefore have the best form and should be 1st past the post. It is this adding together of each horses total points that I'm stuck with!? As well as then displaying the infomation in order of which horse should come 1st, 2nd,3rd and so on..... I'm trying to populate arrays and alsorts but I really haven't a clue!..

I would like it coded in such a way that other infomation can be added aswell as the form. But for now I would like to hear any suggestions on how to dynamically set up the code so it gives the result and displays the 1st, 2nd, 3rd order when a button is pressed.

I've atached what I have got so far...... Cheers guys!

lochwinnoch
08-29-2003, 09:40 AM
Not *totally*clear on what you are asking.

I tried the FLA and by clicking the buttons, the totals then increased.

If you are then talking about displaying these totals in ascending order (for 1st, 2nd, 3rd etc) then surely:

array.sort()

will do it?

If that's not what you mean then can you explaing a bit better. Break it down to 1 question at a time; the last post was asking a few things at once and it gets diluted.

Flash Numpty
08-29-2003, 09:47 AM
ok well basically at the moment theres just numbers flying round everywhere. I need to put those into an Array or arrays.... which can sort them into the lowest number first and diplay NOT as the total points but the horse number ie horse 3 - 1st
horse 2 - 2nd
horse 6 -3rd .... and so on.

Clearer?

lochwinnoch
08-29-2003, 10:18 AM
Hmm. OK. Property arrays (ie objects)?

horse1 = {race1:4, race2:8, race3:2, race4:1};
horse1.makeTotal = function () {
this.myTotal = 0;
for (var i=0; i < 4; i++) {
this.myTotal += this["race"+i];
}
}

This will create a (rough) horse object with race values. When a user clicks one of your form buttons, eg the 1st button of the 1st horse, activate this code:

horse1.race1 ++;

Then, to order the horses:

allHorses = new Array();
// hCount here is taken from your input box at the start
for (var x=1; x < hCount; x++) {
// add the horse objects to the horses array
allHorses.push (_root["horse"+x]);
}

Now need to sort the array, based on the values of myTotal for each horse. I think I've embedded the data too much now.

Will get back to ya...

Flash Numpty
08-29-2003, 10:21 AM
OK!! thanks.

Flash Numpty
08-29-2003, 11:39 AM
Any joy with that mate ?

lochwinnoch
08-29-2003, 03:09 PM
Just got home from work. Had a think during my hour-long train ride. Will knock it into shape and post an FLA I think...

lochwinnoch
08-30-2003, 08:09 AM
Success!

array.sortOn is good, but converts values to a string which is no good for a number-based sort. Problem is that with this list:

1,5,15,9,20

it will sort to:

1,15,20,5,9

coz the strings 1 and 15 are closer than 1 and 5. Had to append a 0 to any value less than 10 to fix it. So above example becomes:

01,05,15,09,20

this sorts correctly to:

01,05,09,15,20

Anyways - I rewrote your code into OOP code as it makes sense to me that a horse is an object with properties such as raceTotal, racesRun etc. It's not 100% done as I have to go and do some other Flash now. It does all you wanted, just that the Horse class needs to be registered with a movieclip containing the buttons and text boxes. Should be easy.

FLA:

Flash Numpty
09-01-2003, 05:00 AM
Excellent !! .. well its certainly given me a better idea of how to approach coding the calclator. Thanks v much !!