Code Snippet: Extract A Value From A Querystring in AS3
I found myself in need of extracting a given value from a query string that was inside an XML attribute. Searching for something like this, I couldn't find anything on par with PHP's parse_str() so I got the string out of the XML and wrote this handy function:
function getQueryStringVar(queryStr:String, targetVar:String):String
{
var pairs = queryStr.split("&");
for(var i:String in pairs)
{
var splitStr = pairs[i].split("=");
if(splitStr[0] == targetVar)return splitStr[1];
}
return "";
}
Usage is easy. Just send it your query string as the first parameter (make sure there's not a "?" at the beginning) and key you want to know the value of.
// myVal is set to "example"
var myVal:String = getQueryStringVar("val1=test&val2=example", "val2");
Let me know if you find this function useful or if you improve upon it.
Actionscript 3.0 List Toggler
So today, I found myself in need of a really long list inside a List() component in Flash. Thing is, that list needed to be able to toggle the elements on and off so that the toggled-on items would be used in another set of logic.
As far as I know, there's no way to embed a checkbox component inside a list component using a dataprovider. So I devised a solution.
Basically, I have two movies in the library (exported for actionscript) and create a DataProvider(), linking it to a List() component. I set the List's "iconField" to the movie clip that looks like an unchecked checkmark and then toggle to the checked movieclip when that list item was selected.
See below how I did it.
import fl.controls.List;
import fl.events.ListEvent;
import fl.data.DataProvider;
var dp:DataProvider = new DataProvider();
//Put a bunch of things in the list
for(var i = 0; i < 20; i++)
{
dp.addItem( { iconSource:UncheckedBox, label:"Item "+ i } );
}
var list:List = new List();
list.iconField = "iconSource";
list.dataProvider = dp;
list.width = 150;
list.height = 200;
list.x = 10;
list.y = 10;
addChild(list);
list.addEventListener(ListEvent.ITEM_CLICK, toggleState);
function toggleState(e:ListEvent):void
{
if(dp.getItemAt(e.index).iconSource == CheckedBox)
{
dp.getItemAt(e.index).iconSource = UncheckedBox;
}
else
{
dp.getItemAt(e.index).iconSource = CheckedBox;
}
//Must invalidate the list stage element so that it redraws with the new icon.
list.invalidate();
}
In this example, you can select items on the left and the list on the right updates with only the content that is selected:
Here's the download to see how it's all put together. (602kb)
If I'm going about this all wrong or if you really can embed a checkbox into a list component, please leave me a note in the comments below.
Flash’s Built-in Rounding Function is Severely Limited
Now that I'm working more and more in Flash ActionScript 3 I'll be posting some of my findings about it here.
My first AS3 related post is about the Math.round() function. This function will only round to a whole number (int). What if I have 3.74257444 and need a result of 3.74 instead of what Math.round() would give me: 4?
Well, here's the solution:
function roundDecimal(num:Number, places:Number):Number
{
return int( (num) * Math.pow(10, places) ) / Math.pow(10, places);
}
This function will return the specified amount of decimal places rounded correctly.
Example usage:
var myNumber:Number = 3.74257444;
trace(roundDecimal(myNumber, 2)); //Returns 3.74
trace(roundDecimal(myNumber, 3)); //Returns 3.743
trace(Math.round(myNumber)); //Returns 4
I hope this helps anyone looking for a similar solution.
[via]


