{"id":179,"date":"2009-02-06T00:00:16","date_gmt":"2009-02-06T07:00:16","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=700"},"modified":"2009-02-06T00:00:16","modified_gmt":"2009-02-06T07:00:16","slug":"graphics-class-methods-in-xml","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=179","title":{"rendered":"Graphics Class Methods in XML"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ for simplicity I left this XML inline, this will work exactly the same if it were external\r\nvar program:XML=&lt;body&gt;\r\n\t\t\t\t&lt;draw&gt;\r\n\t\t\t\t  &lt;!&#x5B;CDATA&#x5B;\r\n\t\t\t\t   beginFill(0xFF0000);\r\n\t\t\t\t   drawCircle(100,100,50);\r\n\t\t\t\t   endFill();\r\n\t\t\t\t   lineStyle(0, 0x666666);\r\n\t\t\t\t   moveTo(100, 100);\r\n\t\t\t\t   lineTo(200, 200);\r\n\t\t\t\t   moveTo(300, 200);\r\n\t\t\t\t   curveTo(350, 300, 400, 200);\r\n\t\t\t\t   lineStyle(0, 0x0000FF);\r\n\t\t\t\t   drawRect(200, 50,100,100) ;\r\n\t\t\t\t\t]]&gt;\r\n\t\t\t\t&lt;\/draw&gt;\r\n&lt;\/body&gt;;\r\n\r\n\/\/ parse and run the Graphics class commands from the XML\r\nrender(parseFunctions(program.draw.toString()));\r\n\r\nfunction parseFunctions(dat:String):Array{\r\n\tvar a:Array = dat.split(&quot;;&quot;) ;\r\n\tfor (var i:int = 0; i&lt;a.length-1; i++){\r\n\t\ta&#x5B;i] = a&#x5B;i].split(\/()|(|)\/g);\r\n\t\tvar f:String = a&#x5B;i]&#x5B;0] = a&#x5B;i]&#x5B;0].replace(\/s\/g,&quot;&quot;);\r\n\t\ta&#x5B;i] = a&#x5B;i].splice(0, a&#x5B;i].length - 1);\r\n\t\tif (a&#x5B;i].length&gt; 1){\r\n\t\t a&#x5B;i] = a&#x5B;i]&#x5B;1].split(&quot;,&quot;);\r\n\t\t a&#x5B;i].unshift(f);\r\n\t\t}\r\n\t}\r\n\treturn a.splice(0,a.length - 1);\r\n}\r\nfunction render(p:Array):void {\r\n\tfor (var i:int = 0; i&lt;p.length; i++) {\r\n\t\tgraphics&#x5B;p&#x5B;i]&#x5B;0]].apply(graphics,p&#x5B;i].splice(1));\r\n\t}\r\n}\r\n<\/pre>\n<p>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().<\/p>\n<p>The parseFunctions() function reads through the CDATA string and formats it in a 2D array that looks like this:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;&#x5B;beginFill, 0xFF0000], &#x5B;drawCircle, 100, 100, 50], etc...]\r\n<\/pre>\n<p>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&#8230; <\/p>\n<p>As is this won&#8217;t really work with most of the new fp10 graphics methods&#8230;  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ for simplicity I left this XML inline, this will work exactly the same if it were external var program:XML=&lt;body&gt; &lt;draw&gt; &lt;!&#x5B;CDATA&#x5B; 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) ; ]]&gt; &lt;\/draw&gt; &lt;\/body&gt;; \/\/ parse and run the Graphics class commands from the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,88,17,33,20,21,34],"tags":[3,4],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-external-data","category-functions","category-graphics","category-string-manipulation","category-strings","category-xml","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}