
{"id":412,"date":"2008-04-30T10:08:02","date_gmt":"2008-04-30T09:08:02","guid":{"rendered":"http:\/\/www.matthijskamstra.nl\/blog\/?p=412"},"modified":"2008-05-04T02:20:11","modified_gmt":"2008-05-04T01:20:11","slug":"as2-to-as3-get-all-objects-in-a-movieclip","status":"publish","type":"post","link":"https:\/\/www.matthijskamstra.nl\/blog\/2008\/04\/30\/as2-to-as3-get-all-objects-in-a-movieclip\/","title":{"rendered":"AS2 to AS3: get all objects in a movieclip"},"content":{"rendered":"<p>Sometimes you want a list of everything inside a movieclip. For example: you want to know the instance names of every movie in the root.<\/p>\n<h3>ActionScript 2<\/h3>\n<p>A little trick that I used a lot in AS2 is:<\/p>\n<div class=\"showcode\">\n<pre>\r\nfor(var i in _root){\r\n   trace('key: ' + i + ', value: ' + _root[i]);\r\n}\r\n<\/pre>\n<\/div>\n<p>or <\/p>\n<div class=\"showcode\">\n<pre>\r\nfor(var i in target_mc){\r\n   trace('key: ' + i + ', value: ' + target_mc[i]);\r\n}\r\n<\/pre>\n<\/div>\n<p>to find out which movies are in <code>target_mc<\/code><\/p>\n<p>It was not only a trick to <code>trace <\/code> everything, I also used it to reset movieclips (place them outside the stage, or delete them) or quickly make buttons out of them:<\/p>\n<div class=\"showcode\">\n<pre>\r\nfor(var i in target_mc){\r\n\ttarget_mc[i].id = i;\r\n\ttarget_mc[i].onRelease = function (){\r\n\t\ttrace (\"id = \" + this.id); \/\/ onRelease the id will be called (the name of the movie)\r\n\t};\r\n}\r\n<\/pre>\n<\/div>\n<p><\/p>\n<h3>ActionScript 3<\/h3>\n<p>But in AS3 this doesn&#8217;t work any more!<br \/>\nAnd I know: AS3 should make my life easier&#8230;. well if I see the solution to the problem created by AS3, I have to disagree!<\/p>\n<div class=\"showcode\">\n<pre>\r\nfor (var i:uint = 0; i < target_mc.numChildren; i++){\r\n\ttrace ('\\t|\\t ' +i+'.\\t name:' + target_mc.getChildAt(i).name + '\\t type:' + typeof (target_mc.getChildAt(i))+ '\\t' + target_mc.getChildAt(i));\r\n}\r\n<\/pre>\n<\/div>\n<p>In AS3 you first have to get the \"number of children\" (<code>numChildren<\/code>) in a <code>DisplayObjectContainer<\/code>, then you have to say which child you want (<code>getChildAt(i)<\/code>)...<br \/>\nA good thing about AS3 is, you get <strong>everything<\/strong> in a movieClip, even shapes you made there without a instance name.<\/p>\n<p>I'm glad that I work with <a href=\"http:\/\/www.flashdevelop.org\/community\/\">FlashDevelop<\/a>, which has <a href=\"http:\/\/en.wikipedia.org\/wiki\/Snippet_%28programming%29\">snippets<\/a> so I don't have to type this constantly!<\/p>\n<p>(For the people that use FlashDevelop too, here is my snippet:)<\/p>\n<div class=\"showcode\">\n<pre>\r\n\/\/ $(Clipboard)is a DisplayObject\r\ntrace ('+ number of DisplayObject: ' + $(Clipboard).numChildren + '  --------------------------------');\r\nfor (var i:uint = 0; i < $(Clipboard).numChildren; i++){\r\n\ttrace ('\\t|\\t ' +i+'.\\t name:' + $(Clipboard).getChildAt(i).name + '\\t type:' + typeof ($(Clipboard).getChildAt(i))+ '\\t' + $(Clipboard).getChildAt(i));\r\n}\r\ntrace ('\\t+ --------------------------------------------------------------------------------------');\r\n<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want a list of everything inside a movieclip. For example: you want to know the instance names of every movie in the root. ActionScript 2 A little trick that I used a lot in AS2 is: for(var i in _root){ trace(&#8216;key: &#8216; + i + &#8216;, value: &#8216; + _root[i]); } or for(var [&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],"tags":[68,39,97,408,398],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-as3","tag-actionscript","tag-as2","tag-as2-to-as3","tag-as3","tag-flash"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/412","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=412"}],"version-history":[{"count":0,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/media?parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/categories?post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matthijskamstra.nl\/blog\/wp-json\/wp\/v2\/tags?post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}