{"id":221,"date":"2009-03-16T00:00:51","date_gmt":"2009-03-16T07:00:51","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=974"},"modified":"2009-03-16T00:00:51","modified_gmt":"2009-03-16T07:00:51","slug":"functional-pseudo-objects","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=221","title":{"rendered":"Functional Pseudo-Objects"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar moverNum:int = 40;\r\nvar movers:Array = new Array();\r\n\r\nfor (var i:int = 0; i&lt;moverNum; i++){\r\n\tmovers.push(makeMover());\r\n}\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tfor (var i:int = 0; i&lt;moverNum; i++){\r\n\t\tmovers&#x5B;i]();\r\n\t}\r\n}\r\n\r\nfunction makeMover():Function{\r\n\t\/\/ mover vars &amp; setup\r\n\tvar xVel:Number = Math.random() * 5 + 1;\r\n\tvar right:Number = stage.stageWidth + 30;\r\n\tvar s:Shape = Shape(addChild(new Shape()));\r\n\twith(s.graphics) beginFill(0xFF0000), drawCircle(0,0,5);\r\n\ts.x = Math.random() * stage.stageWidth;\r\n\ts.y = Math.random() * stage.stageHeight;\r\n\t\/\/ return a &quot;run&quot; function\r\n\treturn function():void {\r\n\t\t s.x += xVel;\r\n\t\t if (s.x &gt; right){\r\n\t\t\ts.x = -30; \r\n\t\t }\r\n\t}\r\n}\r\n<\/pre>\n<p>This snippet creates 40 shapes that move to the right at a random velocity. When they animate off the right hand side of the stage they return from the left.<\/p>\n<p>The makeMover() function has a series of variable definitions related to the x velocity and position of a Shape. The makeMover() function then returns an anonymous function that contains some logic to move the Shape &#8220;s&#8221;. The anonymous function has access to the makeMover() functions temporary variables&#8230;<\/p>\n<p>On the main loop we run all of the anonymous functions &#8211; animating all the shapes by their random x velocities.  <\/p>\n<p>It would be interesting to write the same program using classes and test the for speed differences. My assumption is that anonymous functions are expensive, but it would still be interesting to see&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var moverNum:int = 40; var movers:Array = new Array(); for (var i:int = 0; i&lt;moverNum; i++){ movers.push(makeMover()); } addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { for (var i:int = 0; i&lt;moverNum; i++){ movers&#x5B;i](); } } function makeMover():Function{ \/\/ mover vars &amp; setup var xVel:Number = Math.random() * 5 + 1; var right:Number = stage.stageWidth + 30; var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[3,4],"class_list":["post-221","post","type-post","status-publish","format-standard","hentry","category-functions","category-motion","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/221","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=221"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/221\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}