Actionscript:
-
function decode(str:String):Object {
-
var vars:Object={};
-
var parse:Array =str.split("&");
-
var i:int=0;
-
for (i = 0; i<parse.length; i++) {
-
var pair:Array=parse[i].split("=");
-
if (pair.length==2) {
-
vars[pair[0]]=pair[1];
-
}
-
}
-
return vars;
-
}
-
-
var nameValuePairs="one=1&&two=éllo&three=1000&four=0xFF0000";
-
var parsed:Object=decode(nameValuePairs);
-
-
trace(parsed.one);
-
trace(parsed.two);
-
trace(parsed.three);
-
trace(parsed.four);
-
-
/*outputs:
-
1
-
éllo
-
1000
-
0xFF0000
-
*/
Well not exactly a URLVariables replacement, but I often use the URLVariables.decode() function and find that its very picky... like if there is an extra & or something it freaks out and breaks, or if there are line returns. As a quick and simple solution the other day I wrote this function into part of a class (Model class) that I was working on. Fixed the problem and even handles Spanish characters nicely.
There is plenty of room for improvement with this simple function... feel free to post improvements in the comments.
By Zevan | December 11, 2009
Actionscript:
-
[SWF(width = 700, height=700, frameRate=12)]
-
var cmds:Array = [];
-
-
var loader:URLLoader = new URLLoader();
-
var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
-
var vars:URLVariables = new URLVariables();
-
vars.q = "#asgraph";
-
// results per page
-
vars.rpp = "100";
-
vars.page = 1;
-
-
req.data = vars;
-
req.method = URLRequestMethod.GET;
-
-
loader.addEventListener(Event.COMPLETE, onLoaded);
-
loader.load(req);
-
-
var txt:TextField = TextField(addChild(new TextField()));
-
txt.defaultTextFormat = new TextFormat("_sans", 12);
-
with (txt){ x=10, y=10, autoSize="left"; }
-
txt.htmlText = "loading...";
-
-
function onLoaded(evt:Event):void{
-
removeChild(txt);
-
var searchData:XML = new XML(loader.data);
-
var atom:Namespace = searchData.namespace("");
-
-
var leng:int = searchData.atom::entry.length() -1;
-
for (var i:int = leng; i>=0; i--){
-
var cmd:String =
-
searchData.atom::entry[i].atom::title.toString().replace(/\#asgraph/g,
-
"");
-
// added this to ignore words starting with @
-
cmd = cmd.replace(/@(\w+)/g, "");
-
cmds.push(cmd);
-
}
-
-
var time:Timer = new Timer(100, cmds.length);
-
time.addEventListener(TimerEvent.TIMER, onTick);
-
time.start();
-
}
-
function onTick(evt:TimerEvent):void{
-
render(parseFunctions(cmds[evt.target.currentCount - 1]));
-
graphics.endFill();
-
graphics.lineStyle();
-
}
-
-
// parse and run Graphics class commands
-
function parseFunctions(dat:String):Array{
-
var a:Array = dat.split(";") ;
-
for (var i:int = 0; i<a.length-1; i++){
-
a[i] = a[i].split(/\(\)|\(|\)/g);
-
var f:String = a[i][0] = a[i][0].replace(/\s/g,"");
-
a[i] = a[i].splice(0, a[i].length - 1);
-
if (a[i].length> 1){
-
a[i] = a[i][1].split(",");
-
a[i].unshift(f);
-
}
-
}
-
return a.splice(0,a.length - 1);
-
}
-
function render(p:Array):void {
-
for (var i:int = 0; i<p.length; i++) {
-
try{
-
graphics[p[i][0]].apply(graphics,p[i].splice(1));
-
}catch(e:Error){};
-
}
-
}
This is a simple idea I had awhile back... This snippet searches twitter for the #asgraph hashtag and if it finds standard Graphics class method calls it renders them.
So if you tweet something likethis :
#asgraph beginFill(0xFF); drawCircle(200,200,10);
it will get rendered into the below swf (you'll need to refresh to see your tweet get rendered):
This movie requires Flash Player 9
Here is a direct link to the swf
Also posted in Graphics | Tagged actionscript, as3, flash |
Actionscript:
-
package com.hapticdata.utils
-
{
-
import flash.external.ExternalInterface;
-
/**
-
* Simplifies posting to Google's Analytics Tracker, allows easily disabling for development phase
-
* and uses ExternalInterface rather than navigateToURL
-
* @class Urchin
-
* @author Kyle Phillips - <a href="http://www.haptic-data.com">http://www.haptic-data.com</a>
-
* @created October 28, 2008
-
* @example Analytics.post("section");
-
*/
-
-
-
public class Analytics
-
{
-
-
public static var enabled:Boolean = true;
-
//appended as a directory to all trackings
-
public static var swfID:String="/flashevent/";
-
//correct for default snippet. Change this if you have a custom-wrapped analytics method
-
public static var functionToCall:String = "pageTracker._trackPageview";
-
-
/**
-
* Will invoke the set javascript function
-
* @param location:String - a string identifying where the user is in the site
-
*/
-
public static function post(location:String):void
-
{
-
if(enabled && ExternalInterface.available)
-
{
-
ExternalInterface.call(functionToCall,swfID+location);
-
}
-
}
-
}
-
}
This contest entry by Kyle Phillips is a utility class for dealing with google analytics. If you haven't messed with google analytics I suggest you give it a try.
Kyle Phillips links:
>> http://workofkylephillips.com
>> http://labs.hapticdata.com
I usually find myself adding google analytics code in a very hacky way... usually because the client asks for it at the 11th hour. Using a class like Kyle's would enable you to simply add the tracking code from the start and then if the client doesn't ask for it... you can just keep the class disabled... or charge more when you offer google analytics tracking for "phase 2" of the project
By Zevan | October 8, 2009
Actionscript:
-
if(parent == stage){
-
// no container
-
}else{
-
// has container
-
}
Found myself writing this snippet today.... it figures out if the current swf is inside a container (Loader) or not... lots of other ways to do this... feel free to post your version in the comments...
Also posted in Loader, misc | Tagged actionscript, as3, flash |