PDA

View Full Version : External variables load but are not used


mas_bass
04-20-2010, 03:59 PM
Hi all,

I have the following variables inside an AS2 script

var data_a = 19;
var data_b = 20;
var data_c = 27;
var data_d = 24;
var data_e = 10;
var data_f = 5;
var data_g = 24;
var data_h = 20;
var data_i = 19;

var day_b = "Sunday"
var day_c = "Monday"
var day_d = "Tuesday"
var day_e = "Wednesday"
var day_f = "Thursday"
var day_g = "Friday"
var day_h = "Saturday"


as I need to load them from an external file, I have replaced them with

varReceiver = new LoadVars();
varReceiver.load("data.txt");
varReceiver.onLoad = function(){
trace(this.data_a);
trace(this.data_b);
};

and I put all the above variables in a data.txt file as follows:
data_a=19&data_b=20&data_c=27&data_d=24&data_e=10&data_f=5&data_g=24&data_h=20&data_i=19&day_b=Sunday&day_c=Monday&day_d=Tuesday&day_e=Wednesday&day_f=Thursday&day_g=Friday&day_h=Saturday

when I test the movie the trace window shows data_a and data_b but the rest of the script does not work and gives undefined everywhere. It seems like the variables are properly loaded but the rest of the script cannot use them...


var dataY:Array = new Array(data_a, data_b, data_c, data_d, data_e, data_f, data_g, data_h, data_i);
var dayArray = new Array(day_b, day_c, day_d, day_e, day_f, day_g, day_h);
var limitY = 0;
var miniY = dataY[0];
for (var i in dataY) {
limitY = Math.max(limitY, dataY[i]);
miniY = Math.min(miniY, dataY[i]);
}
limitY += 40;
miniY -= 40;
var sizeY = 300;
var multY = sizeY/(limitY-miniY);

makeChart();

function makeChart() {
var contentMC = _root.createEmptyMovieClip("contentMC", _root.getNextHighestDepth());
contentMC._x = 500;
contentMC._y = 100;
drawAxis(contentMC,600,sizeY);
makeTxtBox(contentMC,limitY,-10,300,"right",0x000000);
makeTxtBox(contentMC,miniY,-10,20,"right",0x000000);
var chartMC = contentMC.createEmptyMovieClip("chartMC", contentMC.getNextHighestDepth());
chartMC.lineStyle(2,0x000000,20);
chartMC.moveTo(25,(-(dataY[0]-miniY)*multY));
for (var i = 1; i<dataY.length-1; i++) {
var point = makeMark(chartMC, i*65, -((dataY[i]-miniY)*multY));
chartMC.lineTo(point._x,point._y);
var infoTxt = dayArray[i-1]+newline+dataY[i];
var overunder = (i%2 == 1) ? 40 : -10;
makeTxtBox(chartMC,infoTxt,(i*65),((dataY[i]-miniY)*multY)+overunder,"center",0xFFFFFF);
}
chartMC.lineTo(dataY.length*65-25,-((dataY[dataY.length-1]-miniY)*multY));
}
function makeMark(mc, x1, y1) {
var markMC = mc.createEmptyMovieClip("markMC"+mc.getNextHighestDepth(), mc.getNextHighestDepth());
markMC.lineStyle(3,0xFF0000);
markMC.beginFill(0xFF0000,100);
markMC.moveTo(-1,1);
markMC.lineTo(1,1);
markMC.lineTo(1,-1);
markMC.lineTo(-1,-1);
markMC.lineTo(-1,1);
markMC.endFill();
markMC._x = x1;
markMC._y = y1;
return markMC;
}
function drawAxis(mc, x2, y2) {
mc.lineStyle(2,0x000000,100);
mc.moveTo(0,0);
mc.lineTo(0,-y2);
mc.moveTo(0,0);
mc.lineTo(x2,0);
mc._x += 50;
mc._y += y2+50;
}
function makeTxtBox(mc, infoTxt, posX, posY, align, color) {
var my_fmt:TextFormat = new TextFormat();
my_fmt.color = 0x000000
my_fmt.font = "Verdana";
my_fmt.size = 12;
my_fmt.align = "center";
var my_txt = mc.createTextField("my_txt", mc.getNextHighestDepth(), posX, -posY, 0, 0);
my_txt.autoSize = align;
my_txt.multiline = true;
my_txt.text = infoTxt;
my_txt.setTextFormat(my_fmt);
}

vinayak.kadam
04-21-2010, 07:14 AM
This is definetly due to the data types. When u accept the numbers from text file, they may be accepted as strings instead of numbers. You need to type cast them. Where ever u are using data_a or data_b type cast them i.e. convert to numbers using Number(data_a), Number(data_b) and so on.

Check with the above change, if still does not works then upload ur FLA here and we could check the issues and solve them.

mas_bass
04-21-2010, 04:07 PM
thanks for the tip, had a look around for casting and tried typecasting the vars with numerical values like this:


varReceiver = new LoadVars();
varReceiver.load("data.txt");

var data_a:Number = data_aa;
var data_b:Number = data_bb;
var data_c:Number = data_cc;
var data_d:Number = data_dd;
var data_e:Number = data_ee;
var data_f:Number = data_ff;
var data_g:Number = data_gg;
var data_h:Number = data_hh;
var data_i:Number = data_ii;


and changed the text inside data.txt to:

&data_aa=19&data_bb=20&data_cc=27&data_dd=24&data_ee=10&data_ff=5&data_gg=24&data_hh=20&data_ii=19&day_b=Sunday&day_c=Monday&day_d=Tuesday&day_e=Wednesday&day_f=Thursday&day_g=Friday&day_h=Saturday&

(I guess the other vars containing day names can stay as they are, they're not processed mathematically)

and it still doesn't work. Am I doing it correctly like that or is there an other way?

vinayak.kadam
04-22-2010, 04:56 AM
Can u attach ur FLA here for us to check and implement ?

mas_bass
04-22-2010, 06:51 AM
sure, here it is (saved as CS3 format) + data.txt

the first two functions you'll see are an attempt I'm making at making the movie fade in and after some set interval of x seconds have it fade out (it's not complete yet). The calling of variables is just below those two.

vinayak.kadam
04-22-2010, 09:59 AM
I did some major changes to your code. The problem was that arrays were not being poulated with values after teh text file was loaded. I just changes teh way text file was loaded and manually wrote statements to populate array with values. Please check the FLA. I was ot sure exactly how the output should be so cannot say if I did was right or wrong.

Request you to please run the attached FLA and check!

mas_bass
04-22-2010, 12:51 PM
Thanks for your time and effort! What this script does is it takes the data of the last 9 days, plots 7 points, joins them with a line and writes the data value + day name above or below the plotted point. The extra two days (one before and one after the 7 we plot) are there so there's a line coming in the graph before and after the first and last plotted points. On the left, the two numbers are the maximum and minimum this scale represents and change according to the data input. It should look like the GIF I attached.

vinayak.kadam
04-22-2010, 01:14 PM
Thanks for that screenshot. Things are as per your request now. Check the attachment please. FLA is saved in Flash CS3 version.

mas_bass
04-22-2010, 01:26 PM
great! thanks a lot!!!