Random Name Generator/Shuffle names with no repeats
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Random Name Generator/Shuffle names with no repeats

  1. #1
    Member
    Join Date
    Jun 2004
    Posts
    32

    Random Name Generator/Shuffle names with no repeats

    Hey everyone,
    I'm trying to make a little game where names are chosen randomly from a list and appear on stage each time the user clicks a button. I'm okay with making that happen, but I don't know how to make it so that the same name isn't chosen again. Eventually I'll have a list of about 250 people and I just want each person to be chosen once. I get the concept, but no idea how to make it happen. Can you help?

    I'm attaching a zip with the file in it that I'm working with.
    Thanks in advance!
    Attached Files Attached Files

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

    The code you have is a little rustic, but this will help you along the way.
    PHP Code:
    var parentContainer:DisplayObjectContainer DisplayObjectContainer(status_box);
    var 
    textField:TextField status_box.r_status;

    textField.autoSize TextFieldAutoSize.CENTER;
    textField.parentContainer.height 0.5 textField.textHeight 0.5;

    var 
    aArray:Array = new Array();
    var 
    dummyArray:Array = new Array();
    var 
    selectName:String;
    var 
    selectNumber:Number;

    var 
    myLoader:URLLoader = new URLLoader();
    myLoader.load(new  URLRequest("200-one-liners.txt"));
    myLoader.addEventListener(Event.COMPLETE,  aLoaded);

    function 
    aLoaded(event:Event):void
    {
        
    // *** Change imported data into array;
        
    aArray event.target.data.split("\r\n");
        
    // *** Get first name and remmove it from imported array;
        
    selectNumber Math.floor(Math.random() * (aArray.length 1));
        
    selectName aArray[selectNumber];
        
    // *** Remove selected from imported array;
        
    aArray.splice(selectNumber,1);
        
    // *** Spit out text value;
        
    status_box.r_status.text selectName;
    }

    function 
    clickHandler(event:MouseEvent):void
    {
        
    // *** Current button play status;
        
    event.currentTarget.gotoAndPlay("click");
        
    // *** If original array is empty;
        
    if (aArray.length 1)
        {
            
    trace("names empty");
            return;
        }
        else
        {
            
    // *** Get next name from array;
            
    selectNumber Math.floor(Math.random() * (aArray.length 1));
            
    selectName aArray[selectNumber];
            
    // *** Push to dummy array;
            
    dummyArray.push(selectName);
            
    // *** Remove next name from original array;
            
    aArray.splice(selectNumber,1);
            
    // *** Spit out text value;
            
    status_box.r_status.text selectName;
        }
        
    // *** Traces;
        
    trace("Dummy Array: " dummyArray);
        
    trace("Imported Array: " aArray);
        
    trace("-------------------------");
    }

    g_gen_button.addEventListener(MouseEvent.MOUSE_DOWNclickHandler); 
    you don't actually need the dummyArray, it's just there to show you what is happening

  3. #3
    Member
    Join Date
    Jun 2004
    Posts
    32
    Thank you so much! The notes are much appreciated too.

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

Poll by Flashkit.com