<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rabidGadfly &#187; click</title>
	<atom:link href="http://rabidgadfly.com/tag/click/feed/" rel="self" type="application/rss+xml" />
	<link>http://rabidgadfly.com</link>
	<description>Simple Solutions to Nagging Coding Problems</description>
	<lastBuildDate>Wed, 19 Oct 2011 13:43:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>AS3 Clickable Button Inside a Draggable Movie Clip</title>
		<link>http://rabidgadfly.com/2010/03/as3-clickable-button-inside-a-draggable-movie-clip/</link>
		<comments>http://rabidgadfly.com/2010/03/as3-clickable-button-inside-a-draggable-movie-clip/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 17:08:52 +0000</pubDate>
		<dc:creator>rabidgadfly</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[click]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[movieclip]]></category>

		<guid isPermaLink="false">http://www.rabidgadfly.com/?p=129</guid>
		<description><![CDATA[Demonstrates how to use AS3 to create a clickable button inside a draggable movie clip.]]></description>
			<content:encoded><![CDATA[<p>A few years back I wrote <a href="http://www.rabidgadfly.com/?p=33" alt="AS2 Clickable Button Inside a Draggable Movie Clip">a popular post</a> that demonstrated an AS2 technique to have a clickable button inside of a draggable movie clip. Recently, I received a request to see how it would be accomplished in AS3.</p>
<p>I haven&#8217;t posted in a LONG time and I thought it was a great idea. Not only does it get me blogging again, but it&#8217;s a great, short introduction to a couple of the new features in AS3.<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_buttonInDraggableMC_as3_553842018"
			class="flashmovie"
			width="450"
			height="400">
	<param name="movie" value="http://www.rabidgadfly.com/swf/buttonInDraggableMC_as3.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.rabidgadfly.com/swf/buttonInDraggableMC_as3.swf"
			name="fm_buttonInDraggableMC_as3_553842018"
			width="450"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><span id="more-129"></span></p>
<p style="margin:5px 0 15px 0;"><em><a href="http://www.rabidgadfly.com/fla/buttonInDraggableMC_as3_cs3.zip">Download the sample</a></em></p>
<p>AS3 uses events to handle mouse click functionality. Rather than the AS2 method of adding an onRelease function to the clickable object, you now assign an event listener. While this is a pretty significant difference that may take a bit of getting used to, programmatically it makes a lot of sense.</p>
<p>AS3 used the DOM event model as a basis for its own event handling. So, instead of something that looks like this:</p>
<pre class="brush: as3; title: ; notranslate">
//AS2 button event
container_mc.drag_btn.onPress = function() {
	startDrag(this._parent);
};
</pre>
<p>The AS3 method looks like this:</p>
<pre class="brush: as3; title: ; notranslate">
//AS3 event handling
container_mc.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler_Drag);
function mouseDownHandler_Drag(mEvent:MouseEvent):void {
	mEvent.target.startDrag();
}
</pre>
<p><em>For a good introduction to AS3 Event Handlers, check out the <a href="http://www.newtonflash.com/blog/2009/03/05/as3-event-handling-part-1/">Newton Flash 6 part series</a>. Even though he uses the moniker of &#8220;Devigner&#8221;, which I can&#8217;t stand, the posts are pretty informative and to the point.</em></p>
<p>Another significant difference is that I no longer need an invisible button to allow me to drag the container around. AS3&#8242;s event bubbling makes it possible to listen to both the container and the button within the container at the same time. Ruben Swieringa wrote a <a href="http://www.rubenswieringa.com/blog/eventbubbles-eventcancelable-and-eventcurrenttarget">great post explaining AS3 bubbling</a> which is definitely worth a look.</p>
<p>One addition to the code that may trip you up is the assignment of buttonMode to the Click Me button. AS3 does not assume that you want the hand cursor to appear over a movie clip just because you added a listener. Because of this, by default the cursor will remain an I-bar unless you set the clip&#8217;s buttonMode to true:</p>
<pre class="brush: as3; title: ; notranslate">
container_mc.btn.buttonMode = true;
</pre>
<p>Another change to notice in the sample code is that there are no longer referrals to &#8220;_parent.&#8221; (Though if there were, you would see that the new syntax drops the underscore so that it is simply &#8220;parent&#8221;.) The reason it was required in the first place was because I needed to reference an object that was out of the clickable objects scope, and I didn&#8217;t want to use _root. AS3&#8242;s Event Handling method allows you to put the function code in the same scope as the outer container. This means that I can refer to it directly rather than having to navigate upwards:</p>
<pre class="brush: as3; title: ; notranslate">
//as2
this._parent._parent.text_txt.text=&quot;\rclick #&quot;+counter;

//as3
text_txt.text=&quot;\rclick #&quot;+counter;
</pre>
<p>One final piece that I&#8217;d like to point out is the reference to mEvent.target in my handler functions. mEvent is a reference to the mouse event pulled from the function&#8217;s parms. mEvent.target is a reference to the object that the listener was attached to. So, when I want to start dragging the object that was clicked, I just refer to the target of the event:</p>
<pre class="brush: as3; title: ; notranslate">
mEvent.target.startDrag();
</pre>
<p>My apologies for such a long post about such a short sample. I just felt that this sample offered a great opportunity to address some of the early stumbling blocks when making the move to AS3.</p>
<p>-Glenn</p>
<p>NOTE: If you didn&#8217;t notice the sample link above, <em><a href="http://www.rabidgadfly.com/fla/buttonInDraggableMC_as3.zip">here it is again</a></em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://rabidgadfly.com/2010/03/as3-clickable-button-inside-a-draggable-movie-clip/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

