{"id":178,"date":"2009-02-05T00:00:12","date_gmt":"2009-02-05T07:00:12","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=696"},"modified":"2009-02-05T00:00:12","modified_gmt":"2009-02-05T07:00:12","slug":"dynamic-graphics-and-functionapply","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=178","title":{"rendered":"Dynamic Graphics and Function.apply()"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar cmds:Array = &#x5B;&#x5B;&quot;lineStyle&quot;, 0, 0xFF0000], &#x5B;&quot;drawCircle&quot;,100, 100, 50], &#x5B;&quot;drawRect&quot;, 50, 50, 100, 100]];\r\ncmds.push(&#x5B;&quot;drawCircle&quot;, 100, 100, 70]);\r\ncmds.push(&#x5B;&quot;beginFill&quot;,  0x555555]);\r\ncmds.push(&#x5B;&quot;drawRoundRect&quot;, 80, 80, 40, 40, 10, 10]);\r\ncmds.push(&#x5B;&quot;endFill&quot;]);\r\n\r\nrender(cmds);\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 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&#8217;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.<\/p>\n<p>I&#8217;ve been thinking about the best way to do this for awhile&#8230; I started off doing something very convoluted and then realized that I could use Function.apply()&#8230;. <\/p>\n<p>Tomorrow I&#8217;ll post a snippet showing how to use this function in conjunction with XML.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var cmds:Array = &#x5B;&#x5B;&quot;lineStyle&quot;, 0, 0xFF0000], &#x5B;&quot;drawCircle&quot;,100, 100, 50], &#x5B;&quot;drawRect&quot;, 50, 50, 100, 100]]; cmds.push(&#x5B;&quot;drawCircle&quot;, 100, 100, 70]); cmds.push(&#x5B;&quot;beginFill&quot;, 0x555555]); cmds.push(&#x5B;&quot;drawRoundRect&quot;, 80, 80, 40, 40, 10, 10]); cmds.push(&#x5B;&quot;endFill&quot;]); render(cmds); function render(p:Array):void { for (var i:int = 0; i&lt;p.length; i++) { graphics&#x5B;p&#x5B;i]&#x5B;0]].apply(graphics,p&#x5B;i].splice(1)); } } The above creates a function called render() that takes a 2D array [&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,17,33],"tags":[3,4],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-functions","category-graphics","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/178","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=178"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}