{"id":784,"date":"2008-09-06T08:00:45","date_gmt":"2008-09-06T12:00:45","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=784"},"modified":"2022-05-29T08:23:34","modified_gmt":"2022-05-29T12:23:34","slug":"tutorial-simple-dynamic-perspective-in-as2-and-as3","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-simple-dynamic-perspective-in-as2-and-as3\/","title":{"rendered":"Tutorial: Simple Dynamic Perspective in AS2 and AS3"},"content":{"rendered":"<p>This tutorial will show you how to create a simple illusion of perspective, dynamically in both ActionScript 2.0 and ActionScript 3.0. Although this technique isn&#8217;t the most advanced or realistic, it gets the job done. Let&#8217;s see it, shall we?<\/p>\n<h3>ActionScript 2.0<\/h3>\n<p>We&#8217;ll first make the effect in ActionScript 2.0. To begin, we have to create an object that we have to add the perspective to. I&#8217;m simply going to make a square that is 50x50px. Here it is:<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/dynamic-perspective\/box.gif\" alt=\"My lil box\" \/><br \/>\nNext, convert it into a MovieClip. Make sure it&#8217;s registration point is set to the center. It doesn&#8217;t matter what you call it. I&#8217;m going to call mine &#8220;box&#8221;. After doing so, give your newly created MovieClip an instance name of <tt>mcBox<\/tt>. We can now reference it in ActionScript.<\/p>\n<p>Now, simply create a new layer called &#8220;actions&#8221; and place the following code into it:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/creating a function that will run every frame\r\nmcBox.onEnterFrame = function(){\r\n\t\/\/scaling the box based on the y value\r\n\tmcBox._xscale = 100*(mcBox._y\/Stage.height);\r\n\tmcBox._yscale = 100*(mcBox._y\/Stage.height);\r\n\t\r\n\t\/\/making the box follow the mouse\r\n\t\/\/so we can actually show the perspective change dynamically\r\n\tmcBox._x = _xmouse;\r\n\tmcBox._y = _ymouse;\r\n}\r\n<\/pre>\n<p>It&#8217;s pretty easy, right? But, if we test out the application, it isn&#8217;t as realistic as it could be. All you have to do to help with it is to add a gradient to the background, and have a horizon line at the top. Here&#8217;s the final product:<\/p>\n<p><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"550\" height=\"400\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/dynamic-perspective\/source-as2.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/dynamic-perspective\/source-as2.swf\"><\/embed><\/object><\/p>\n<h3>ActionScript 3.0<\/h3>\n<p>Now, we can make this effect in ActionScript 3.0. We&#8217;re going to start off with the same thing as we did in AS2. If you skipped that part, don&#8217;t worry I&#8217;ll sum it up for you.<\/p>\n<ol>\n<li>Make a 50x50px box<\/li>\n<li>Convert it into a MovieClip with registration point in center<\/li>\n<li>Give it an instance name of <tt>mcBox<\/tt><\/li>\n<li>Create a new layer called actions<\/li>\n<\/ol>\n<p>Now, all we have to do is add some code to the actions layer. Here it is:<\/p>\n<pre lang=\"actionscript\">\r\nmcBox.addEventListener(Event.ENTER_FRAME, moveBox);\r\n\r\n\/\/creating a function that will run every frame\r\nfunction moveBox(event:Event):void{\r\n\t\/\/scaling the box based on the y value\r\n\tmcBox.scaleX = mcBox.y\/stage.stageHeight;\r\n\tmcBox.scaleY = mcBox.y\/stage.stageHeight;\r\n\t\r\n\t\/\/making the box follow the mouse\r\n\t\/\/so we can actually show the perspective change dynamically\r\n\tmcBox.x = mouseX;\r\n\tmcBox.y = mouseY;\r\n}\r\n<\/pre>\n<p>Pretty nifty code, eh? Well, here&#8217;s what it&#8217;ll turn out to look like (after I add a gradient background).<\/p>\n<p><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"550\" height=\"400\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/dynamic-perspective\/source-as3.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/dynamic-perspective\/source-as3.swf\"><\/embed><\/object><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will show you how to create a simple illusion of perspective, dynamically in both ActionScript 2.0 and ActionScript 3.0. Although this technique isn&#8217;t the most advanced or realistic, it gets the job done. Let&#8217;s see it, shall we? ActionScript 2.0 We&#8217;ll first make the effect in ActionScript 2.0. To begin, we have to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,7,8,65,4],"tags":[13,182,154,145,11],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/784"}],"collection":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/comments?post=784"}],"version-history":[{"count":6,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/784\/revisions"}],"predecessor-version":[{"id":789,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/784\/revisions\/789"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=784"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}