{"id":1256,"date":"2011-03-12T01:39:18","date_gmt":"2011-03-12T08:39:18","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=1256"},"modified":"2014-01-28T17:10:05","modified_gmt":"2014-01-29T00:10:05","slug":"actionscript-3-code-snipplet-of-the-day-enhanced-typeof-evaluation","status":"publish","type":"post","link":"https:\/\/www.nathalielawhead.com\/candybox\/actionscript-3-code-snipplet-of-the-day-enhanced-typeof-evaluation","title":{"rendered":"Actionscript 3 Code Snipplet of The Day: Enhanced &#8220;Typeof&#8221; Evaluation"},"content":{"rendered":"<p><code>typeof<\/code> is handy but can be a little too flimsy at times.<br \/>\nBasically all it <a href=\"http:\/\/help.adobe.com\/en_US\/FlashPlatform\/reference\/actionscript\/3\/operators.html#typeof\" target=\"_blank\">really understands<\/a> are only six possible values;<br \/>\n<code><br \/>\nboolean,<br \/>\nfunction,<br \/>\nnumber,<br \/>\nobject,<br \/>\nstring,<br \/>\nand xml<\/code>.<br \/>\nIf you want to run a conditional to check if you&#8217;ve got some common ethnic group, such as a <code>movieclip<\/code>, on your hands then it works charms&#8230; BUT if you want to use <code>typeof<\/code> to check for a <code>Text Field<\/code>, or maybe something more exciting, then it&#8217;ll fail.<br \/>\nAt least I know of no other way&#8230;<br \/>\nSo!<br \/>\nToday&#8217;s super simple code blurb of the day is:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">for<\/span> <span style=\"color: #333333\">(<\/span><span style=\"color: #008800; font-weight: bold\">var<\/span> i:<span style=\"color: #333399; font-weight: bold\">Number<\/span><span style=\"color: #333333\">=<\/span><span style=\"color: #0000DD; font-weight: bold\">0<\/span><span style=\"color: #333333\">;<\/span> i<span style=\"color: #333333\">&lt;<\/span>clip<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">numChildren<\/span><span style=\"color: #333333\">;<\/span> i<span style=\"color: #333333\">++)<\/span>\r\n<span style=\"color: #333333\">{<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> clips:<span style=\"color: #333399; font-weight: bold\">Object<\/span> <span style=\"color: #333333\">=<\/span> clip<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">getChildAt<\/span><span style=\"color: #333333\">(<\/span>i<span style=\"color: #333333\">);<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> <span style=\"color: #333333\">(<\/span>clips<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">toString<\/span><span style=\"color: #333333\">()<\/span> <span style=\"color: #333333\">==<\/span> <span style=\"background-color: #fff0f0\">&#39;[object TextField]&#39;<\/span><span style=\"color: #333333\">)<\/span>\r\n\t<span style=\"color: #333333\">{<\/span>\r\n\t\t<span style=\"color: #888888\">\/\/do shit here<\/span>\r\n\t\t<span style=\"color: #0066BB; font-weight: bold\">trace<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;I&#39;m a text field!&quot;<\/span><span style=\"color: #333333\">);<\/span>\r\n\t<span style=\"color: #333333\">}<\/span>\r\n<span style=\"color: #333333\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>The above loops through all nested <code>movieclips<\/code> inside of a <code>parent movieclip<\/code> and then checks what content is a text field. If it is then do what needs to be done&#8230; In this case just <code>trace<\/code>, because&#8230; <code>trace<\/code> is just what you do if you&#8217;re at a loss of what to do&#8230; ok!? Don&#8217;t judge me!<\/p>\n<p>&#8230;Note that <code>(clips.toString() == '[object TextField]')<\/code> is the bit that you probably came googling here for. You can substitute <code>clips<\/code> for whatever the hell you need to check against. The above looping through the children movieclips is just for added karma points.<br \/>\n+10<br \/>\nYou have gained karma reading this.<br \/>\nYou now have enough to <a href=\"http:\/\/nathalielawhead.com\/noodles\/\" target=\"_blank\">confront the grue.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>typeof is handy but can be a little too flimsy at times. Basically all it really understands are only six possible values; boolean, function, number, object, string, and xml. If you want to run a conditional to check if you&#8217;ve got some common ethnic group, such as a movieclip, on your hands then it works charms&#8230; BUT if you want to use typeof to check for a Text Field, or maybe something more exciting, then it&#8217;ll fail. At least I know of no other way&#8230; So! Today&#8217;s super simple code blurb of the day is: for (var i:Number=0; i&lt;clip.numChildren; i++)&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[12],"tags":[],"class_list":["post-1256","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-resources"],"_links":{"self":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/1256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/comments?post=1256"}],"version-history":[{"count":4,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/1256\/revisions"}],"predecessor-version":[{"id":2972,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/1256\/revisions\/2972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media?parent=1256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=1256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=1256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}