View Full Version : [AS2] Randomizing LoadVars content, what am i doing wrong?? 4 game!

04-17-2007, 08:49 PM
Ok...this is a very small part of the complete code from a game im creating and i would really appreciate some help on this function:

mcBtnComputer.onRelease = function() {
//Goal: perform random computer selection
var computerCheck:String = new String();
//computerCheck string going to make sense later
var tStats:Array = new Array();
tStats[0] = "mojo";
tStats[1] = "flavour";
tStats[2] = "yeah";
//randomize tNum from 0 to 2
tNum = random(2);
computerCheck = tStats[tNum];
//computerCheck = mojo or flavour or yeah (up to here works fine)
//loadVars as tGirls2 (working fine)
var tGirls2:LoadVars = new LoadVars();
tGirls2.onLoad = function() {
//HERE I NEED HELP: if i try: computerSelection = tGirls2.mojo; <--works fine
//but if i try as below its not working and i need it like this because i want to
//randomize it
computerSelection = Number(tGirls2.computerCheck);
//computerSelection = NAN ... why?? remember before on
//trace computerCheck was displaying or mojo or flavour or yeah

the txtfile has 3 variables
&mojo = 10&
&flavour = 8&
&yeah = 6&

04-18-2007, 02:51 AM
Unfortunately, the amount of source you provided does not allow me to completely debug your application. You may have a problem with your text files, or with variables declared outside the scope of this function (computercheck). However, there is one thing you can do to help your debugging. trace thirls2.computercheck (without the Number conversion) and see what it is. When a Number is traced it can only show a string representation of a Number, undefined or null. Any other value will come up as NaN. Trace the value before conversion and see what it is. That may give you some clue as to what's wrong.

04-18-2007, 04:07 AM
Hey J! first of all thanx for replying...I tried what u suggested and its giving undefined, but anyway cant seem to address the issue...

im guessing the problem is on this line:

computerSelection = Number(tGirls2.computerCheck);

i've tried:

computerSelection = Number(tGirls2[computerCheck]);
computerSelection = (tGirls2[computerCheck]);
computerSelection = Number(tGirls2.computerCheck);

Still not working...still getting undefined when i try this first and then the line above :


But...if i try: (is giving me the correct amount of letters on the TXT file!!, and not the amount of undefined) (????)


04-18-2007, 04:42 AM
J!! i realized what was the problem. This is the correct code:

computerCheck = (tStats)[tNum];

Output: mojo, flavour or yeah

Thanks for your help it was very helpfull!

04-25-2007, 06:21 PM
Glad to help!

The cornerstone of good programming is good debugging.