{"id":159,"date":"2009-01-17T00:00:29","date_gmt":"2009-01-17T07:00:29","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=543"},"modified":"2009-01-17T00:00:29","modified_gmt":"2009-01-17T07:00:29","slug":"nestingfunctioncalls","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=159","title":{"rendered":"nesting(function(calls()))"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)]\r\n\r\nfor (var i:int = 0; i&lt;10; i++){\r\n       \/\/ draggable ellipse\r\n\tvar dot:Sprite = drag(createSprite(&quot;Ellipse&quot;,  -10, -10, 20, 20));\r\n\tdot.x = Math.random() * stage.stageWidth ;\r\n\tdot.y = Math.random() * stage.stageHeight ;\r\n}\r\n\r\nfor (i = 0; i&lt;10; i++){\r\n  \r\n      var box:Sprite = drag(spin(createSprite(&quot;Rect&quot;, -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));\r\n\tbox.x = Math.random() * stage.stageWidth ;\r\n\tbox.y = Math.random() * stage.stageHeight ;\r\n}\r\n\r\n\r\n \/\/ createSprite can create ellipses or rectangles\r\nfunction createSprite(shape:String, xp:Number, yp:Number, w:Number, h:Number, col:uint=0x444444):Sprite {\r\n\tvar s:Sprite = new Sprite();\r\n\ts.graphics.beginFill(col);\r\n\t\/\/ trick from a previous post\r\n\ts.graphics&#x5B;&quot;draw&quot; + shape](xp, yp, w, h);\r\n\taddChild(s);\r\n\treturn s;\r\n}\r\n\r\n\/\/ drag and spin add listeners to an untyped target and return that target for easy function nesting\r\nfunction drag(target:*):*{\r\n\ttarget.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent){ evt.currentTarget.startDrag(); });\r\n\treturn target;\r\n}\r\n\r\nfunction spin(target:*, speed:Number):*{\r\n\ttarget.addEventListener(Event.ENTER_FRAME, function(evt:Event){ evt.currentTarget.rotation+=speed; });\r\n\treturn target;\r\n}\r\n\r\nstage.addEventListener(MouseEvent.MOUSE_UP, function(){ stopDrag() });\r\n<\/pre>\n<p>The above will create some draggable circles and some rotating draggable rects&#8230; but that&#8217;s not really the point&#8230;.<\/p>\n<p>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&#8230;. this makes it so I can write things like this:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\ndrag(spin(createSprite(&quot;Rect&quot;, -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));\r\n<\/pre>\n<p>Readability can be a problem so&#8230; consider that before using this for anything&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)] for (var i:int = 0; i&lt;10; i++){ \/\/ draggable ellipse var dot:Sprite = drag(createSprite(&quot;Ellipse&quot;, -10, -10, 20, 20)); dot.x = Math.random() * stage.stageWidth ; dot.y = Math.random() * stage.stageHeight ; } for (i = 0; i&lt;10; i++){ var box:Sprite = drag(spin(createSprite(&quot;Rect&quot;, -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1)); box.x = [&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,94],"tags":[3,4],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-functions","category-misc","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/159","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=159"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/159\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}