A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Array game

  1. #1
    Always needs help marinebro0306's Avatar
    Join Date
    Jul 2005
    Location
    Philadelphia
    Posts
    166

    Array game

    I am trying to make a computer version of CatchPhrase, which is also like Password. When I press a button, I want a random word from an array to display in a dynamic text box. It's probably very simple, but I simply can't find the explanation for this.

    I know how to make an array, but how do I make it so a random word is shown at the press of the button?
    This could also be comparible to an 8-ball game, where when you press, a random word/phrase pops up.

  2. #2
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    in first frame
    code:

    myWords=new Array("one","two","three")
    totalWords=myWords.length


    function randRange(min,max) {
    randomNum= Math.floor(Math.random()*(max-min))+min;
    return randomNum
    }



    then in button or other event put
    code:

    on (release){
    currentWord=myWords[randRange(0,totalWords)];
    }


  3. #3
    Member
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    58

    Random Array number

    Hai there,

    maybe i think to simple but what about generating an number with the
    nr=(Math.random() * 100); function. This wil give you a number between 0 and 100. then you can call the array with i thought Array[nr] and put it in a variable with vartxt1=Array[nr]

    is that possible?

    greetings Rob
    Last edited by RobVos; 09-10-2005 at 04:28 AM.

  4. #4
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    isn't that basically what I said?

    using things like totalWords keeps you from having to rework code when the length of the array changes.

    The randRange function I just find usefull in general that way if I want to get a number between say 10-100 I can.

    Now if you want each element only used once, like when dealing cards from an array of 52 cards what I'd do is to make a second array and using the random number to fill it with random elements from the first array always checking that it hasn't been used before then you can just pull from the array from the top and remove the array element just like dealing a shuffled deck of cards.

  5. #5
    Always needs help marinebro0306's Avatar
    Join Date
    Jul 2005
    Location
    Philadelphia
    Posts
    166
    Quote Originally Posted by blanius
    Now if you want each element only used once, like when dealing cards from an array of 52 cards what I'd do is to make a second array and using the random number to fill it with random elements from the first array always checking that it hasn't been used before then you can just pull from the array from the top and remove the array element just like dealing a shuffled deck of cards.
    So...how do I do that?

  6. #6
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Ok took a bit of work to get it going.

    try this
    http://bretlanius.com/flash/shuffle.html

    and source is
    http://bretlanius.com/flash/shuffle.fun

    you've have to actually shuffle a copy of the original array to be very usefull but this should get you going.

    The meat of this is the following prototype function. By using a this method all arrays in your movie can easily be "shuffled"
    code:

    Array.prototype.shuffle = function() {
    len = this.length;
    for (var i=0; i < len; i++) {
    rand = Math.floor(Math.random()*len);
    //swap current index with a random one
    temp = this[i];
    this[i] = this[rand];
    this[rand] = temp;
    }
    }

    Last edited by blanius; 09-10-2005 at 03:49 PM.

  7. #7

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