dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: loadVars()...how do I call randomly?

  1. #1
    Media Developer CJElven's Avatar
    Join Date
    Jan 2002
    Location
    San Antonio, TX
    Posts
    298
    As I mentioned before, I'm trying to switch over to loadVars instead of the now (apparently) outdated loadVariablesNum. However, I can't seem to figure out how to do something that is very easy with my loadVariablesNum.

    I have a random facts section with a text file with 155 random science facts. For one thing, the new Math.random is no good for creating random variables...unless you want something between 0 and 1. Very annoying. So, good old random(0,topnumber) for me.

    Anyway, the old code ran like this
    Code:
    function factreload() {
    	newfact = random(factsText.numFacts);
    	myFacts = eval("fact"+newfact+"txt");
    }
    and every time you press a button or call factreload, it would change the fact being displayed. Now, however, after having used the loadVars() tool, I can't seem to figure out how I call the various variables out of my newly created TextObject. Can anyone help? The text file is loaded into an object called factsHolder...
    Code:
    factsHolder = new loadVars();
    factsHolder.load("facts.txt");
    factsHolder.onLoad = function(success) {
    };
    Or do I have to stick with the loadVariablesNum? Does anyone know what can be done?

  2. #2
    Senior Member chi-styler's Avatar
    Join Date
    Jul 2001
    Location
    Bristol, UK
    Posts
    1,237
    Personally, I would do it like this

    in txt file

    myFacts=Fact 1|Fact 2|Fact 3

    Code:
    factsHolder = new LoadVars();
    factsHolder.load("facts.txt");
    factsHolder.onLoad = function(success) {
      if(success) {
        myFactsArray = factsHolder.myFacts.split("|");
        randNum = Math.floor(Math.random() * myFactsArray.length);
        myTextBox.text = myFactsArray[randNum];
      }
    }

  3. #3
    Media Developer CJElven's Avatar
    Join Date
    Jan 2002
    Location
    San Antonio, TX
    Posts
    298
    Now, maybe I'm not understanding the point of Math.floor and Math.random...but wouldn't that particular combination return 0 every time? After all, the Math.random is only supposed to return a number between 0 and 1, and Math.floor rounds it down to the nearest integer or whole number. That's always 0! Maybe Math.random returns something larger than 1...but I can't find out how to do that. So, no matter how much you multiply the Array length times your new integer...it should always be 0/Zip/Zero/Zilch. Anyway, I'd like to understand why I'm doing what I'm doing before I do it.

  4. #4
    Senior Member chi-styler's Avatar
    Join Date
    Jul 2001
    Location
    Bristol, UK
    Posts
    1,237
    Yes, Math.random() on its own will give you a number between 0 and 1. So, you multiply it (by myFactsArray.length in my example)

    Just try this in a new movie

    num = 3;
    randNum = Math.floor(Math.random() * num);
    trace(randNum);

    that will give a integer which is either 0, 1, or 2.

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