By Zevan | February 17, 2009
Actionscript:
-
var dial:Sprite = Sprite(addChild(new Sprite()));
-
with(dial.graphics) lineStyle(1, 0x000000), beginFill(0xCCCCCC), drawCircle(0,0,100), lineTo(0,0);
-
dial.x = stage.stageWidth / 2;
-
dial.y = stage.stageHeight / 2;
This snippet shows how you can combine drawCircle() and lineTo() to quickly draw a clock/dial primitive shape. I'll use this in tomorrows snippet...
By Zevan | February 14, 2009
Actionscript:
-
[SWF(width=600,height=400,backgroundColor=0x000000,frameRate=30)]
-
var mcs:Vector.<MovieClip> = new Vector.<MovieClip>();
-
var num:int = 2000;
-
for (var i:int = 0; i<num; i++){
-
var s:MovieClip = new MovieClip();
-
s.graphics.lineStyle(0,0xFFFFFF);
-
s.graphics.drawCircle(0,0, Math.random()*30 + 3);
-
s.x = Math.random()*stage.stageWidth;
-
s.y = Math.random()*stage.stageHeight;
-
// comment out to kill your cpu
-
s.cacheAsBitmap = true;
-
addChild(s);
-
s.vx = Math.random() * 2 - 1;
-
s.vy = Math.random() * 2 - 1;
-
mcs.push(s);
-
}
-
addEventListener(Event.ENTER_FRAME, onCenter);
-
function onCenter(evt:Event):void{
-
for (var i:int = 0; i<num; i++){
-
mcs[i].x += mcs[i].vx;
-
mcs[i].y += mcs[i].vy;
-
}
-
}
This was created in response to a question about the real speed gain of cacheAsBitmap. Just comment out the cacheAsBitmap line to see the difference.
Despite it's extreme simplicity, this is actually a rather fun file to play with, change some of the graphics class method calls around and see what happens.
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.