<?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>FlexMan &#187; Button</title>
	<atom:link href="http://flexman.info/tag/button/feed/" rel="self" type="application/rss+xml" />
	<link>http://flexman.info</link>
	<description>Everything is Flex</description>
	<lastBuildDate>Sun, 30 Oct 2011 12:46:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Interesting Tutorial: Make a Magnetic Button in Flex 4</title>
		<link>http://flexman.info/2010/07/09/interesting-tutorial-make-a-magnetic-button-in-flex-4/</link>
		<comments>http://flexman.info/2010/07/09/interesting-tutorial-make-a-magnetic-button-in-flex-4/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 01:52:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Adobe-Flex]]></category>
		<category><![CDATA[Button]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Magnetic]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://flexman.info/?p=358</guid>
		<description><![CDATA[This interesting tutorial explains how to make a magnetic button in Flex 4 with a simple example. Here is the source code of this class which extands spark button: package com.flexblog.components { &#160; &#160; import&#160;flash.events.MouseEvent; &#160; &#160; import&#160;flash.ui.Mouse; &#160;&#160; &#160; &#160; import&#160;mx.core.FlexGlobals; &#160; &#160; import&#160;mx.events.FlexEvent; &#160; &#160; import&#160;mx.events.MoveEvent; &#160;&#160; &#160; &#160; import&#160;spark.components.Button; &#160; &#160; import&#160;spark.effects.Move; [...]]]></description>
			<content:encoded><![CDATA[<p>This interesting tutorial explains how to make a magnetic button in Flex 4 with a simple example. Here is the source code of this class which extands spark button:</p>
<div style="margin: 0px 0px 0px 15px;">
<script type="text/javascript"><!--
google_ad_client = "pub-2748932162110627";
/* 1st-FlexMan-Article-336-280-01 */
google_ad_slot = "5782465088";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<p><span id="more-358"></span></p>
<div class="hl-surround"><ol class="hl-main ln-show" title="Double click to hide line number." ondblclick = "linenumber(this)"><li class="hl-firstline"><span style="color: Blue;">package</span><span style="color: Gray;"> </span><span style="color: Blue;">com</span><span style="color: Gray;">.</span><span style="color: Blue;">flexblog</span><span style="color: Gray;">.</span><span style="color: Blue;">components</span></li>
<li><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Teal;">flash</span><span style="color: Gray;">.</span><span style="color: Blue;">events</span><span style="color: Gray;">.</span><span style="color: Blue;">MouseEvent</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Teal;">flash</span><span style="color: Gray;">.</span><span style="color: Blue;">ui</span><span style="color: Gray;">.</span><span style="color: Teal;">Mouse</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Teal;">mx</span><span style="color: Gray;">.</span><span style="color: Blue;">core</span><span style="color: Gray;">.</span><span style="color: Blue;">FlexGlobals</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Teal;">mx</span><span style="color: Gray;">.</span><span style="color: Blue;">events</span><span style="color: Gray;">.</span><span style="color: Blue;">FlexEvent</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Teal;">mx</span><span style="color: Gray;">.</span><span style="color: Blue;">events</span><span style="color: Gray;">.</span><span style="color: Blue;">MoveEvent</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Blue;">spark</span><span style="color: Gray;">.</span><span style="color: Blue;">components</span><span style="color: Gray;">.</span><span style="color: Teal;">Button</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Blue;">spark</span><span style="color: Gray;">.</span><span style="color: Blue;">effects</span><span style="color: Gray;">.</span><span style="color: Blue;">Move</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;">&nbsp;</span><span style="color: Blue;">spark</span><span style="color: Gray;">.</span><span style="color: Blue;">effects</span><span style="color: Gray;">.</span><span style="color: Blue;">easing</span><span style="color: Gray;">.</span><span style="color: Blue;">Elastic</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">MagneticButton</span><span style="color: Gray;"> </span><span style="color: Green;">extends</span><span style="color: Gray;"> </span><span style="color: Teal;">Button</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// xAncor and yAncor determine the point to which</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// the button returns when mouse cursor is too far away</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">[</span><span style="color: Blue;">Bindable</span><span style="color: Olive;">]</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">var</span><span style="color: Gray;"> </span><span style="color: Blue;">xAncor</span><span style="color: Gray;">:</span><span style="color: Teal;">Number</span><span style="color: Gray;"> = </span><span style="color: Maroon;">0</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">[</span><span style="color: Blue;">Bindable</span><span style="color: Olive;">]</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">var</span><span style="color: Gray;"> </span><span style="color: Blue;">yAncor</span><span style="color: Gray;">:</span><span style="color: Teal;">Number</span><span style="color: Gray;"> = </span><span style="color: Maroon;">0</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// Distance on x axis which determine maximum</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// distance from xAncor in one direction</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">[</span><span style="color: Blue;">Bindable</span><span style="color: Olive;">]</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">var</span><span style="color: Gray;"> </span><span style="color: Blue;">xDist</span><span style="color: Gray;">:</span><span style="color: Teal;">Number</span><span style="color: Gray;"> = </span><span style="color: Maroon;">100</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// Distance on y axis which determine maximum</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// distance from yAncor in one direction</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">[</span><span style="color: Blue;">Bindable</span><span style="color: Olive;">]</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">var</span><span style="color: Gray;"> </span><span style="color: Blue;">yDist</span><span style="color: Gray;">:</span><span style="color: Teal;">Number</span><span style="color: Gray;"> = </span><span style="color: Maroon;">50</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// Property that makes transitions between different positions</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">private</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">var</span><span style="color: Gray;"> </span><span style="color: Blue;">_move</span><span style="color: Gray;">:</span><span style="color: Blue;">Move</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">Move</span><span style="color: Olive;">(</span><span style="color: Green;">this</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">function</span><span style="color: Gray;"> </span><span style="color: Blue;">MagneticButton</span><span style="color: Olive;">()</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">super</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">/**</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Tries to add event listener for mouse move event to stage.</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * If stage is null than adds event listener for application</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * complete and than adds event listener for mouse move</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">try</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Blue;">easer</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;">&nbsp;</span><span style="color: Blue;">Elastic</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Teal;">duration</span><span style="color: Gray;"> = </span><span style="color: Maroon;">1500</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">xAncor</span><span style="color: Gray;"> = </span><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Teal;">x</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">yAncor</span><span style="color: Gray;"> = </span><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Teal;">y</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">FlexGlobals</span><span style="color: Gray;">.</span><span style="color: Blue;">topLevelApplication</span><span style="color: Gray;">.</span><span style="color: Blue;">stage</span><span style="color: Gray;">.</span><span style="color: Blue;">addEventListener</span><span style="color: Olive;">(</span><span style="color: Blue;">MouseEvent</span><span style="color: Gray;">.</span><span style="color: Blue;">MOUSE_MOVE</span><span style="color: Gray;">, </span><span style="color: Blue;">mouse_move</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">catch</span><span style="color: Olive;">(</span><span style="color: Blue;">e</span><span style="color: Gray;">:</span><span style="color: Teal;">Error</span><span style="color: Olive;">)</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">FlexGlobals</span><span style="color: Gray;">.</span><span style="color: Blue;">topLevelApplication</span><span style="color: Gray;">.</span><span style="color: Blue;">addEventListener</span><span style="color: Olive;">(</span><span style="color: Blue;">FlexEvent</span><span style="color: Gray;">.</span><span style="color: Blue;">APPLICATION_COMPLETE</span><span style="color: Gray;">, </span><span style="color: Blue;">app_complete</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">/**</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Function that is called when Main application is complete.</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * It sets easer property and duration property for _move, set ancor</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * of the button to the buttens current x and y position and as</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * mentioned above adds an event listener for mouse move</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">private</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">function</span><span style="color: Gray;"> </span><span style="color: Blue;">app_complete</span><span style="color: Olive;">(</span><span style="color: Blue;">e</span><span style="color: Gray;">:</span><span style="color: Blue;">FlexEvent</span><span style="color: Olive;">)</span><span style="color: Gray;">:</span><span style="color: Green;">void</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Blue;">easer</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;">&nbsp;</span><span style="color: Blue;">Elastic</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Teal;">duration</span><span style="color: Gray;"> = </span><span style="color: Maroon;">1500</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">xAncor</span><span style="color: Gray;"> = </span><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Teal;">x</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">yAncor</span><span style="color: Gray;"> = </span><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Teal;">y</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">stage</span><span style="color: Gray;">.</span><span style="color: Blue;">addEventListener</span><span style="color: Olive;">(</span><span style="color: Blue;">MouseEvent</span><span style="color: Gray;">.</span><span style="color: Blue;">MOUSE_MOVE</span><span style="color: Gray;">, </span><span style="color: Blue;">mouse_move</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">/**</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Sets the ancor of the button and also move it to that position.</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">function</span><span style="color: Gray;"> </span><span style="color: Blue;">setAncor</span><span style="color: Olive;">(</span><span style="color: Teal;">_x</span><span style="color: Gray;">:</span><span style="color: Teal;">Number</span><span style="color: Gray;">,</span><span style="color: Teal;">_y</span><span style="color: Gray;">:</span><span style="color: Teal;">Number</span><span style="color: Olive;">)</span><span style="color: Gray;">:</span><span style="color: Green;">void</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">xAncor</span><span style="color: Gray;"> = </span><span style="color: Teal;">_x</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">yAncor</span><span style="color: Gray;"> = </span><span style="color: Teal;">_y</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Blue;">xTo</span><span style="color: Gray;"> = </span><span style="color: Teal;">_x</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Blue;">yTo</span><span style="color: Gray;"> = </span><span style="color: Teal;">_y</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Teal;">play</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;&nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">/**</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; *&nbsp; Function that actually calculates and moves the button to the right position.</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">private</span><span style="color: Gray;">&nbsp;</span><span style="color: Green;">function</span><span style="color: Gray;"> </span><span style="color: Blue;">mouse_move</span><span style="color: Olive;">(</span><span style="color: Blue;">e</span><span style="color: Gray;">:</span><span style="color: Blue;">MouseEvent</span><span style="color: Olive;">)</span><span style="color: Gray;">:</span><span style="color: Green;">void</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// Some basic math logic to calculate if mouse cursor</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// is close enough to move the button to that location</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">if</span><span style="color: Gray;">&nbsp;</span><span style="color: Olive;">(</span><span style="color: Gray;"> </span><span style="color: Teal;">Math</span><span style="color: Gray;">.</span><span style="color: Teal;">abs</span><span style="color: Olive;">((</span><span style="color: Blue;">xAncor</span><span style="color: Gray;"> + </span><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Teal;">width</span><span style="color: #8b0000;">/</span><span style="color: Red;">2) - stage.mouseX) &lt; 2*xDist &amp;&amp;</span></li>
<li><span style="color: Red;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Math.abs((yAncor + this.height</span><span style="color: #8b0000;">/</span><span style="color: Maroon;">2</span><span style="color: Olive;">)</span><span style="color: Gray;"> - </span><span style="color: Blue;">stage</span><span style="color: Gray;">.</span><span style="color: Blue;">mouseY</span><span style="color: Olive;">)</span><span style="color: Gray;"> &lt; </span><span style="color: Maroon;">2</span><span style="color: Gray;">*</span><span style="color: Blue;">yDist</span><span style="color: Gray;">&nbsp;</span><span style="color: Olive;">)</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Blue;">xTo</span><span style="color: Gray;"> = </span><span style="color: Blue;">stage</span><span style="color: Gray;">.</span><span style="color: Blue;">mouseX</span><span style="color: Gray;"> - </span><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Teal;">width</span><span style="color: #8b0000;">/</span><span style="color: Red;">2;</span></li>
<li><span style="color: Red;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _move.yTo = stage.mouseY - this.height</span><span style="color: #8b0000;">/</span><span style="color: Maroon;">2</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">else</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">{</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Blue;">xTo</span><span style="color: Gray;"> = </span><span style="color: Blue;">xAncor</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Blue;">yTo</span><span style="color: Gray;"> = </span><span style="color: Blue;">yAncor</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">_move</span><span style="color: Gray;">.</span><span style="color: Teal;">play</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span></li>
<li><span style="color: Olive;">}</span></li></ol></div>
<p>This component can be used just like we would any other component.</p>
<div class="hl-surround"><ol class="hl-main ln-show" title="Double click to hide line number." ondblclick = "linenumber(this)"><li class="hl-firstline"><span style="color: Olive;">&lt;?</span><span style="color: Green;">xml</span><span style="color: Gray;"> </span><span style="color: #00008b;">version</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">1.0</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">encoding</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">utf-8</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">?&gt;</span></li>
<li><span style="color: Olive;">&lt;</span><span style="color: Green;">s:Application</span><span style="color: Gray;">&nbsp;</span><span style="color: #00008b;">xmlns:fx</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">http://ns.adobe.com/mxml/2009</span><span style="color: #8b0000;">&quot;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #00008b;">xmlns:s</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">library://ns.adobe.com/flex/spark</span><span style="color: #8b0000;">&quot;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #00008b;">xmlns:mx</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">library://ns.adobe.com/flex/mx</span><span style="color: #8b0000;">&quot;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #00008b;">xmlns:components</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">com.flexblog.components.*</span><span style="color: #8b0000;">&quot;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #00008b;">backgroundColor</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">#B9B9B9</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;">&nbsp;</span><span style="color: #00008b;">width</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">500</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">height</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">300</span><span style="color: #8b0000;">&quot;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #00008b;">viewSourceURL</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">srcview/index.html</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">&gt;</span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">&lt;</span><span style="color: Green;">components:MagneticButton</span><span style="color: Gray;">&nbsp;</span><span style="color: #00008b;">label</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">I am magnetic button</span><span style="color: #8b0000;">&quot;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #00008b;">x</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">100</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;">&nbsp;</span><span style="color: #00008b;">y</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">50</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">xDist</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">50</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">yDist</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">30</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">/&gt;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">&lt;</span><span style="color: Green;">components:MagneticButton</span><span style="color: Gray;">&nbsp;</span><span style="color: #00008b;">label</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">I am also magnetic button</span><span style="color: #8b0000;">&quot;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #00008b;">x</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">300</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;">&nbsp;</span><span style="color: #00008b;">y</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">50</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">xDist</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">50</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">yDist</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">30</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: Olive;">/&gt;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">&lt;</span><span style="color: Green;">s:Button</span><span style="color: Gray;">&nbsp;</span><span style="color: #00008b;">label</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">I am normal button</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">x</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">150</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> </span><span style="color: #00008b;">y</span><span style="color: Gray;">=</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">150</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">/&gt;</span></li>
<li><span style="color: Gray;">&nbsp;&nbsp; </span></li>
<li><span style="color: Olive;">&lt;/</span><span style="color: Green;">s:Application</span><span style="color: Olive;">&gt;</span></li></ol></div>
<p>More details can be found <a href="http://www.flex-blog.com/how-to-make-magnetic-button/">here</a>.</p>
<h3>Related Posts</h3><ul class="related_post"><li><a href="http://flexman.info/2010/07/14/flex-4-books-effortless-flex-4-development-available/" title="Flex 4 Books &#8220;Effortless Flex 4 Development&#8221; Available">Flex 4 Books &#8220;Effortless Flex 4 Development&#8221; Available</a></li><li><a href="http://flexman.info/2010/07/12/how-to-install-adobe-flex-builder-linuxalpha-in-ubuntu/" title="How To Install Adobe Flex Builder Linux(alpha) In Ubuntu">How To Install Adobe Flex Builder Linux(alpha) In Ubuntu</a></li><li><a href="http://flexman.info/2009/09/11/14-basic-tutorials-for-away3d/" title="14+ Basic Tutorials For Away3D">14+ Basic Tutorials For Away3D</a></li><li><a href="http://flexman.info/2009/03/22/beginning-tutorial-for-actionscript-learner/" title="Beginning Tutorial For ActionScript Learner">Beginning Tutorial For ActionScript Learner</a></li><li><a href="http://flexman.info/2008/11/06/a-video-tutorial-about-php-working-with-flex/" title="A Video Tutorial About PHP Working With Flex">A Video Tutorial About PHP Working With Flex</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://flexman.info/2010/07/09/interesting-tutorial-make-a-magnetic-button-in-flex-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to create a button and assign a value to a text box within it in actionscript?</title>
		<link>http://flexman.info/2008/04/21/how-to-create-a-button-and-assign-a-value-to-a-text-box-within-it-in-actionscript/</link>
		<comments>http://flexman.info/2008/04/21/how-to-create-a-button-and-assign-a-value-to-a-text-box-within-it-in-actionscript/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 10:17:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Adobe-Flash]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Button]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://flexman.info/?p=4</guid>
		<description><![CDATA[Can we create an instance of a button from the library dynamically? If you have already a button Symbol in your Library with the linkageName buttonSymbol just attach the button on Stage and asign values to it via the button&#8217;s name. The full code: this.attachMovie(&#34;buttonSymbol&#34;, &#34;btn&#34;, 1); this.btn.txtField.text = &#34;a Text&#34;; The above is not [...]]]></description>
			<content:encoded><![CDATA[<p>Can we create an instance of a button from the library dynamically?</p>
<p>If you have already a button Symbol in your Library with the linkageName buttonSymbol just attach the button on Stage and asign values to it via the button&#8217;s name.</p>
<p>The full code:</p>
<div class="hl-surround"><ol class="hl-main ln-show" title="Double click to hide line number." ondblclick = "linenumber(this)"><li class="hl-firstline"><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Teal;">attachMovie</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">buttonSymbol</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;">, </span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">btn</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;">, </span><span style="color: Maroon;">1</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Green;">this</span><span style="color: Gray;">.</span><span style="color: Blue;">btn</span><span style="color: Gray;">.</span><span style="color: Blue;">txtField</span><span style="color: Gray;">.</span><span style="color: Teal;">text</span><span style="color: Gray;"> = </span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">a Text</span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;">;</span></li></ol></div>
<div style="margin: 0px 0px 0px 15px;">
<script type="text/javascript"><!--
google_ad_client = "pub-2748932162110627";
/* 1st-FlexMan-Article-336-280-01 */
google_ad_slot = "5782465088";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<p><span id="more-4"></span><br />
The above is not a good way to resolve it.</p>
<p>The problem:</p>
<p>Creating buttons dynamically and updating the button text per button.</p>
<p><span style="color: darkred;"><strong>Failed solution 1:</strong></span> multiple buttons with attachMove(). While the buttons can be duped and thus have unique instance names the dynamic text fields in them do not &#8211; though we have seemingly unique buttons all the text fields within them are considered the same and updating one will update them all with the same thing. Essentialy this does not work for multiple attached buttons:</p>
<p>dynamicButtonInstanceName.dynamicTextFieldVarName = arg;</p>
<p><span style="color: darkred;"><strong>Failed solution 2:</strong></span> dynamically create text fields on top of the dynamically created buttons. This fails because it effectively &#8220;turns off&#8221; the button&#8217;s behaviour &#8211; the text when appearing over the button kills the button &#8211; no rollover, no events &#8211; nothing. Must be a bug/limitation of some kind.</p>
<p><strong><span style="color: seagreen;">Succesful solution </span></strong>- encapsulate the button &amp; text in a move clip. Dupe the clip as many times as needed. The movie clip consists of a blank button (no text) on one layer and a dynamic text field on another layer. use AttachMovie to dupe this movie clip and you will be able to succesfully update the contents of the text field and the button will still work. The bonus is that you only need to position the encapsulating movie clip and not the button and text individualy.</p>
<p>dynamicMovieClipInstanceName.dynamicTextFieldVarNa  me = arg;</p>
<h3>Related Posts</h3><ul class="related_post"><li><a href="http://flexman.info/2009/05/29/an-open-source-configuration-tool-for-actionscript-2-3-sprouts/" title="An Open Source Configuration Tool For ActionScript 2/3 &#8211; Sprouts">An Open Source Configuration Tool For ActionScript 2/3 &#8211; Sprouts</a></li><li><a href="http://flexman.info/2010/01/16/a-framework-help-you-to-create-flashflex-website-with-all-of-the-benefits-of-html-website-swfseo/" title="A Framework Help You to Create Flash/Flex Website With All of the Benefits of HTML Website &#8211; SWFSEO">A Framework Help You to Create Flash/Flex Website With All of the Benefits of HTML Website &#8211; SWFSEO</a></li><li><a href="http://flexman.info/2009/11/30/an-intuitive-user-friendly-3d-engine-in-actionscript-2-3-sandy/" title="An Intuitive &amp; User Friendly 3D Engine in ActionScript 2/3 &#8211; Sandy">An Intuitive &amp; User Friendly 3D Engine in ActionScript 2/3 &#8211; Sandy</a></li><li><a href="http://flexman.info/2009/11/21/a-mx-framework-for-flash-flex-air-razor-component-framework/" title="A MX Framework for Flash/Flex &amp; AIR &#8211; Razor Component Framework">A MX Framework for Flash/Flex &amp; AIR &#8211; Razor Component Framework</a></li><li><a href="http://flexman.info/2009/10/30/application-framework-for-flex-and-flash-in-as3-parsley/" title="Application Framework For Flex and Flash in AS3 &#8211; Parsley">Application Framework For Flex and Flash in AS3 &#8211; Parsley</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://flexman.info/2008/04/21/how-to-create-a-button-and-assign-a-value-to-a-text-box-within-it-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 6/23 queries in 0.079 seconds using disk
Object Caching 254/471 objects using disk

Served from: flexman.info @ 2012-02-11 22:26:34 -->
