Hi,
I'm posting and getting back data from an ASP page on a site.
The data returns using the following function:
var dataS3:String;
function handleComplete3(event:Event):void
{
var loader3:URLLoader = URLLoader(event.target);
dataS3 = loader3.data;
trace(dataS3);
}
The trace (showing the data received) looks like: score1=0.5244005132792953&score2=-0.27105341041929653
How do I get these 2 scores (score1 and score2), which appear to be part of a data string, into separate variables that I can then use as numeric values in later frames (round/add/subtract/multiply/divide them)? Is there a way to parse out the 2 variables for use in later frame maths? Would I declare var DataS3 differently or keep it as a string, then somehow convert the returning string to separate numbers or integers?
Any help appreciated.
Hi Ned,
Been doing online reading as suggested.
Have found the way to separate the returned string into 2 variables.
Can convert to numbers (I think-no error message on debugging) and round to 3 decimal places:
var dataS3:String;
var dataS4:String;
var dataS5:Number;
var dataS6:Number;
var dataS7:Number;
var dataS8:Number;
function handleComplete3(event:Event):void
{
var loader3:URLLoader = URLLoader(event.target);
dataS3 = loader3.data.score1;
dataS4 = loader3.data.score2;
dataS5 = Number(dataS3);
dataS6 = Number(dataS4);
dataS7 = Math.round(dataS5*1000)/1000;
dataS8 = Math.round(dataS6*1000)/1000;
trace(dataS5);
trace(dataS6);
trace(dataS7);
trace(dataS8);
}
}
Thank you.
Typically you use objects you can loop on for this. You can also type cast as you're doing only when you need to rather than to create a variable just to use it.
Your example re-written using an array and casting can be:
function handleComplete3(event:Event):void
{
// make a bunch of var=value pairs
var urlVars:Array = String(event.target.data).split("&");
// S5
trace(String(urlVars[0]).split("=")[1]); // prints string
// S6
trace(String(urlVars[1]).split("=")[1]); // prints string
// now you want conversion, just convert in-line to numbers instead of assigning new vars
// S7
trace(Math.round(Number(String(urlVars[0]).split("=")[1]) * 1000) / 1000); // prints number
// S8
trace(Math.round(Number(String(urlVars[1]).split("=")[1]) * 1000) / 1000); // prints number
}
The basic point is you can use the data you receive directly and unless it's in a very intensively called function you can just perform multiple operation at the same time without saving out to extra variables.
You're allowed to daisy chain and cast types as you go. For example I didn't NEED to split the string by an &ersand into a temporary array, I did it for ease of reading. To print your S7 I could have did this single line of code:
function handleComplete3(event:Event):void
{
// traces your S7
trace(Number(Math.round(Number(String(String(event.target.data).split ("&")[0]).split("=")[1]) * 1000) / 1000) );
}
There's no difference between the S7's being printed. You assigned a bunch of variables and cast them one by one. I anonymously casted them daisy-chain style without assigning any variables working directly on the data. I just wanted to point out the daisy chaining power of ActionScript. You can convert things as you go and condense code and dynamic variable usage quite a bit.
Hi sinious,
Thank you for these better ways of doing the scripting, and with more efficiency.
As I "clean up" my project, I can use these ideas to improve it.
Next I am searching for a final piece of the project: how to display the score results using a dynamic bar chart (bars go up or down based on score value). Not easy for me. Score results can be positive or negative nums, so I need to start at mid-screen, positive bar up depending on positive score value, negative bar down. Haven't found any AS3 like this to help get started. Biggest challenge is to have bar move slowly up or down (maybe over 2-3 seconds). So move Y plus some way to time this. Challenging for someone like me with modest scripting skill. I'll keep at it. ![]()
Again, much thanks for your help.
In those tutorials the XML is providing the data for the bar charts, just the numeric value. You'd just remove all that XML code and supply your own numeric values. Otherwise the bar chart code would be the same.
e.g.
someBar.scaleY = int(_xml.Somenode[i].someValue * someMathValue / etc);
to
someBar.scaleY = myValue;
You're still adjusting the same property of the bar on the chart, the size of it (whether it be height, scaleY, etc), you just erase the XML code and supply your own value instead. The tutorial should specify what the value expects which will either be a height in pixels or a scale value between -1 and 1. It's up to you to perform the math to convert the number correctly. Feel free to post any questions on that process.
North America
Europe, Middle East and Africa
Asia Pacific