{"id":2507,"date":"2012-11-30T15:43:12","date_gmt":"2012-11-30T22:43:12","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=2507"},"modified":"2012-12-01T14:28:10","modified_gmt":"2012-12-01T21:28:10","slug":"super-simple-script-snippets-button-rollover-rollout-with-enter_frame-fun","status":"publish","type":"post","link":"http:\/\/www.nathalielawhead.com\/candybox\/super-simple-script-snippets-button-rollover-rollout-with-enter_frame-fun","title":{"rendered":"Super Simple Script Snippets: Button RollOver &#038; RollOut With ENTER_FRAME Fun"},"content":{"rendered":"<p>Here&#8217;s something basic, and fun, while my next <a href=\"http:\/\/tetrageddon.com\" target=\"_blank\">game<\/a> is underway &#8212; it&#8217;s a small components from the next release.<br \/>\nDictating the animation of a movieclip with ENTER_FRAMEs &#8211; for smooth and consistent rollover, or rollouts.<br \/>\nI prefer these (as I know many other developers \/ designers do) up against standard button events or animations. <\/p>\n<p>These are <em>basic examples<\/em>, and I&#8217;m sure they&#8217;ll be useful to those starting out, and looking (googling) for a solution like this. So here&#8217;s my approach at it&#8230;<\/p>\n<p><strong>Example #1:<\/strong> have a button, or movieclip, increment or decremnt its frame number according to its mouse event (rollOver, or rollOut) as dictated by an ENTER_FRAME and its frame number.<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_1.fla\" target=\"_blank\">DOWNLOAD SOURCE<\/a>, or <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_1_cs4.fla\" target=\"_blank\">DOWNLOAD AS CS4<\/a><\/p>\n<p>[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_1.swf&#8221; height=&#8221;212&#8243; width=&#8221;190&#8243;]<\/p>\n<p><strong>Code:<\/strong><\/p>\n<p>function rollOver_event(clip:MovieClip, type:String) {<br \/>\n\t\/\/<br \/>\n\tvar num_frame:Number = clip.currentFrame;<br \/>\n\t\/\/<br \/>\n\tfunction _roll(event:Event){<br \/>\n\t\tif(clip.currentFrame< =clip.totalFrames &#038;&#038; type==\"rollOver\"){\n\t\t\ttrace(\"rollOver\");\n\t\t\tclip.gotoAndStop(num_frame+=1);\n\t\t}\n\t\tif(clip.currentFrame>=1 &#038;&#038; type==&#8221;rollOut&#8221;){<br \/>\n\t\t\ttrace(&#8220;rollOut&#8221;);<br \/>\n\t\t\tclip.gotoAndStop(num_frame-=1);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\t\/\/handle listeners and make sure there is always only one&#8230;<br \/>\n\tif(clip.hasEventListener(Event.ENTER_FRAME)){<br \/>\n\t\tclip.removeEventListener(Event.ENTER_FRAME, _roll);<br \/>\n\t}<br \/>\n\tclip.addEventListener(Event.ENTER_FRAME, _roll);<br \/>\n\t\/\/<br \/>\n}<\/p>\n<p>function rollOver_mouse(event:MouseEvent) {<br \/>\n\trollOver_event(MovieClip(event.currentTarget),event.type);<br \/>\n}<\/p>\n<p>function rollOut_mouse(event:MouseEvent){<br \/>\n\trollOver_event(MovieClip(event.currentTarget),event.type);<br \/>\n}<\/p>\n<p>mc_safe.addEventListener(MouseEvent.ROLL_OVER, rollOver_mouse);<br \/>\nmc_safe.addEventListener(MouseEvent.ROLL_OUT, rollOut_mouse);<\/p>\n<p>mc_safe.stop();<\/p>\n<p><strong>Example #2:<\/strong> is the exact same as above but you can also pass it a movieclip name (instead of MovieClip(event.currentTarget)). So that the button dictates the state of an independent\/separate movieclip&#8230;<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_2.fla\" target=\"_blank\">DOWNLOAD SOURCE<\/a>, or <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_2_cs4.fla\" target=\"_blank\">DOWNLOAD AS CS4<\/a><\/p>\n<p>[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_2.swf&#8221; height=&#8221;212&#8243; width=&#8221;190&#8243;]<\/p>\n<p><strong>Code (the minor change&#8230;):<\/strong><\/p>\n<p>function rollOver_mouse(event:MouseEvent) {<br \/>\n\trollOver_event(mc_safe,event.type);<br \/>\n}<\/p>\n<p>function rollOut_mouse(event:MouseEvent){<br \/>\n\trollOver_event(mc_safe,event.type);<br \/>\n}<\/p>\n<p>btn_safe.addEventListener(MouseEvent.ROLL_OVER, rollOver_mouse);<br \/>\nbtn_safe.addEventListener(MouseEvent.ROLL_OUT, rollOut_mouse);<\/p>\n<p><strong>Example #3:<\/strong> Then there&#8217;s this unusual approach (unusual since it seems very niche).<br \/>\nHave a button (or movieclip) play on rollover till a certain designated frame, stay paused at that frame while you&#8217;re rolled over, and continue playing from that point on when you rollout&#8230; Without using boolean flags! Pun intended.<br \/>\nNote: pass the frame number to rollOver_toFrame(btn_safe, mc_safe, 15); I like it. It works.<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_3.fla\" target=\"_blank\">DOWNLOAD SOURCE<\/a>, or <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_3_cs4.fla\" target=\"_blank\">DOWNLOAD AS CS4<\/a><\/p>\n<p>[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/rollover_rollout_3.swf&#8221; height=&#8221;212&#8243; width=&#8221;190&#8243;]<\/p>\n<p><strong>Code (Fun with conditional statements!) :<\/strong><\/p>\n<p>function rollOver_toFrame_event(event:Event)<br \/>\n{<br \/>\n\t\/\/variable set-up<br \/>\n\tvar clip:MovieClip = MovieClip(event.currentTarget);<br \/>\n\tvar hitarea:Object = root[clip.name+&#8221;_hitarea&#8221;];<br \/>\n\tvar num_frame:Number = clip.currentFrame;<br \/>\n\tvar frame:Number = root[clip.name+&#8221;_frame&#8221;];<br \/>\n\t\/\/Rollover (if it&#8217;s less than or greater than the target frame play)<br \/>\n\tif((clip.currentFrame < frame || \n\t\tclip.currentFrame > frame) &#038;&#038;<br \/>\n\t\thitarea.hitTestPoint(mouseX,mouseY)){<br \/>\n\t\t\tclip.gotoAndStop(num_frame+=1);<br \/>\n\t}<br \/>\n\t\/\/Rollout<br \/>\n\tif(clip.currentFrame >= frame &#038;&#038;<br \/>\n\t   !hitarea.hitTestPoint(mouseX,mouseY)){<br \/>\n\t\t\tclip.gotoAndStop(num_frame+=1);<br \/>\n\t}<br \/>\n\t\/\/I triggered it, and am no longer over it, finish playing<br \/>\n\tif (clip.currentFrame < frame &#038;&#038; \n\t\tclip.currentFrame > 1 &#038;&#038;<br \/>\n\t\t!hitarea.hitTestPoint(mouseX,mouseY)){<br \/>\n\t\t\tclip.play();<br \/>\n\t}<br \/>\n\t\/\/Reached the end &#8211; reset<br \/>\n\tif(clip.currentFrame == clip.totalFrames){<br \/>\n\t\tclip.gotoAndStop(1);<br \/>\n\t}<br \/>\n}<br \/>\n\/\/<br \/>\nfunction rollOver_toFrame(hitarea:Object, clip:MovieClip, frame:Number){<br \/>\n\t\/\/set up the variables to get past the event handler<br \/>\n\t\/\/since you can&#8217;t pass function parameters to event handlers<br \/>\n\t\/\/this is one of the few workarounds&#8230;<br \/>\n\t\/\/also make sure it doesn&#8217;t have a listener already&#8230;<br \/>\n\tif(!clip.hasEventListener(Event.ENTER_FRAME)){<br \/>\n\t\troot[clip.name+&#8221;_hitarea&#8221;] = hitarea;<br \/>\n\t\troot[clip.name+&#8221;_frame&#8221;] = frame;<br \/>\n\t\tclip.addEventListener(Event.ENTER_FRAME, rollOver_toFrame_event);<br \/>\n\t}<br \/>\n}<\/p>\n<p>\/\/you can also pass the desired movieclip (that should play)<br \/>\n\/\/as the button\/hitarea parameter value&#8230; for whatever reason&#8230;<br \/>\nrollOver_toFrame(btn_safe, mc_safe, 15);<\/p>\n<p>mc_safe.stop();<\/p>\n<p><strong>Example #0:<\/strong> Aaaand for the hell of it, plus to pay respects to the older language, here&#8217;s <strong>the <em>Actionscript 2<\/em> version of all the above<\/strong>:<\/p>\n<p>function rollOver_event() {<br \/>\n\tthis.onRollOver = function() {<br \/>\n\t\tthis.onEnterFrame = function() {<br \/>\n\t\t\tthis.gotoAndStop(this._currentframe+1);<br \/>\n\t\t\tif (this._currentframe == this._totalframes) {<br \/>\n\t\t\t\tthis.onEnterFrame = undefined;<br \/>\n\t\t\t}<br \/>\n\t\t};<br \/>\n\t};<br \/>\n}<\/p>\n<p>function rollOut_event() {<br \/>\n\tthis.onRollOut = function() {<br \/>\n\t\tthis.onEnterFrame = function() {<br \/>\n\t\t\tthis.gotoAndStop(this._currentframe-1);<br \/>\n\t\t\tif (this._currentframe == this._totalframes-this._totalframes+1) {<br \/>\n\t\t\t\tthis.onEnterFrame = undefined;<br \/>\n\t\t\t}<br \/>\n\t\t};<br \/>\n\t};<br \/>\n}<\/p>\n<p>Use function.apply, and apply it to the button\/moviclip (a.e. rollOver_event.apply(my_clip); ).<\/p>\n<p><strong>On a more complex note!&#8230; <\/strong><br \/>\n<em>I&#8217;ll be putting together a tutorial for a Flash Pandorabots chatbot (pulling Pandorabots into Flash) soon!<\/em><br \/>\nI&#8217;ve done them about a million times, and it would be fun to share that knowledge.<br \/>\nI have a tutorial for installing <a href=\"http:\/\/www.nathalielawhead.com\/candybox\/programe-e-alice-aiml-bot-using-flash-php-actionscript-walkthrough-source-files\" target=\"_blank\">Program E and making a Flash chatbot (click here)<\/a>. It&#8217;s one of the most popular tutorials I&#8217;ve written (with about 407 visits a day, atm).<br \/>\nSeeing that it&#8217;s a popular topic, I&#8217;m sure this next one will be useful to those interested in bots.<\/p>\n<p>For more open source games, and sourcefiles with links to their respective tutorial pages visit:<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/\" target=\"_blank\">Tetrageddon Games Source Files Directory<\/a><\/p>\n<p>Or drop by <a href=\"http:\/\/tetrageddon.com\" target=\"_blank\">Tetrageddon Games<\/a> to play games with all codery vs. art put to use!<br \/>\n<a href=\"http:\/\/tetrageddon.com\" target=\"_blank\"><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Offender\/sup.jpg\" alt=\"\" \/><\/a><\/p>\n<blockquote><p>As long as you <a href=\"https:\/\/plus.google.com\/u\/0\/117289855674566768688\/posts\/9hG9EEJ54GQ\" target=\"_blank\">don\u2019t give up<\/a>, you\u2019re in the winning.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s something basic, and fun, while my next game is underway &#8212; it&#8217;s a small components from the next release. Dictating the animation of a movieclip with ENTER_FRAMEs &#8211; for smooth and consistent rollover, or rollouts. I prefer these (as I know many other developers \/ designers do) up against standard button events or animations. These are basic examples, and I&#8217;m sure they&#8217;ll be useful to those starting out, and looking (googling) for a solution like this. So here&#8217;s my approach at it&#8230; Example #1: have a button, or movieclip, increment or decremnt its frame number according to its mouse&#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-2507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","category-resources"],"_links":{"self":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/comments?post=2507"}],"version-history":[{"count":14,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2507\/revisions"}],"predecessor-version":[{"id":2518,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2507\/revisions\/2518"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media?parent=2507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=2507"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=2507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}