By Zevan | February 6, 2009
Actionscript:
-
// for simplicity I left this XML inline, this will work exactly the same if it were external
-
var program:XML=<body>
-
<draw>
-
<![CDATA[
-
beginFill(0xFF0000);
-
drawCircle(100,100,50);
-
endFill();
-
lineStyle(0, 0x666666);
-
moveTo(100, 100);
-
lineTo(200, 200);
-
moveTo(300, 200);
-
curveTo(350, 300, 400, 200);
-
lineStyle(0, 0x0000FF);
-
drawRect(200, 50,100,100) ;
-
]]>
-
</draw>
-
</body>;
-
-
// parse and run the Graphics class commands from the XML
-
render(parseFunctions(program.draw.toString()));
-
-
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++) {
-
graphics[p[i][0]].apply(graphics,p[i].splice(1));
-
}
-
}
The above code builds on yesterdays post by showing how one could potentially store graphics class method calls in XML using a few regular expressions and Function.apply().
The parseFunctions() function reads through the CDATA string and formats it in a 2D array that looks like this:
Actionscript:
-
[[beginFill, 0xFF0000], [drawCircle, 100, 100, 50], etc...]
The render() function reads through this 2D array, using the first value of each nested array as the function and the remaining values as arguments...
As is this won't really work with most of the new fp10 graphics methods...
By Zevan | January 8, 2009
Actionscript:
-
// tracing out large numbers will go into exponent notation
-
trace(1000000000000000000000000);
-
// outputs: 1e+24
-
-
var a:String = "1000000000000000000000009";
-
var b:String = "12000000000000000000000001";
-
-
// bigAdd function adds two large numbers
-
trace(bigAdd(a, b));
-
// outputs: 13000000000000000000000010
-
-
// unclear what regular addition is doing here:
-
trace(int(a) + int(b));
-
// outputs: -3758096384
-
-
function bigAdd(a:String, b:String):String{
-
var answer:String="";
-
var carry:int = 0;
-
var zeros:int = 0;
-
var i:int = 0;
-
var max:int = 0;
-
-
// obtain max variable and prepend zeros to the shortest string
-
if (a.length> b.length){
-
max = a.length;
-
zeros = max - b.length;
-
for (i= 0; i<zeros; i++) {
-
b = "0" + b;
-
}
-
}else if (b.length> a.length){
-
max = b.length;
-
zeros = max - a.length;
-
for (i= 0; i<zeros; i++) {
-
a = "0" + a ;
-
}
-
}else{
-
// a and b have same number of digets
-
max = a.length;
-
}
-
-
// add each character starting with the last (max - 1),
-
// carry the 1 if the sum has a length of 2
-
for (i = max - 1; i> -1; i--){
-
var sum:String = String(int(a.charAt(i)) + int(b.charAt(i)) + carry);
-
-
if (sum.length == 2){
-
answer = sum.charAt(1) + answer;
-
carry = 1;
-
}else{
-
carry = 0;
-
answer = sum + answer;
-
}
-
}
-
if (carry == 1){
-
answer = 1 + answer;
-
}
-
return answer;
-
}
This snippet demos a function called bigAdd(), which can be used to add very large numbers that the standard numerical datatypes (int, Number, etc) won't really work with. It uses a string based addition algorithm.
When I decided to write this snippet I dug around for awhile trying to find a nice elegant algorithm to port, something done without prepending zeros etc.... but I couldn't find one that suited my needs... so I just created this one from scratch. There's room for optimization and improvement but it works well enough for just playing around.
BigDecimal & BigInteger
A few months back I needed to do some programming with very large numbers. I dug around for awhile and eventually found out that java has a built in BigInteger andBigDecimal class.... I used java to write the program I had in mind, but thought it would be fun to eventually create something similar for ActionScript....
By Zevan | December 4, 2008
Actionscript:
-
var phoneField:TextField = new TextField();
-
with (phoneField) {
-
type=TextFieldType.INPUT;
-
maxChars=12;
-
restrict="0-9";
-
border=true;
-
width=100;
-
height=20;
-
x=y=20;
-
}
-
addChild(phoneField);
-
-
phoneField.addEventListener(TextEvent.TEXT_INPUT, onInput);
-
-
function onInput(evt:TextEvent):void {
-
if (phoneField.length==3 || phoneField.length==7) {
-
phoneField.appendText("-");
-
var leng:int=phoneField.text.length;
-
phoneField.setSelection(leng, leng);
-
}
-
}
Quick way to make sure text input is formatted like a phone number.
By Zevan | November 28, 2008
Actionscript:
-
var words:String = "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
-
-
// outputs: "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
-
trace(words);
-
-
// the "/g" tells it to replace all instances of ActionSnippet.com
-
words = words.replace(/ActionSnippet.com/g, "SomeWebsite.com");
-
-
// outputs: SomeWebsite.com is a website. SomeWebsite.com is a blog.
-
trace(words);
Just a quick response to a student question.