A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: FLASH CS4 - Need to compare three values

  1. #1
    Junior Member
    Join Date
    Nov 2016
    Posts
    3

    Exclamation FLASH CS4 - Need to compare three values

    Good morning to the collective! I have THRE variables containing randomly generated numbers. I need to determine programmatically which TWO are the largest numbers. What is the simplest way to do that in ActionScript 2.0? I'm trying If Else statements but can't seem to get the pattern correct. Thanks in advance!

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Put them in an array and sort the array numerically.

    PHP Code:
    var number1:Number Math.round(Math.random() * 10000);
    var 
    number2:Number Math.round(Math.random() * 10000);
    var 
    number3:Number Math.round(Math.random() * 10000);

    // *** use above vars or below math

    var numberArray:Array = new Array();

    numberArray.push({name "Number 1"value Math.round(Math.random() * 10000) });// or your number vars instead of the math
    numberArray.push({name "Number 2"value Math.round(Math.random() * 10000) });
    numberArray.push({name "Number 3"value Math.round(Math.random() * 10000) });

    numberArray.sortOn("value",Array.DESCENDING | Array.NUMERIC);

    for (var 
    i:Number 0numberArray.lengthi++)
    {
        
    trace(numberArray[i].name ", " numberArray[i].value);

    I'm sure there might be other perhaps easier ways of doing it but you have a nice object array to work with there

  3. #3
    Junior Member
    Join Date
    Nov 2016
    Posts
    3

    Thumbs up Thank you! May I ask for a little more help?

    Thank you thank you! I used the array structure to produce the 3 values in the correct order. Actually, I changed the order to ASCENDING and made i equal to 1 so it only reports the 2 numbers I need. However, I'm afraid my meager skills require a bit more assistance.

    To briefly explain, I am developing a "Family Feud" game to be played live and operated by an operator (me), moderated by a live host (client). I am faced with 6 Teams playing 2 at a time, resulting in 3 winners. I then need to programmatically determine the two highest scores (which you have already done for me) and using the variables already established, populate a final round without knowing in advance who the two teams will be.

    I now need to take these 2 highest numbers (game scores) which already equate to their individual numeric variables (round1HighScore, round2HighScore and round3HighScore), and produce their associated team name variables (round1Winner, round2Winner and round3Winner). I am again trying to use a series of "if" and "else if" statements nested in the for/next loop to determine each score's equivalent variable and then declare the associated name variable as one of the 2 finalists.

    Confused yet?

    I'd be happy to copy and paste what I have but I'm afraid my skills stopped developing years ago as the live gaming is all I really use Flash for and once I have built the games they just need re-skinned/customized per job. I'm still stuck in ActionScript 2 and do everything in the longest most convoluted way possible using the most basic commands. I'm sure there are much simpler ways to do what I've already done, but so far it all works. This 3 into 2 challenge has me stymied though.

    Thanks again for any help you can offer,
    Brian






    Quote Originally Posted by fruitbeard View Post
    Hi,

    Put them in an array and sort the array numerically.

    PHP Code:
    var number1:Number Math.round(Math.random() * 10000);
    var 
    number2:Number Math.round(Math.random() * 10000);
    var 
    number3:Number Math.round(Math.random() * 10000);

    // *** use above vars or below math

    var numberArray:Array = new Array();

    numberArray.push({name "Number 1"value Math.round(Math.random() * 10000) });// or your number vars instead of the math
    numberArray.push({name "Number 2"value Math.round(Math.random() * 10000) });
    numberArray.push({name "Number 3"value Math.round(Math.random() * 10000) });

    numberArray.sortOn("value",Array.DESCENDING | Array.NUMERIC);

    for (var 
    i:Number 0numberArray.lengthi++)
    {
        
    trace(numberArray[i].name ", " numberArray[i].value);

    I'm sure there might be other perhaps easier ways of doing it but you have a nice object array to work with there

  4. #4
    Junior Member
    Join Date
    Nov 2016
    Posts
    3
    Thank you! I submitted a response earlier but I don't see it now. I need a bit more help if you can spare it.

    First, briefly what I'm doing. I only use Flash to create live, audience interactive games for large business meetings. My main line of work is Presentations, and so I rarely use Flash, which is also why I am stuck in ActionScript 2.0. I have developed a Family Feud game designed for live operation (by me) and driven by a live host (client). The game is structured for 3 "rounds" of 2 teams each (total of 6 teams). I basically have 2 sets of variables, one for the team name that won each round, and one for the winning score of each round. The variables are roundXHighScore and roundXWinner (where X is 1-3) The problem for me arises in how to take the 3 winners and using the two highest scores, define those two winning team names as the final two teams. i.e.: finalTeam1 = round1Winner and finalTeam2 = round3Winner

    Using If statements nested in the for loop of the Array you provided I am able to determine the first final player, but am unable to determine the second one. Here's what I've done:

    var numberArray:Array = new Array();

    numberArray.push({name : "Number 1", value : round1HighScore });// or your number vars instead of the math
    numberArray.push({name : "Number 2", value : round2HighScore });
    numberArray.push({name : "Number 3", value : round3HighScore });

    numberArray.sortOn("value",Array.ASCENDING | Array.NUMERIC);

    for (var i:Number = 1; i < numberArray.length; i++) {

    if (numberArray[i].value == round1HighScore){
    finalTeam1=round1Winner;
    }
    else if (numberArray[i].value == round2HighScore){
    finalTeam1=round2Winner;
    }
    else if (numberArray[i].value == round3HighScore){
    finalTeam1=round3Winner;
    }

    trace(numberArray[i].name + ", " + numberArray[i].value);
    }

    I am admittedly not a guru in any sense, and tend to use what I know even if it creates a long and laborious script. As long as the final product works no one cares how it was built, and I simply don't use the software often enough to overcome my shortcomings.

    Any further assistance you can offer would be greatly appreciated.

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