<?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>Stroep &#187; Flex</title> <atom:link href="http://blog.stroep.nl/tag/flex/feed/" rel="self" type="application/rss+xml" /><link>http://blog.stroep.nl</link> <description>Just a collection of random works - Mark Knol</description> <lastBuildDate>Tue, 07 Sep 2010 07:08:59 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Flex tutorial &#8211; Build a calculator</title><link>http://blog.stroep.nl/2008/08/flex-tutorial-build-a-calculator/</link> <comments>http://blog.stroep.nl/2008/08/flex-tutorial-build-a-calculator/#comments</comments> <pubDate>Sun, 24 Aug 2008 23:10:21 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Flex]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[flashdevelop]]></category> <category><![CDATA[Javascript]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=35</guid> <description><![CDATA[With <a href="http://www.adobe.com/products/flex/">Flex</a> you can create applications. So, why not start creating a very simple application, like a calculator?How does a calculator work, in my opinion? Simple: it just evaluates a string. But how can we evaluate a simple string like “1+1″ in AS3? I think we can’t, because eval() isn’t supported in actionscript 3. So I search on google, and found some solutions for it. But nothing really satisfied me. I don’t want to use PHP or another serverside script for evaluating strings, do you? So why not using a client side script, like javascript?I want to share how I did this to you, and also the process of creating a calculator.]]></description> <content:encoded><![CDATA[<p>With <a href="http://www.adobe.com/products/flex/">Flex</a> you can create applications. So, why not start creating a very simple application, like a calculator?</p><p>How does a calculator work, in my opinion? Simple: it just evaluates a string. But how can we evaluate a simple string like “1+1″ in AS3? I think we can’t, because eval() isn’t supported in actionscript 3. So I search on google, and found some solutions for it. But nothing really satisfied me. I don’t want to use PHP or another serverside script for evaluating strings, do you? So why not using a client side script, like javascript?</p><p>I want to share how I did this to you, and also the process of creating a calculator.</p><p><strong>Setup a new project</strong><br /> I use <a href="http://www.flashdevelop.org/">FlashDevelop</a> for Flex applications. Create a new project ( Menu > Project > Flex 3 project).  Open the main.mxml file in the src-folder. Make shure it is set to &#8216;always compile&#8217; (the green icon). Let&#8217;s add some data:</p><div class="bbCSH" style="font-family: monospace;"> &lt;?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?&gt;<br /> &lt;mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span>&gt;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; &lt;mx:Panel title=<span style="color: #ff0000;">&quot;Calculator&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;30&quot;</span> paddingBottom=<span style="color: #ff0000;">&quot;30&quot;</span> paddingRight=<span style="color: #ff0000;">&quot;30&quot;</span> paddingTop=<span style="color: #ff0000;">&quot;30&quot;</span> &gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Text</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;input&quot;</span>/&gt; &lt;mx:TextInput id=<span style="color: #ff0000;">&quot;input&quot;</span> /&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;output.text = input.text&quot;</span> label=<span style="color: #ff0000;">&quot;calculate&quot;</span> &nbsp;/&gt;&nbsp; &nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Text</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;output&quot;</span>/&gt; &lt;mx:TextInput id=<span style="color: #ff0000;">&quot;output&quot;</span> /&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &lt;/mx:Panel&gt;&nbsp;<br /> &nbsp; &nbsp; <br /> &lt;/mx:Application&gt;<br /> &nbsp;</div><p>What did we just created? Check out the code and press ctrl-enter to run the application. We have a container panel, an input field and an output field. Press the button and the text from the input will be shown in the output textfield. Great, isn&#8217;t it?</p><p>To be real; if you never ever used Flex, this ÍS great. How much time did we spend to create a simple interface like this? We just used 5 tags, and it already is an application.</p><p><img src="/wp-content/uploads/calculator01.jpg" alt="Basic layout of a flex calculator" /></p><p><strong>Create a html document</strong><br /> We need html, because we need to communicate between the swf and javascript.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span><br /> <span style="color: #009900;"><span style="color: #000000; ">&lt;html&gt;</span></span><br /> <span style="color: #009900;"><span style="color: #000000; ">&lt;head&gt;</span></span><br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; ">&lt;title&gt;</span></span>My calculator<span style="color: #009900;"><span style="color: #000000; ">&lt;/title&gt;</span></span><br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; ">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> /<span style="color: #000000; ">&gt;</span></span><br /> <span style="color: #009900;"><span style="color: #000000; ">&lt;/head&gt;</span></span><br /> <span style="color: #009900;"><span style="color: #000000; ">&lt;body&gt;</span></span></p><p><span style="color: #009900;"><span style="color: #000000; ">&lt;object</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;application/x-shockwave-flash&quot;</span> <span style="color: #000066;">data</span>=<span style="color: #ff0000;">&quot;flexproject.swf&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;800&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;600&quot;</span><span style="color: #000000; ">&gt;</span></span><br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; ">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;movie&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;flexproject.swf&quot;</span> /<span style="color: #000000; ">&gt;</span></span><br /> <span style="color: #009900;"><span style="color: #000000; ">&lt;/object&gt;</span></span> &nbsp; &nbsp; &nbsp;<br /> &nbsp; &nbsp;<br /> <span style="color: #009900;"><span style="color: #000000; ">&lt;/body&gt;</span></span><br /> <span style="color: #009900;"><span style="color: #000000; ">&lt;/html&gt;</span></span></div><p>No, nothing special, just another html file. It&#8217;s better to use <a href="http://code.google.com/p/swfobject/">swfObject</a> for embedding flashfiles, but that&#8217;s not what this tutorial is about. Save the html document in the same directory as the .swf file (in my case: in the bin folder)<br/><script type="text/javascript">google_ad_client = "pub-8026631169002810";
/* 468x15, gemaakt 30-6-08 */
google_ad_slot = "6262097246";
google_ad_width = 430;
google_ad_height = 15;</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead show_ads.js"></script><br/><br/><strong>Let&#8217;s calculate</strong><br /> We need our flex application to calculate! Let&#8217;s add some script (actionscript) to the application. Put this somewhere between the mx:Application tag:</p><div class="bbCSH" style="font-family: monospace;"> &nbsp; &nbsp; &lt;mx:Script&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">eval</span><span style="color: #66cc66;">&#40;</span> str:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> ExternalInterface.<span style="color: #0066CC;">call</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;eval&quot;</span>, str <span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>;<br /> &nbsp; &nbsp; &lt;/mx:Script&gt;</div><p>With externalinterface we can communicate between flash and javascript. We have just created an internal function named &#8216;eval&#8217;. This is going to be the function for the calculator. This function uses a string. Externalinterface calls the javascript function &#8220;eval&#8221; with the parameter from the function and returns that string. This is the magical trick in this tutorial, so make shure you get this point <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> You have created an actionscript function called eval, that uses the eval function from javascript. I think this is a really global function, so you can use it in other AS3 projects too.</p><p>Alright, let&#8217;s edit the calculate-button like this:<div class="bbCSH" style="font-family: monospace;">&lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;output.text = eval( input.text )&quot;</span> label=<span style="color: #ff0000;">&quot;calculate&quot;</span> &nbsp;/&gt;</div><p>Now build the project and open the html document we have created earlier. Note: from now on just build your project en hit refresh on your browser. You can&#8217;t calculate within the FlashPlayer, because javascript calculates for us, remember?</p><p><em>Now we have a very simple calculator. </em></p><p>Let&#8217;s make it better. We don&#8217;t need two textfields (input / output), but we need one uneditable field. I choose to use a mx:Label, please don&#8217;t ask me why. We also need buttons, just like the windows calculator. The button need to add text to our label-field. For that, let&#8217;s add a new AS3-function to the mx:Script tag:<div class="bbCSH" style="font-family: monospace;"><span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> addText<span style="color: #66cc66;">&#40;</span> str:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.<span style="color: #0066CC;">text</span> += str;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div><p>This is straight forward. When we call the function, the input field adds text that is passed by the function. I used a function for this, because we are going to use it more than 1 time. You could use this inline in the buttons, but we need a lot of buttons. Maybe later we would edit this, so then we only have to edit the function, instead of copy/paste this multiple times.<br /> Make a button to test it. Add somewhere a button like this:<div class="bbCSH" style="font-family: monospace;">&lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8216;add me&#8217; )&quot;</span> label=<span style="color: #ff0000;">&quot;click me&quot;</span> /&gt;</div><p>Test the movie. It works! You can delete the button.</p><p>Now, let&#8217;s add the other buttons. I used a mx:Panel with an absolute layout, so we can use x and y positions. I changed the layout a bit, and created something this: (this also is the the full code)</p><div class="bbCSH" style="font-family: monospace;"> &lt;?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?&gt;<br /> &lt;mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> themeColor=<span style="color: #ff0000;">&quot;#ff0000&quot;</span> <span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#333333&quot;</span> barColor=<span style="color: #ff0000;">&quot;#ffcc00&quot;</span>&gt;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; &lt;mx:Panel title=<span style="color: #ff0000;">&quot;Calculator&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;30&quot;</span> paddingBottom=<span style="color: #ff0000;">&quot;30&quot;</span> paddingRight=<span style="color: #ff0000;">&quot;30&quot;</span> paddingTop=<span style="color: #ff0000;">&quot;30&quot;</span> &gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:Label id=<span style="color: #ff0000;">&quot;input&quot;</span> fontSize=<span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:Panel borderStyle=<span style="color: #ff0000;">&quot;none&quot;</span> label=<span style="color: #ff0000;">&quot;Calculator&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> &nbsp;layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> &gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;1&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;1&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;0&quot;</span> /&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;2&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;2&quot;</span> x=<span style="color: #ff0000;">&quot;50&quot;</span> y=<span style="color: #ff0000;">&quot;0&quot;</span> &nbsp; /&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;3&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;3&quot;</span> x=<span style="color: #ff0000;">&quot;100&quot;</span> y=<span style="color: #ff0000;">&quot;0&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;4&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;4&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;30&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;5&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;5&quot;</span> x=<span style="color: #ff0000;">&quot;50&quot;</span> y=<span style="color: #ff0000;">&quot;30&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;6&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;6&quot;</span> x=<span style="color: #ff0000;">&quot;100&quot;</span> y=<span style="color: #ff0000;">&quot;30&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;7&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;7&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;60&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;8&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;8&quot;</span> x=<span style="color: #ff0000;">&quot;50&quot;</span> y=<span style="color: #ff0000;">&quot;60&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;9&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;9&quot;</span> x=<span style="color: #ff0000;">&quot;100&quot;</span> y=<span style="color: #ff0000;">&quot;60&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8217;0&#8242; )&quot;</span> label=<span style="color: #ff0000;">&quot;0&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;90&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8216;+&#8217; )&quot;</span> label=<span style="color: #ff0000;">&quot;+&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;120&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8216;-&#8217; )&quot;</span> label=<span style="color: #ff0000;">&quot;-&quot;</span> x=<span style="color: #ff0000;">&quot;50&quot;</span> y=<span style="color: #ff0000;">&quot;120&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8216;*&#8217; )&quot;</span> label=<span style="color: #ff0000;">&quot;x&quot;</span> x=<span style="color: #ff0000;">&quot;100&quot;</span> y=<span style="color: #ff0000;">&quot;120&quot;</span> &nbsp;/&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;addText( &#8216;/&#8217; )&quot;</span> label=<span style="color: #ff0000;">&quot;/&quot;</span> x=<span style="color: #ff0000;">&quot;150&quot;</span> y=<span style="color: #ff0000;">&quot;120&quot;</span> &nbsp;/&gt;&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;input.text = &#8221;&quot;</span> label=<span style="color: #ff0000;">&quot;CE&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;150&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;190&quot;</span> &nbsp;/&gt;&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;input.text = eval( input.text )&quot;</span> label=<span style="color: #ff0000;">&quot;=&quot;</span> &nbsp;<span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;110&quot;</span> x=<span style="color: #ff0000;">&quot;150&quot;</span> y=<span style="color: #ff0000;">&quot;0&quot;</span> /&gt;&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &lt;/mx:Panel&gt;&nbsp;&nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &lt;/mx:Panel&gt;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; &lt;mx:Script&gt;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">eval</span><span style="color: #66cc66;">&#40;</span> str:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> ExternalInterface.<span style="color: #006600;">available</span> <span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> ExternalInterface.<span style="color: #0066CC;">call</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;eval&quot;</span>, str <span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Unavailable&quot;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&nbsp; &nbsp;&nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> addText<span style="color: #66cc66;">&#40;</span> str:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.<span style="color: #0066CC;">text</span> += str;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &lt;/mx:Script&gt;<br /> &nbsp; &nbsp; <br /> &lt;/mx:Application&gt;<br /> &nbsp;</div><p>What are you waiting for? Run the application <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /></p><p><img src="/wp-content/uploads/calculator02.jpg" alt="Layout of your flex calculator" /></p><p>Well, that looks different! Take a look at the code. We have created buttons like 0-9, and some math signs. This buttons don&#8217;t calculate, they just add text. We have changed the &#8216;calculate&#8217;-button&#8217; to an &#8216;=&#8217; sign, and we have a clear button.</p><p>I think this is a start of using Flex and building a calculator. I’m learning Flex too, so correct me if i&#8217;m doing something wrong.</p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2008/08/flex-tutorial-build-a-calculator/feed/</wfw:commentRss> <slash:comments>9</slash:comments> </item> </channel> </rss>