By Zevan | February 13, 2009
Actionscript:
-
var path:String = "section/home.swf";
-
-
var deepLinkValue:String = path.split("/")[1].split(".swf")[0];
-
-
trace(deepLinkValue);
-
/*outputs:
-
home
-
*/
As a primarily self taught programmer there was a time when I didn't know what a regular expression was. There was also a time when ActionScript didn't have them (you had to use a library). Sometimes when I'm in a rush I still do weird things like the above instead of regular expressions... pretty nasty I know.
Here are two different regular expressions that do the same thing:
Actionscript:
-
var path = "section/home.swf"
-
-
trace(path.match(/\w+(?=\.)/))
-
/*outputs:
-
home
-
*/
-
-
trace(path.replace(/.+\/|\..+/g, ""))
-
/* also outputs:
-
home
-
*/
Actually, I'd be curious to see other regular expressions that only return one value (not an array) that achieve this same thing... feel free to post of a comment if you can think of one...
By Zevan | February 12, 2009
Actionscript:
-
var someString:String = "_file";
-
trace(someString.substr(1));
-
/*
-
ouptuts:
-
file
-
*/
Turns out the second argument of substr() is optional..... not sure how I never noticed that before.
By Zevan | February 11, 2009
Actionscript:
-
private var _correctPath:String;
-
-
//.... somewhere a little later
-
-
if (root.loaderInfo.url.split("http://").length == 1){
-
_correctPath = "http://www.mywebsite.com/this/is/an/absolute/path/";
-
}else{
-
_correctPath = "";
-
}
I like to do something like this when I'm working locally... it automatically tells my swf to use an absolute path for php files, xml files etc.... then, when I upload it, the LoaderInfo.url property contains an "http://" so it uses a relative path for all the external files. This is common especially if you don't have php and mysql installed on your machine and need to test these using your server.
Posted in misc | Tagged actionscript, flash |
By Zevan | February 10, 2009
Actionscript:
-
// see previous post for details, this code won't run on it's own
-
with(addChild(window)){
-
x = 10, y = 10;
-
with(addChild(c0)) x = 10, y = 10;
-
with(addChild(c1)) x = 20, y = 10;
-
with(addChild(box)){
-
x = 50, y = 10;
-
with(addChild(txt)) x = 5, y = 5;
-
}
-
}
It's entertaining to try and think of different DisplayObject nesting techniques. I have a few more of these rolling around in my head that I'll try out soon.... The only good/notable thing about the above is that it shows that nested with statements actually work like you'd expect them to....
Posted in misc | Tagged actionscript, flash |