
{"id":377,"date":"2008-08-27T09:00:06","date_gmt":"2008-08-27T08:00:06","guid":{"rendered":"http:\/\/www.matthijskamstra.nl\/blog\/?p=377"},"modified":"2009-05-28T22:16:57","modified_gmt":"2009-05-28T21:16:57","slug":"from-as2-to-as3-where-did-it-go-swapdepths","status":"publish","type":"post","link":"https:\/\/www.matthijskamstra.nl\/blog\/2008\/08\/27\/from-as2-to-as3-where-did-it-go-swapdepths\/","title":{"rendered":"From AS2 to AS3 &#8211; Where did it go &#8211; swapDepths"},"content":{"rendered":"<p>Where did <code>swapDepths ()<\/code> go in AS3?<\/p>\n<p>This is what the <a href=\"http:\/\/livedocs.adobe.com\/flex\/2\/langref\/migration.html\">ActionScript 2.0 Migration<\/a> has to say about this:<\/p>\n<table width='95%' border=\"1\" cellpadding=\"0\" cellspacing=\"0\">\n<tr style='background-color:silver'>\n<td><strong>ActionScript&nbsp;2.0&nbsp;<\/strong><\/td>\n<td><strong>ActionScript&nbsp;3.0&nbsp;<\/strong><\/td>\n<td><strong>Comments<\/strong><\/td>\n<\/tr>\n<tr style='background-color:#F5F5F5'>\n<td>swapDepths() Method <\/td>\n<td>Removed <\/td>\n<td>In ActionScript 3.0, you can achieve similar functionality by using the methods of the DisplayObjectContainer class, such as the addChildAt(), setChildIndex(), swapChildren(), and swapChildrenAt() methods. <\/td>\n<\/tr>\n<\/table>\n<p>Removed&#8230; bummer, so with what do I replace it with&#8230;<\/p>\n<p>In AS2 you can use <code>swapDepths()<\/code> in two ways:<\/p>\n<blockquote>\n<ul>\n<li>A Number that specifies the depth level where the movie clip is to be placed. <\/li>\n<li>An instance name that specifies the movie clip instance whose depth is swapped with the movie clip for which the method is being applied. Both movie clips must have the same parent movie clip. <\/li>\n<\/ul>\n<\/blockquote>\n<p>In the examples there are two movieClips: &#8216;<code>circle_mc<\/code>&#8216; and &#8216;<code>square_mc<\/code>&#8216;. Movieclip <code>square_mc<\/code> is under <code>circle_mc<\/code> (z-index is lower then circle_mc). And the task is to get  <code>square_mc<\/code> above <code>circle_mc<\/code>.<\/p>\n<h3>AS 2<\/h3>\n<p>There are a couple of ways to do that in ActionScript 2:<\/p>\n<p><strong>Example #1<\/strong><br \/>\nSwap the depth of two movieclips which each other (square_mc will be placed on the depth of circle_mc, and the other way around)<br \/>\n<code>this.square_mc.swapDepths(this.circle_mc);<\/code><\/p>\n<p><strong>Example #2<\/strong><br \/>\nI consider this a hack, but one that works<br \/>\n<code>this.square_mc.swapDepths(1000000);<\/code><\/p>\n<p><strong>Example #3<\/strong><br \/>\nWhen you want to change the depth of something, this is usually to place it on top:<br \/>\n<code>this.square_mc.swapDepths(this.getNextHighestDepth());<\/code><\/p>\n<h3>AS 3<\/h3>\n<p>Lets try this in ActionScript 3:<\/p>\n<p><strong>Example #1<\/strong><br \/>\n<code>this.swapChildren(square_mc, circle_mc);<\/code><br \/>\n<em>Visit <a href=\"http:\/\/livedocs.adobe.com\/flash\/9.0\/ActionScriptLangRefV3\/flash\/display\/DisplayObjectContainer.html#swapChildren()\">livedocs<\/a> at the Adobe site for more information and example code<\/em><br \/>\n[as]<br \/>\nvar square = this.getChildByName (&#8216;square_mc&#8217;);<br \/>\nvar circle = this.getChildByName (&#8216;circle_mc&#8217;);<\/p>\n<p>trace(this.getChildAt(0).name); \/\/ square_mc<br \/>\ntrace(this.getChildAt(1).name); \/\/ circle_mc<\/p>\n<p>this.swapChildren(square as DisplayObject, circle as DisplayObject);<\/p>\n<p>trace(this.getChildAt(0).name); \/\/ circle_mc<br \/>\ntrace(this.getChildAt(1).name); \/\/ square_mc<br \/>\n[\/as]<\/p>\n<p><strong>Example #2<\/strong><br \/>\nnot possible anymore&#8230; if you want a detailed explanation visit <a href=\"http:\/\/www.dreaminginflash.com\/2007\/11\/06\/as2-swapdepths-vs-as3-setchildindex\/\">dreaming in Flash<\/a> blog, and read more about swapDepths() in AS3  (it&#8217;s explained with a image)<\/p>\n<p><strong>Example #3<\/strong><br \/>\nTo place a movieclip on the highest depth in actionscript2, we will use MovieClip.getNextHighestDepth,<br \/>\nbut in AS3 that function is removed \ud83d\ude41<\/p>\n<p>In AS3 you need to use <code>numChildren<\/code>:<br \/>\n<code>this.setChildIndex ( square_mc , this.numChildren - 1 );<\/code><br \/>\n<em>Visit <a href=\"http:\/\/livedocs.adobe.com\/flash\/9.0\/ActionScriptLangRefV3\/flash\/display\/DisplayObjectContainer.html#setChildIndex()\">livedocs<\/a> at the Adobe site for more information and example code<\/em><\/p>\n<p>I realize that this is not a complete explanation&#8230; but I hope this is a starting point to find old function that you used in AS2<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Where did swapDepths () go in AS3? This is what the ActionScript 2.0 Migration has to say about this: ActionScript&nbsp;2.0&nbsp; ActionScript&nbsp;3.0&nbsp; Comments swapDepths() Method Removed In ActionScript 3.0, you can achieve similar functionality by using the methods of the DisplayObjectContainer class, such as the addChildAt(), setChildIndex(), swapChildren(), and swapChildrenAt() methods. Removed&#8230; bummer, so with what [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,172,3],"tags":[68,39,97,398,114,168,166],"class_list":["post-377","post","type-post","status-publish","format-standard","hentry","category-as3","category-as3-migration","category-flash","tag-actionscript","tag-as2","tag-as2-to-as3","tag-flash","tag-from-as2-to-as3","tag-migration","tag-movieclip"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":3,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":945,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions\/945"}],"wp:attachment":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}