{"id":2696,"date":"2013-03-27T15:52:09","date_gmt":"2013-03-27T22:52:09","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=2696"},"modified":"2013-03-27T15:57:34","modified_gmt":"2013-03-27T22:57:34","slug":"super-simple-script-snippets-%e2%80%93-part-2","status":"publish","type":"post","link":"https:\/\/www.nathalielawhead.com\/candybox\/super-simple-script-snippets-%e2%80%93-part-2","title":{"rendered":"Super Simple Script Snippets \u2013 Part 2"},"content":{"rendered":"<p>This is part two of my previous post of fun code trickery. <a href=\"http:\/\/www.nathalielawhead.com\/candybox\/super-simple-script-snippets-standalone-leaderboard-system-and-more\" target=\"_blank\">Click here<\/a>.<br \/>\nI&#8217;m thrilled with how development is going. It will definitely be a fun mobile game. <\/p>\n<p>Like I said, you can find more in the Open Source Directory. <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/\" target=\"_blank\">Click here.<\/a><\/p>\n<p>So&#8230; I should hit &#8220;Publish&#8221; now before I get distracted too much with my blog. :)<\/p>\n<p><strong>Random Sound From Array Of Strings<\/strong><br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/random_sound_from_array.fla\">Download Here<\/a><br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/random_sound_from_array.swf&#8221; height=&#8221;456&#8243; width=&#8221;608&#8243;]<\/p>\n<p><strong>If Removed From Stage Remove All Listeners<\/strong><br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/remove_from_stage_remove_all_listeners.fla\">Download Here<\/a><br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/remove_from_stage_remove_all_listeners.swf&#8221; height=&#8221;80&#8243; width=&#8221;428&#8243;]<\/p>\n<p>Code:<\/p>\n<p><code>\/\/loop through an array of functions\/listeners that you pass it<br \/>\n\/\/removes them<br \/>\n\/\/call this from wherever the listeners are added<br \/>\n\/\/usage: remove_listener(clip, [listener1, listener2]);<br \/>\nfunction remove_listener(clip:Object, list:Array){<br \/>\n\tfunction removed(event:Event){<br \/>\n\t\ttrace(clip.name+\" removed listener\");<br \/>\n\t\tfor(var i:Number = 0; i < list .length; ++i){\n\t\t\tevent.currentTarget.removeEventListener(Event.ENTER_FRAME, list[i]);\n\t\t\t\/\/delete this:\n\t\t\ttxt_list.text += \"\\n\"+String(event.currentTarget+\" removed \"+i+\" listeners\");\n\t\t}\n\t\tevent.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed);\n\t}\n\tclip.addEventListener(Event.REMOVED_FROM_STAGE,removed);\n}<\/code><\/p>\n<p><strong>Remove All Children Of Certain Class<\/strong><br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/removeAllChildren.fla\">Download Here<\/a><br \/>\n[swfobj src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/removeAllChildren.swf\" height=\"148\" width=\"273\"]<\/p>\n<p>Code:<\/p>\n<p>\/\/Removes all childrens of a Class.<br \/>\n\/\/Loops through container<br \/>\n\/\/Removes what it finds<br \/>\nfunction removeAllChildren(obj:Class){<br \/>\n\t\/\/pushed to array because if you remove<br \/>\n\t\/\/while you loop through the stage<br \/>\n\t\/\/then the loop terminates prematurely<br \/>\n\tvar removeArray:Array=new Array();<br \/>\n\tfor(var i:Number=0;i < this .numChildren;i++){\n\t\tif(this.getChildAt(i) is obj){\n\t\t\tremoveArray.push(this.getChildAt(i));\n\t\t}\n\t}\n\t\/\/after you got it all, remove\n\tfor (var j:Number = 0; j<removeArray.length; ++j){\n\t\tremoveChild(removeArray[j]);\n\t}\n};\n\n\n<strong>Reverse Snow<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/reverse_snow.fla\">DOWNLOAD SOURCE<\/a><br \/>\n[swfobj src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/reverse_snow.swf\" height=\"400\" width=\"500\"]<\/p>\n<p>Code:<\/p>\n<p><\/code><code>\/\/SNOW\/\/<br \/>\nfunction initSnowFunc(clip:MovieClip, windSpeed:Number)  {<br \/>\n\t\/\/<br \/>\n\tvar radius:Number = 0;<br \/>\n\tvar xpos:Number = clip.x;<br \/>\n\tvar i:Number = 1+Math.random()*2;<br \/>\n\tvar k:Number = -Math.PI+Math.random()*Math.PI;<br \/>\n\t\/\/set up first run<br \/>\n\tclip.x = Math.random()*stage.stageWidth;<br \/>\n\tclip.y = Math.random()*stage.stageHeight;<br \/>\n\tgotoAndStopRand(clip);<br \/>\n\t\/\/move<br \/>\n\tfunction this_onEnterFrame(event:Event) {<br \/>\n\t\tvar this_clip = event.currentTarget;<br \/>\n\t\t\/\/horizontal<br \/>\n\t\tradius -= (k\/180)*Math.PI;<br \/>\n\t\tthis_clip.x += Math.ceil(windSpeed*(Math.cos(radius)));<br \/>\n\t\t\/\/vertical<br \/>\n\t\tthis_clip.y -= Math.ceil(i);<br \/>\n\t\t\/\/re-start<br \/>\n\t\tif (this_clip.y< =-30) {\n\t\t\tgotoAndStopRand(this_clip);\n\t\t\tthis_clip.y = stage.stageHeight;\n\t\t\tthis_clip.x = Math.random()*stage.stageWidth;\n\t\t}\n\t}\n\t\/\/if snow has been removed then clear all listeners\n\tfunction removed(event:Event){\n\t\tevent.currentTarget.removeEventListener(Event.ENTER_FRAME, this_onEnterFrame);\n\t\tevent.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed);\n\t}\n\tclip.addEventListener(Event.REMOVED_FROM_STAGE,removed);\n\tclip.addEventListener(Event.ENTER_FRAME, this_onEnterFrame);\n\t\/\/\n};\n\n\/\/Call this to make\nfunction createSnow(snowAmount:Number, windSpeed:Number)  {\n\tfor ( var i:Number = 0; i<snowAmount; i++) {\n\t\tvar snow:MovieClip = new snowMC();\n\t\taddChild(snow);\n\t\tsnow.name = \"snow\"+i;\n\t\tsnow.mouseEnabled = false;\n\t\tsnow.buttonMode = false;  \n\t\tsnow.mouseChildren = false;\n\t\tinitSnowFunc(snow, windSpeed);\/\/move\n\t}\n};\n\n\/\/Call to remove\nfunction removeSnow()  {\n\tfor (var i:Number = 0; i&lt;999; i++) {\n\t\ttry{\n\t\t\tremoveChildByName(String(\"snow\"+i));\n\t\t}catch(e:Error){\n\t\t\t\/\/trace(\"ok stop\");\n\t\t}\n\t}\n};\n\n\/\/Misc\nfunction removeChildByName(childName:String){\n\tremoveChild(getChildByName(childName));\n}<\/code><\/p>\n<p>-----------<br \/>\n<strong>Button Over And Out Animation With Enter Frame<\/strong><br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/rollOver_rollOut_EnterFrame.fla\">DOWNLOAD SOURCE <\/a><br \/>\n[swfobj src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/rollOver_rollOut_EnterFrame.swf\" height=\"286\" width=\"340\"]<\/p>\n<p>Code:<\/p>\n<p><\/code><code>\/\/onRollOver mc animations<br \/>\nfunction onRollOverMC(myBtn:Object, myMc:MovieClip) {<br \/>\n\t\/\/<br \/>\n\tfunction myMc_onEnterFrame_Over(event:Event) {<br \/>\n\t\tmyMc.gotoAndStop(myMc.currentFrame+1);<br \/>\n\t\tif (myMc.currentFrame == myMc.totalFrames) {<br \/>\n\t\t\tmyMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Over);<br \/>\n\t\t}<br \/>\n\t};<br \/>\n\t\/\/<br \/>\n\tfunction myMc_onEnterFrame_Out(event:Event) {<br \/>\n\t\tmyMc.gotoAndStop(myMc.currentFrame-1);<br \/>\n\t\tif (myMc.currentFrame == myMc.totalFrames) {<br \/>\n\t\t\tmyMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Out);<br \/>\n\t\t}<br \/>\n\t};<br \/>\n\t\/\/<br \/>\n\tfunction myBtn_onRollOver(event:MouseEvent) {<br \/>\n\t\tmyMc.addEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Over);<br \/>\n\t\tmyMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Out);<br \/>\n\t};<br \/>\n\t\/\/<br \/>\n\tfunction myBtn_onRollOut(event:MouseEvent) {<br \/>\n\t\tmyMc.addEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Out);<br \/>\n\t\tmyMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Over);<br \/>\n\t};<br \/>\n\t\/\/<br \/>\n\tif (!myBtn.hasEventListener(MouseEvent.MOUSE_OVER)) {<br \/>\n\t\tmyBtn.addEventListener(MouseEvent.MOUSE_OVER, myBtn_onRollOver);<br \/>\n\t}<br \/>\n\tif(!myBtn.hasEventListener(MouseEvent.MOUSE_OUT)){<br \/>\n\t\tmyBtn.addEventListener(MouseEvent.MOUSE_OUT, myBtn_onRollOut);<br \/>\n\t}<br \/>\n}<\/p>\n<p>onRollOverMC(mc_play, mc_play_back);<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part two of my previous post of fun code trickery. Click here. I&#8217;m thrilled with how development is going. It will definitely be a fun mobile game. Like I said, you can find more in the Open Source Directory. Click here. So&#8230; I should hit &#8220;Publish&#8221; now before I get distracted too much with my blog. :) Random Sound From Array Of Strings Download Here [swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/random_sound_from_array.swf&#8221; height=&#8221;456&#8243; width=&#8221;608&#8243;] If Removed From Stage Remove All Listeners Download Here [swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Haxed_By_Megahurtz\/snippets_AS3\/remove_from_stage_remove_all_listeners.swf&#8221; height=&#8221;80&#8243; width=&#8221;428&#8243;] Code: \/\/loop through an array of functions\/listeners that you pass it \/\/removes them \/\/call this from&#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":[30,12],"tags":[],"class_list":["post-2696","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","category-resources"],"_links":{"self":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2696","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=2696"}],"version-history":[{"count":18,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2696\/revisions"}],"predecessor-version":[{"id":2715,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2696\/revisions\/2715"}],"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=2696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=2696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=2696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}