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 | February 5, 2009
Actionscript:
-
var cmds:Array = [["lineStyle", 0, 0xFF0000], ["drawCircle",100, 100, 50], ["drawRect", 50, 50, 100, 100]];
-
cmds.push(["drawCircle", 100, 100, 70]);
-
cmds.push(["beginFill", 0x555555]);
-
cmds.push(["drawRoundRect", 80, 80, 40, 40, 10, 10]);
-
cmds.push(["endFill"]);
-
-
render(cmds);
-
-
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 creates a function called render() that takes a 2D array of Graphics class methods and then runs them. This is a very interesting technique, specifically if you'd like to write Graphics class method calls in an XML or txt file and then have them run on a given DisplayObject in flash.
I've been thinking about the best way to do this for awhile... I started off doing something very convoluted and then realized that I could use Function.apply()....
Tomorrow I'll post a snippet showing how to use this function in conjunction with XML.
By Zevan | January 17, 2009
Actionscript:
-
[SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)]
-
-
for (var i:int = 0; i<10; i++){
-
// draggable ellipse
-
var dot:Sprite = drag(createSprite("Ellipse", -10, -10, 20, 20));
-
dot.x = Math.random() * stage.stageWidth ;
-
dot.y = Math.random() * stage.stageHeight ;
-
}
-
-
for (i = 0; i<10; i++){
-
-
var box:Sprite = drag(spin(createSprite("Rect", -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));
-
box.x = Math.random() * stage.stageWidth ;
-
box.y = Math.random() * stage.stageHeight ;
-
}
-
-
-
// createSprite can create ellipses or rectangles
-
function createSprite(shape:String, xp:Number, yp:Number, w:Number, h:Number, col:uint=0x444444):Sprite {
-
var s:Sprite = new Sprite();
-
s.graphics.beginFill(col);
-
// trick from a previous post
-
s.graphics["draw" + shape](xp, yp, w, h);
-
addChild(s);
-
return s;
-
}
-
-
// drag and spin add listeners to an untyped target and return that target for easy function nesting
-
function drag(target:*):*{
-
target.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent){ evt.currentTarget.startDrag(); });
-
return target;
-
}
-
-
function spin(target:*, speed:Number):*{
-
target.addEventListener(Event.ENTER_FRAME, function(evt:Event){ evt.currentTarget.rotation+=speed; });
-
return target;
-
}
-
-
stage.addEventListener(MouseEvent.MOUSE_UP, function(){ stopDrag() });
The above will create some draggable circles and some rotating draggable rects... but that's not really the point....
When prototyping and just playing around I write functions that take an Object as an argument, alter that Object in some way and pass that some Object out as a return value.... this makes it so I can write things like this:
Actionscript:
-
drag(spin(createSprite("Rect", -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));
Readability can be a problem so... consider that before using this for anything...
Also posted in dynamic, misc | Tagged actionscript, flash |