By Zevan | April 23, 2009
Actionscript:
-
var str:String='{x: 10, y:10, width:100, heigth:100, name:"myClip", type:"clip"}';
-
-
var obj:Object=toObject(str);
-
-
trace(obj.x + obj.width, obj.name, obj.type);
-
-
function toObject(str:String):Object {
-
str=str.replace(/\{|\}/g,"");
-
// to an array of name value pairs [0] = name, [1] = value etc...
-
var pairs:Array=str.split(/\:|\,\s+/);
-
var obj:Object = new Object();
-
for (var i:int = 0; i<pairs.length; i+=2) {
-
obj[pairs[i]]=numOrString(pairs[i+1]);
-
}
-
return obj;
-
}
-
function numOrString(val:*):* {
-
if (isNaN(Number(val))) {
-
if (val.charAt(0) == '"' && val.charAt(val.length - 1) == '"'){
-
val = val.substr(1, val.length - 2);
-
}
-
} else {
-
val=Number(val);
-
}
-
return val;
-
}
A very limited way to parse an object from a string... Tomorrow I'll post a better version of this that's a good deal more complex and doesn't really use regular expressions... This works nicely if you don't have arrays as Object properties...
Also posted in dynamic, strings | Tagged actionscript, flash |
Actionscript:
-
trace(formatNum(1000));
-
trace(formatNum(10000000));
-
trace(formatNum(1000000.39485));
-
-
function formatNum(num:Number):String {
-
var newStr:String = "";
-
var str:String = num.toString();
-
-
var parts:Array = str.split(".");
-
str = parts[0];
-
var end:String = (parts[1]) ? "." + parts[1] : "";
-
-
var i:int = str.length;
-
while(i--> 0){
-
var char:String = str.charAt(i);
-
if ((str.length - i) % 3 == 0){
-
newStr = "," + char +newStr;
-
}else{
-
newStr = char + newStr;
-
}
-
}
-
return newStr + end;
-
}
-
/*
-
outputs:
-
1,000
-
10,000,000
-
1,000,000.39485
-
*/
The function formatNum() returns a string given a number argument ... it adds commas where appropriate and takes into account decimal values. This is useful anywhere you need to display numbers over 1,000, in a game, in a calculator app etc...
By Zevan | February 26, 2009
Actionscript:
-
var words:String = "one two three four five";
-
-
trace("two: ", words.indexOf("two"));
-
-
var letters:String = "abcd";
-
-
trace("d: ",letters.indexOf("d"));
-
-
trace("z: ",letters.indexOf("z"));
-
-
/*
-
outputs:
-
two: 4
-
d: 3
-
z: -1
-
*/
indexOf() searches a string for another string and returns an index... in line 3 above, I search the words string for the smaller string "two" and indexOf() gives me the index of the letter "t". If indexOf() doesn't find anything it will return -1 (as in the case of line 9).
I seem to recall using this in some unexpected places. I'll see if I can dig up an example over the next few days.
Also posted in strings | Tagged actionscript, flash |
By Zevan | February 25, 2009
Actionscript:
-
var txt:TextField = TextField(addChild(new TextField()));
-
txt.text = "";
-
txt.width = 190;
-
txt.height = 400;
-
txt.multiline = true;
-
-
var count:int = 1;
-
function render():void{
-
var line = int(count).toString(2);
-
while(line.length <31){
-
line = "0" + line;
-
}
-
txt.appendText(line + "\n");
-
txt.scrollV= txt.maxScrollV;
-
}
-
-
addEventListener(Event.ENTER_FRAME, onCountUp);
-
function onCountUp(evt:Event):void {
-
count *= 2;
-
render();
-
if (count ==0x40000000){
-
removeEventListener(Event.ENTER_FRAME, onCountUp);
-
addEventListener(Event.ENTER_FRAME, onCountDown);
-
}
-
}
-
function onCountDown(evt:Event):void {
-
count /= 2;
-
render();
-
if (count ==1){
-
addEventListener(Event.ENTER_FRAME, onCountUp);
-
removeEventListener(Event.ENTER_FRAME, onCountDown);
-
}
-
}
The above animates a zig zag pattern in a TextField.
Also posted in Math, strings |