A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Random Name with Text File. Help

  1. #1
    Junior Member
    Join Date
    Sep 2006
    Posts
    29

    Random Name with Text File. Help

    I want to make a random name selector for choosing pupils in class. The below works, but I was wondering if there was a way that I could set it up so that I could just copy and paste a list of names to the text file, ie have the " &name2 =" in the actionscript or in a different file. It would be so much quicker and useable for non IT staff.

    This is what I have:
    Action script:
    loadVariablesNum("names.txt", 0);
    count = Number(random(numvars)) + 1;
    name = eval("name" add count) ;
    stop();

    text file:
    name1= mary
    &name2= john
    &name3= peter
    &name4= paul
    &name5= sarah
    &name6= charlie
    &numvars=6



    Any help would be gratefully received
    Cheers

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    stop();
    var namesArray;
    var tmpArray;
    var count;
    var name;
    var myLV = new LoadVars();
    // ------------ start LoadVars ---------------- \\
    myLV.onLoad = function(success) {
    	if (success) {
    		for (var i in myLV) {
    			tmpArray = escape(i).split("%0D%0A");
    			break;
    		}
    		namesArray = new Array();
    		for (var i = 0; i<tmpArray.length; i++) {
    			if (tmpArray[i].length != 0) {
    				namesArray.push(tmpArray[i]);
    			}
    		}
    		//trace(namesArray);
    		count = Number(random(namesArray.length));
    		name = namesArray[count];
    		trace(name);
    	} else {
    		trace("Ooops there is something wrong - better check it out.");
    	}
    };
    myLV.load('names.txt');
    text file:
    Code:
    mary
    john
    peter
    paul
    sarah
    charlie

  3. #3
    Junior Member
    Join Date
    Sep 2006
    Posts
    29
    Fantastic!
    Many thanks Dawsonk. It works a treat.
    Cheers

  4. #4
    Junior Member
    Join Date
    Sep 2006
    Posts
    29
    Hi again
    As I said, it works great. Is possible though, to repeat the same script but with variables changed to have other random things on the same stage. With the original code I posted, I could use that on different frames, changing the var and the text file. I just tried the same with yours but know joy. Is it possible to do?
    Thanks

  5. #5
    :
    Join Date
    Dec 2002
    Posts
    3,518
    frame 1
    Code:
    stop();
    
    var name = new Array();
    var colour = new Array();
    var letter = new Array();
    
    function getRandInfo(fileName, info) {
    	var myLV = new LoadVars();
    	myLV.onLoad = function(success) {
    		if (success) {
    			for (var i in myLV) {
    				var tmpArray = escape(i).split("%0D%0A");
    				break;
    			}
    			var infoArray = new Array();
    			for (var i = 0; i<tmpArray.length; i++) {
    				if (tmpArray[i].length != 0) {
    					infoArray.push(tmpArray[i]);
    				}
    			}
    			var count = Number(random(infoArray.length));
    			info.push(infoArray[count]).toString();
    			play()
    		} else {
    			info.push("error").toString();
    			trace("Ooops there is something wrong - better check it out.");
    		}
    	};
    	myLV.load(fileName);
    }
    
    
    getRandInfo('names.txt',name);
    frame 2
    Code:
    stop();
    trace("Name: "+name);
    
    getRandInfo('colours.txt',colour);
    frame 3
    Code:
    stop();
    trace("Colour: "+colour);
    
    getRandInfo('letters.txt',letter);
    frame 4
    Code:
    stop();
    trace("Letter: "+letter);

  6. #6
    Junior Member
    Join Date
    Sep 2006
    Posts
    29
    That's great, thanks!
    I'm going to try it out now.

  7. #7
    Junior Member
    Join Date
    Sep 2006
    Posts
    29
    Just to say it works perfectly and will be a great fun in class.
    Cheers

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