{"id":211,"date":"2009-03-06T00:00:12","date_gmt":"2009-03-06T07:00:12","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=896"},"modified":"2009-03-06T00:00:12","modified_gmt":"2009-03-06T07:00:12","slug":"parallax-based-on-y","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=211","title":{"rendered":"Parallax Based on Y"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(backgroundColor=0x000000, frameRate=30)]\r\n\r\nvar elements:Array = new Array();\r\nfor (var i:int = 0; i&lt;200; i++){\r\n\tvar c:MovieClip= MovieClip(addChild(new MovieClip()));\r\n\tc.x = Math.random()*(stage.stageWidth + 100) - 50;\r\n\tc.y = Math.random()*stage.stageHeight;\r\n\twith(c.graphics) lineStyle(2, 0xFFFFFF,3), drawCircle(0,0,2 + c.y \/ 20);\r\n\tc.startX = c.x;\r\n\telements.push(c);\r\n}\r\n\r\nvar offset:Number = 0;\r\nvar t:Number = 0;\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tt+=.1;\r\n\toffset = 200 * Math.cos(t);\r\n\tfor (var i:int = 0; i&lt;elements.length; i++){\r\n\t   elements&#x5B;i].x = elements&#x5B;i].startX + offset \/ ((stage.stageWidth - elements&#x5B;i].y) \/ 80);\r\n\t}\r\n}\r\n<\/pre>\n<p>I wrote this snippet originally to automatically add parallax motion to a bunch of quick drawings I did within the flash IDE. Each movieClip in the elements array is moved from left to right based on it&#8217;s y position. Clips with a higher y value will oscillate more from left to right than clips with lower y values.<br \/>\n<a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/looking_for.swf\" target=\"blank\"><br \/>\nHere is the original drawing I used this on.<\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/looking_for.swf&quot;,&quot;title&quot;:&quot;looking for&quot;,&quot;width&quot;:800,&quot;height&quot;:600,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: looking for<\/span><\/button><\/span><\/p>\n<p><a data-asruffle-linked=\"true\" href=\"https:\/\/actionsnippet.com\/swfs\/parallaxy.swf\" target=\"blank\">And here is what the above snippet will create.<\/a> <span class=\"asruffle-link\" data-swf=\"{&quot;url&quot;:&quot;https:\\\/\\\/actionsnippet.com\\\/swfs\\\/parallaxy.swf&quot;,&quot;title&quot;:&quot;parallaxy&quot;,&quot;width&quot;:550,&quot;height&quot;:400,&quot;missing&quot;:false,&quot;allowScriptAccess&quot;:false}\"><button type=\"button\" hidden aria-expanded=\"false\">Play demo<span class=\"screen-reader-text\">: parallaxy<\/span><\/button><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(backgroundColor=0x000000, frameRate=30)] var elements:Array = new Array(); for (var i:int = 0; i&lt;200; i++){ var c:MovieClip= MovieClip(addChild(new MovieClip())); c.x = Math.random()*(stage.stageWidth + 100) &#8211; 50; c.y = Math.random()*stage.stageHeight; with(c.graphics) lineStyle(2, 0xFFFFFF,3), drawCircle(0,0,2 + c.y \/ 20); c.startX = c.x; elements.push(c); } var offset:Number = 0; var t:Number = 0; addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { t+=.1; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[3,4],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-motion","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/211","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=211"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/211\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}