

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: How to Constrain Dragging to a Circle</title>
	<atom:link href="http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle/feed" rel="self" type="application/rss+xml" />
	<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle</link>
	<description>Luck is the residue of good design.</description>
	<lastBuildDate>Tue, 20 Mar 2012 15:33:23 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Darpan Dalal</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-401011</link>
		<dc:creator>Darpan Dalal</dc:creator>
		<pubDate>Tue, 30 Jun 2009 09:37:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-401011</guid>
		<description>thank you very very much your code is beautiful for constraining 

thanks very much again :)</description>
		<content:encoded><![CDATA[<p>thank you very very much your code is beautiful for constraining </p>
<p>thanks very much again <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sean Shomes</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362338</link>
		<dc:creator>Sean Shomes</dc:creator>
		<pubDate>Fri, 13 Mar 2009 19:44:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362338</guid>
		<description>That did it! Not sure if it was an issue when the curser got outside of the target mc. Works great. Thanks again Dave, this was a huge help.</description>
		<content:encoded><![CDATA[<p>That did it! Not sure if it was an issue when the curser got outside of the target mc. Works great. Thanks again Dave, this was a huge help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362332</link>
		<dc:creator>David Stiller</dc:creator>
		<pubDate>Fri, 13 Mar 2009 19:23:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362332</guid>
		<description>Sean,

I&#039;m lost, bro.  Sorry!  When I paste this into a new FLA configured for AS3, it works for me.  Now ... that said, I did see where my suggestion could be improved.  AS3 is always turning up sticky points like this.  ;)

In AS2, the &lt;code&gt;mouseMove&lt;/code&gt; registers even when the mouse isn&#039;t over the object in question (&lt;code&gt;mc&lt;/code&gt;, in this case).  In AS3, that doesn&#039;t seem to be the case.  For that reason, instead of assigning the &lt;code&gt;MOUSE_MOVE&lt;/code&gt; handler to &lt;code&gt;mc&lt;/code&gt;, it would be better to temporarily associate it with the Stage instead &#8212; then remove it later, as shown above.  The only change is a revision to two lines, from &lt;code&gt;mc&lt;/code&gt; to &lt;code&gt;stage&lt;/code&gt;:

&lt;pre&gt;&lt;code&gt;function dragHandler(evt:MouseEvent):void {
  stage.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function dragStopHandler(evt:MouseEvent):void {
  stage.removeEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Sean,</p>
<p>I&#8217;m lost, bro.  Sorry!  When I paste this into a new FLA configured for AS3, it works for me.  Now &#8230; that said, I did see where my suggestion could be improved.  AS3 is always turning up sticky points like this.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In AS2, the <code>mouseMove</code> registers even when the mouse isn&#8217;t over the object in question (<code>mc</code>, in this case).  In AS3, that doesn&#8217;t seem to be the case.  For that reason, instead of assigning the <code>MOUSE_MOVE</code> handler to <code>mc</code>, it would be better to temporarily associate it with the Stage instead &mdash; then remove it later, as shown above.  The only change is a revision to two lines, from <code>mc</code> to <code>stage</code>:</p>
<pre><code>function dragHandler(evt:MouseEvent):void {
  stage.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function dragStopHandler(evt:MouseEvent):void {
  stage.removeEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sean Shomes</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362328</link>
		<dc:creator>Sean Shomes</dc:creator>
		<pubDate>Fri, 13 Mar 2009 19:06:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362328</guid>
		<description>Hmm...this doesn&#039;t seem to work. At first I thought it was because I was using &quot;evt.target.x&quot; and &quot;evt.target.y&quot; in my functions rather than just &quot;mc.x&quot; and &quot;mc.y&quot;, but that did not work either. Just to test everything, I even simply copied and pasted your code in a new document with a single mc, and it&#039;s doing the same thing. Not sure if you tested the code or not, but perhaps there&#039;s a syntax error? I&#039;ll keep working on it. Thanks again for all your help!</description>
		<content:encoded><![CDATA[<p>Hmm&#8230;this doesn&#8217;t seem to work. At first I thought it was because I was using &#8220;evt.target.x&#8221; and &#8220;evt.target.y&#8221; in my functions rather than just &#8220;mc.x&#8221; and &#8220;mc.y&#8221;, but that did not work either. Just to test everything, I even simply copied and pasted your code in a new document with a single mc, and it&#8217;s doing the same thing. Not sure if you tested the code or not, but perhaps there&#8217;s a syntax error? I&#8217;ll keep working on it. Thanks again for all your help!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362327</link>
		<dc:creator>David Stiller</dc:creator>
		<pubDate>Fri, 13 Mar 2009 19:03:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362327</guid>
		<description>Sean,

Heh, totally luck of the draw.  I&#039;ve been behind on my blog and on the Adobe forums for a few months.  I do answer when I can &#8212; ASAP &lt;em&gt;when I can&lt;/em&gt; &#8212; but sometimes I go for insanely long stretches!

In both AS2 and AS3, the &lt;code&gt;MovieClip&lt;/code&gt; class is a dynamic class, which means it can have properties added to it at runtime.  These &lt;code&gt;origX&lt;/code&gt; and &lt;code&gt;origY&lt;/code&gt; variables are properties in the sense that they&#039;re being assigned to an object instance (a &lt;code&gt;MovieClip&lt;/code&gt; instance) at runtime, but in practice, they&#039;re no different from timeline variables (the sort you declare with &lt;code&gt;var&lt;/code&gt; in keyframe code.  But you can only &lt;code&gt;type&lt;/code&gt; variables, not properties.  So you could do this:

&lt;pre&gt;&lt;code&gt;var mcOrigX:Number = &lt;em&gt;some number&lt;/em&gt;&lt;/code&gt;&lt;/pre&gt;

but when that variable is a property, no matter what version of ActionScript, it can only be this:

&lt;pre&gt;&lt;code&gt;&lt;em&gt;someObjectReference&lt;/em&gt;.mcOrigX = &lt;em&gt;some number&lt;/em&gt;&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Sean,</p>
<p>Heh, totally luck of the draw.  I&#8217;ve been behind on my blog and on the Adobe forums for a few months.  I do answer when I can &mdash; ASAP <em>when I can</em> &mdash; but sometimes I go for insanely long stretches!</p>
<p>In both AS2 and AS3, the <code>MovieClip</code> class is a dynamic class, which means it can have properties added to it at runtime.  These <code>origX</code> and <code>origY</code> variables are properties in the sense that they&#8217;re being assigned to an object instance (a <code>MovieClip</code> instance) at runtime, but in practice, they&#8217;re no different from timeline variables (the sort you declare with <code>var</code> in keyframe code.  But you can only <code>type</code> variables, not properties.  So you could do this:</p>
<pre><code>var mcOrigX:Number = <em>some number</em></code></pre>
<p>but when that variable is a property, no matter what version of ActionScript, it can only be this:</p>
<pre><code><em>someObjectReference</em>.mcOrigX = <em>some number</em></code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sean Shomes</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362324</link>
		<dc:creator>Sean Shomes</dc:creator>
		<pubDate>Fri, 13 Mar 2009 18:44:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362324</guid>
		<description>Wow dude. That was amazingly fast! I&#039;m going to go through it now, but I&#039;m already noticing my problem in conversion. I was thrown off by the AS2: mc.origX. I was trying to set a new variable: var mcOrigX:Number = mc.x; which I now see is incorrect. Thanks again.</description>
		<content:encoded><![CDATA[<p>Wow dude. That was amazingly fast! I&#8217;m going to go through it now, but I&#8217;m already noticing my problem in conversion. I was thrown off by the AS2: mc.origX. I was trying to set a new variable: var mcOrigX:Number = mc.x; which I now see is incorrect. Thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362316</link>
		<dc:creator>David Stiller</dc:creator>
		<pubDate>Fri, 13 Mar 2009 18:12:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362316</guid>
		<description>Sean,

Event handling is certainly more wordy in AS3, that&#039;s for sure.  In principle, though, you&#039;re doing the same thing you did before &#8212; just with different syntax.  You&#039;re still associating a function with an event, but in AS3, you use the &lt;code&gt;EventDispatcher.addEventListener()&lt;/code&gt; method, which is inherited by movie clips and plenty of other AS3 entities.  Let&#039;s step through the conversion (assuming the movie clip&#039;s instance name is still &lt;code&gt;mc&lt;/code&gt;):

&lt;pre&gt;&lt;code&gt;mc.buttonMode = true;
mc.origX = mc.x;
mc.origY = mc.y;&lt;/code&gt;&lt;/pre&gt;

The only real difference here is that you&#039;re setting the &lt;code&gt;Sprite.buttonMode&lt;/code&gt; property of this movie clip to &lt;code&gt;true&lt;/code&gt;, so that you get the finger cursor when you mouse over the clip (in AS3, movie clips inherit functionality from the &lt;code&gt;Sprite&lt;/code&gt; class).  You&#039;ll also notice that the formerly &lt;code&gt;_x&lt;/code&gt; and &lt;code&gt;_y&lt;/code&gt; properties have lost their underscores.

Here are the first two event handlers, which kick everything off:

&lt;pre&gt;&lt;code&gt;mc.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
mc.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
&lt;/code&gt;&lt;/pre&gt;

This isn&#039;t so different from the AS2 approach; it&#039;s just that instead of referencing the object (&lt;code&gt;mc&lt;/code&gt;) and then associating a function directly to one of the object&#039;s events ...

&lt;code&gt;mc.onPress = function() { ... };&lt;/code&gt;

... you&#039;re using that &lt;code&gt;addEventListener()&lt;/code&gt; method as an intermediary.  In addition, we&#039;re using named functions now, instead of function literals, as shown in the above tutorial.  The main reason we need named functions now is because of how AS3 unhooks functions when they&#039;re no longer needed.  In AS2, you could just set the event reference in question to &lt;code&gt;null&lt;/code&gt;; here, we&#039;ll be using the &lt;code&gt;removeEventListener()&lt;/code&gt; method, which needs a name by which to reference the function in question.

Bear in mind, AS2 could use named functions too.  It is, in fact, arguably a better practice to give your functions names, in case you need to reference them again later.

Here&#039;s how the AS2 version would look with a named function:

&lt;pre&gt;&lt;code&gt;// original
mc.onPress = function():Void {
  this.onMouseMove = function():Void {
    var angle:Number = Math.atan2(
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;// named
mc.onPress = dragHandler;

function dragHandler():Void {
  this.onMouseMove = moveHandler;
};

function moveHandler():Void {
    var angle:Number = Math.atan2(
    ...
&lt;/code&gt;&lt;/pre&gt;

Please note, these are arbitrary names.  You could call your function &lt;code&gt;strudelKicker()&lt;/code&gt; if you wanted to.  Note, too, that I&#039;m typing my functions with the &lt;code&gt;:Void&lt;/code&gt; suffix, which simply lets Flash know that these functions don&#039;t return any values.  This sort of post colon suffix is optional in AS1, AS2, and AS3, but it has certain benefits, including code hinting, compile-time error notification, and &#8212; in AS3 &#8212; even runtime error notification and improved RAM usage.  (Note that &lt;code&gt;:Void&lt;/code&gt; becomes &lt;code&gt;:void&lt;/code&gt; in AS3.)

So, to get back on track, you&#039;ll need the named functions in AS3 because &lt;code&gt;removeEventListener()&lt;/code&gt; needs function names.  Here are the arbitrarily named functions noted in the event listener hook-ups so far:

&lt;pre&gt;&lt;code&gt;function dragHandler(evt:MouseEvent):void {
  stage.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function dragStopHandler(evt:MouseEvent):void {
  stage.removeEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};&lt;/code&gt;&lt;/pre&gt;

&lt;strong&gt;[Note:  See additional comments between Sean and me, following this reply, for a reason to replace &lt;code&gt;mc&lt;/code&gt; with &lt;code&gt;stage&lt;/code&gt; in two places.]&lt;/strong&gt;

When the mouse is pressed (&lt;code&gt;MOUSE_DOWN&lt;/code&gt;), the Stage will get a &lt;code&gt;MOUSE_UP&lt;/code&gt; listener, because none of AS3&#039;s classes has an &lt;code&gt;onReleaseOutside&lt;/code&gt; event.  When the Stage hears a mouse release, that&#039;ll accomplish the same thing here as an &lt;code&gt;onReleaseOutside&lt;/code&gt; would have done in AS2.

Other than that, when &lt;code&gt;mc&lt;/code&gt; is pressed, it&#039;ll be assigned a &lt;code&gt;MOUSE_MOVE&lt;/code&gt; handler, just like in the AS2 version.  When the mouse is released &#8212; either over &lt;code&gt;mc&lt;/code&gt; or the Stage &#8212; that &lt;code&gt;MOUSE_MOVE&lt;/code&gt; handler will be removed.

Finally, the brains of the outfit, which is largely the same as it was before:

&lt;pre&gt;&lt;code&gt;function moveHandler(evt:MouseEvent):void {
  var angle:Number = Math.atan2(
    stage.mouseY - mc.origY,
    stage.mouseX - mc.origX
  );
  var distance:Number = Math.sqrt(
    Math.pow(stage.mouseX - mc.origX, 2) +
    Math.pow(stage.mouseY - mc.origY, 2)
  );
  if (Math.ceil(distance) &gt;= 100) {
    mc.x = mc.origX + (Math.cos(angle) * 100);
    mc.y = mc.origY + (Math.sin(angle) * 100);
  } else {
    mc.x = stage.mouseX;
    mc.y = stage.mouseY;
  }
};&lt;/code&gt;&lt;/pre&gt;

Look over it carefully, and you&#039;ll see that only minor things have changed, such as a reference to the Stage rather than &lt;code&gt;_root&lt;/code&gt;, &lt;code&gt;mouseX&lt;/code&gt; instead of &lt;code&gt;_xmouse&lt;/code&gt;, and the like.

Hope this helps!  Here it is again, all in one shot:

&lt;pre&gt;&lt;code&gt;mc.buttonMode = true;
mc.origX = mc.x;
mc.origY = mc.y;

mc.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
mc.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);

function dragHandler(evt:MouseEvent):void {
  stage.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function dragStopHandler(evt:MouseEvent):void {
  stage.removeEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function moveHandler(evt:MouseEvent):void {
  var angle:Number = Math.atan2(
    stage.mouseY - mc.origY,
    stage.mouseX - mc.origX
  );
  var distance:Number = Math.sqrt(
    Math.pow(stage.mouseX - mc.origX, 2) +
    Math.pow(stage.mouseY - mc.origY, 2)
  );
  if (Math.ceil(distance) &gt;= 100) {
    mc.x = mc.origX + (Math.cos(angle) * 100);
    mc.y = mc.origY + (Math.sin(angle) * 100);
  } else {
    mc.x = stage.mouseX;
    mc.y = stage.mouseY;
  }
};&lt;/code&gt;&lt;/pre&gt;

&lt;strong&gt;[Note:  See additional comments between Sean and me, following this reply, for a reason to replace &lt;code&gt;mc&lt;/code&gt; with &lt;code&gt;stage&lt;/code&gt; in two places.]&lt;/strong&gt;</description>
		<content:encoded><![CDATA[<p>Sean,</p>
<p>Event handling is certainly more wordy in AS3, that&#8217;s for sure.  In principle, though, you&#8217;re doing the same thing you did before &mdash; just with different syntax.  You&#8217;re still associating a function with an event, but in AS3, you use the <code>EventDispatcher.addEventListener()</code> method, which is inherited by movie clips and plenty of other AS3 entities.  Let&#8217;s step through the conversion (assuming the movie clip&#8217;s instance name is still <code>mc</code>):</p>
<pre><code>mc.buttonMode = true;
mc.origX = mc.x;
mc.origY = mc.y;</code></pre>
<p>The only real difference here is that you&#8217;re setting the <code>Sprite.buttonMode</code> property of this movie clip to <code>true</code>, so that you get the finger cursor when you mouse over the clip (in AS3, movie clips inherit functionality from the <code>Sprite</code> class).  You&#8217;ll also notice that the formerly <code>_x</code> and <code>_y</code> properties have lost their underscores.</p>
<p>Here are the first two event handlers, which kick everything off:</p>
<pre><code>mc.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
mc.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
</code></pre>
<p>This isn&#8217;t so different from the AS2 approach; it&#8217;s just that instead of referencing the object (<code>mc</code>) and then associating a function directly to one of the object&#8217;s events &#8230;</p>
<p><code>mc.onPress = function() { ... };</code></p>
<p>&#8230; you&#8217;re using that <code>addEventListener()</code> method as an intermediary.  In addition, we&#8217;re using named functions now, instead of function literals, as shown in the above tutorial.  The main reason we need named functions now is because of how AS3 unhooks functions when they&#8217;re no longer needed.  In AS2, you could just set the event reference in question to <code>null</code>; here, we&#8217;ll be using the <code>removeEventListener()</code> method, which needs a name by which to reference the function in question.</p>
<p>Bear in mind, AS2 could use named functions too.  It is, in fact, arguably a better practice to give your functions names, in case you need to reference them again later.</p>
<p>Here&#8217;s how the AS2 version would look with a named function:</p>
<pre><code>// original
mc.onPress = function():Void {
  this.onMouseMove = function():Void {
    var angle:Number = Math.atan2(
    ...
</code></pre>
<pre><code>// named
mc.onPress = dragHandler;

function dragHandler():Void {
  this.onMouseMove = moveHandler;
};

function moveHandler():Void {
    var angle:Number = Math.atan2(
    ...
</code></pre>
<p>Please note, these are arbitrary names.  You could call your function <code>strudelKicker()</code> if you wanted to.  Note, too, that I&#8217;m typing my functions with the <code>:Void</code> suffix, which simply lets Flash know that these functions don&#8217;t return any values.  This sort of post colon suffix is optional in AS1, AS2, and AS3, but it has certain benefits, including code hinting, compile-time error notification, and &mdash; in AS3 &mdash; even runtime error notification and improved RAM usage.  (Note that <code>:Void</code> becomes <code>:void</code> in AS3.)</p>
<p>So, to get back on track, you&#8217;ll need the named functions in AS3 because <code>removeEventListener()</code> needs function names.  Here are the arbitrarily named functions noted in the event listener hook-ups so far:</p>
<pre><code>function dragHandler(evt:MouseEvent):void {
  stage.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function dragStopHandler(evt:MouseEvent):void {
  stage.removeEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};</code></pre>
<p><strong>[Note:  See additional comments between Sean and me, following this reply, for a reason to replace <code>mc</code> with <code>stage</code> in two places.]</strong></p>
<p>When the mouse is pressed (<code>MOUSE_DOWN</code>), the Stage will get a <code>MOUSE_UP</code> listener, because none of AS3&#8242;s classes has an <code>onReleaseOutside</code> event.  When the Stage hears a mouse release, that&#8217;ll accomplish the same thing here as an <code>onReleaseOutside</code> would have done in AS2.</p>
<p>Other than that, when <code>mc</code> is pressed, it&#8217;ll be assigned a <code>MOUSE_MOVE</code> handler, just like in the AS2 version.  When the mouse is released &mdash; either over <code>mc</code> or the Stage &mdash; that <code>MOUSE_MOVE</code> handler will be removed.</p>
<p>Finally, the brains of the outfit, which is largely the same as it was before:</p>
<pre><code>function moveHandler(evt:MouseEvent):void {
  var angle:Number = Math.atan2(
    stage.mouseY - mc.origY,
    stage.mouseX - mc.origX
  );
  var distance:Number = Math.sqrt(
    Math.pow(stage.mouseX - mc.origX, 2) +
    Math.pow(stage.mouseY - mc.origY, 2)
  );
  if (Math.ceil(distance) >= 100) {
    mc.x = mc.origX + (Math.cos(angle) * 100);
    mc.y = mc.origY + (Math.sin(angle) * 100);
  } else {
    mc.x = stage.mouseX;
    mc.y = stage.mouseY;
  }
};</code></pre>
<p>Look over it carefully, and you&#8217;ll see that only minor things have changed, such as a reference to the Stage rather than <code>_root</code>, <code>mouseX</code> instead of <code>_xmouse</code>, and the like.</p>
<p>Hope this helps!  Here it is again, all in one shot:</p>
<pre><code>mc.buttonMode = true;
mc.origX = mc.x;
mc.origY = mc.y;

mc.addEventListener(MouseEvent.MOUSE_DOWN, dragHandler);
mc.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);

function dragHandler(evt:MouseEvent):void {
  stage.addEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function dragStopHandler(evt:MouseEvent):void {
  stage.removeEventListener(MouseEvent.MOUSE_UP, dragStopHandler);
  mc.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
};

function moveHandler(evt:MouseEvent):void {
  var angle:Number = Math.atan2(
    stage.mouseY - mc.origY,
    stage.mouseX - mc.origX
  );
  var distance:Number = Math.sqrt(
    Math.pow(stage.mouseX - mc.origX, 2) +
    Math.pow(stage.mouseY - mc.origY, 2)
  );
  if (Math.ceil(distance) >= 100) {
    mc.x = mc.origX + (Math.cos(angle) * 100);
    mc.y = mc.origY + (Math.sin(angle) * 100);
  } else {
    mc.x = stage.mouseX;
    mc.y = stage.mouseY;
  }
};</code></pre>
<p><strong>[Note:  See additional comments between Sean and me, following this reply, for a reason to replace <code>mc</code> with <code>stage</code> in two places.]</strong></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sean Shomes</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362262</link>
		<dc:creator>Sean Shomes</dc:creator>
		<pubDate>Fri, 13 Mar 2009 15:54:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-362262</guid>
		<description>This works really great! However, I&#039;m trying to convert this to AS3. Having trouble assigning the listeners within the &quot;on pressed&quot; function, now that .onPress and .onMouseMove are gone</description>
		<content:encoded><![CDATA[<p>This works really great! However, I&#8217;m trying to convert this to AS3. Having trouble assigning the listeners within the &#8220;on pressed&#8221; function, now that .onPress and .onMouseMove are gone</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Stiller</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-74309</link>
		<dc:creator>David Stiller</dc:creator>
		<pubDate>Mon, 20 Aug 2007 17:43:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-74309</guid>
		<description>amir,

Glad to hear it.  :)</description>
		<content:encoded><![CDATA[<p>amir,</p>
<p>Glad to hear it.  <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: amir</title>
		<link>http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-74161</link>
		<dc:creator>amir</dc:creator>
		<pubDate>Mon, 20 Aug 2007 04:45:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.quip.net/blog/2006/flash/actionscript-20/how-to-constrain-circle#comment-74161</guid>
		<description>wow never thought of that approach. now its more clear to me how this pie thingy works.

thanks david you&#039;re a life saver :D</description>
		<content:encoded><![CDATA[<p>wow never thought of that approach. now its more clear to me how this pie thingy works.</p>
<p>thanks david you&#8217;re a life saver <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

