<?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; Javascript</title> <atom:link href="http://blog.stroep.nl/tag/javascript/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, 10 Jan 2012 20:09:53 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Javascript for AS3 developers</title><link>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/</link> <comments>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/#comments</comments> <pubDate>Tue, 25 Jan 2011 23:46:17 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[html5]]></category> <category><![CDATA[oop]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1146</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/netscape_logo.jpg" alt="" title="Javascript for AS3 developers" width="100" height="100" class="alignleft size-full wp-image-1158" />This article is some training for your JavaScript skills. I love Actionscript , and I am also starting to like JavaScript. HTML5 is an hot topic today, and it seems using JavaScript is an important part of it, so why not learn more of it?]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/netscape_logo.jpg" alt="" title="Javascript for AS3 developers" width="100" height="100" class="alignleft size-full wp-image-1158" />This article is some training for your JavaScript skills. I love Actionscript , and I am also starting to like JavaScript. However, coding in JavaScript feels like Actionscript 1.0. It&#8217;s a pity there aren&#8217;t decent editors, especially if you want to write object-oriented-code, which <em>is</em> possible with JavaScript . At the moment I think VisualStudio is the best JavaScript editor, but I really wish there was a tool for JavaScript like FlashDevelop (a clean editor for Actionscript projects) with great intellisense, code completion and snippets etc. But we should not focus on tools, lets focus on the language and get the most out of it. HTML5 is an hot topic today, and it seems using JavaScript is an important part of it, so why not learn more of it? I have seen some <a href="http://mrdoob.com/">stunning HTML5 examples</a> over the last year. As a developer it is very interesting to see how others code JavaScript. By the way; Personally I find it very funny to see how JavaScript has changed it&#8217;s &#8216;image&#8217;. Some of us remember the good days in Netscape. JavaScript was .. *kuch* not fast and associated with ugly rainbow mouse pointers and aggressive popup screens. Now it is associated with HTML5, which is supposed to be the future and it will be bright <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> I refuse to put my opinion about this topic here.<br /> I think the &#8216;image&#8217; of JS has slowly changed after great frameworks like jQuery have reached the developers. jQuery is awesome, I advice to use jQuery as default in all sites/apps.</p><p>Anyway, there are (ofcourse) some techniques to code like Actionscript in JavaScript. These snippets have helped me to understand object oriented JavaScript. For too long I did not even tried to learn what was possible with JavaScript because I thought it was a very limited language. Maybe some information is outdated; I am still learning, so please share your feedback.</p><h2>Object oriented javascript</h2><p>In javascript it is possible to create classes, public and private vars, getters/setters and there are even constructors. A function could be seen as a class. This is a bit strange at first, but if you use it for a while you will understand. Now lets create our own class right now.</p><p>Lets port some stuff of <a href="http://code.google.com/p/stroep/source/browse/trunk/flashsources/utils%20classes/nl/stroep/utils/Color.as">this Color Class</a> to javascript. It has private vars and public functions.</p><h2>Declare classes</h2><p>Defining classes is as easy as defining functions, because it is almost the same. After creating an function, you could already make some instances of it. The example below creates a <em>Color</em> class with a public variable called <em>value</em>. After that we create 2 instances of the <em>Color</em> class with some other values.</p><p>Note: I use the same code conventions as Actionscript: Classes should be <em>UpperCased</em>, functions/vars should be <em>lowerCased </em>and constants should be <em>FULL_CAPS</em>. Oh and since javascript looks like as1.0, I sometimes use an underscore before private vars <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /></p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {<br /> &nbsp; &nbsp; this.value = value || 0;<br /> &nbsp; &nbsp; alert(&quot;Color created with value: &quot; + value)<br /> }</p><p>var myRedColor = new Color(0xFF0000); // create instance of Color &nbsp;(a red one)<br /> var myOrangeColor = new Color(0xFFCC00); // create instance of Color &nbsp;(an orange one)<br /> alert( myRedColor.value );<br /> alert( myOrangeColor.value );<br /> &nbsp;</div><h2>Public / Private</h2><p>In actionscript it is very useful to use <a href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">encapsulation</a>, which is a mechanism for restricting access to other objects. The previous+following example should work in all browsers. The difference between public and private vars/functions in javascript can be declared like this:</p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {<br /> &nbsp; &nbsp; // public variable<br /> &nbsp; &nbsp; this.value = value || 0xFFFFFF; // set default value to 0xFFFFFF for parameter if it isn&#8217;t defined</p><p>&nbsp; &nbsp; // private variable<br /> &nbsp; &nbsp; var _name = &quot;test&quot;;</p><p>&nbsp; &nbsp;// public function<br /> &nbsp; &nbsp;this.getRandomColor = function( )<br /> &nbsp; {<br /> &nbsp; &nbsp; &nbsp;return Math.random() * 0xFFFFFF;<br /> &nbsp; }</p><p>&nbsp; // private function<br /> &nbsp; function getNiceColor()<br /> &nbsp; {<br /> &nbsp; &nbsp; &nbsp;return 0xffcc00;<br /> &nbsp; }<br /> }</p><p>// create instance of Color<br /> var color = new Color(0xFF0000);<br /> alert( color.value ); // returns red color<br /> alert( color.getRandomColor() ); // returns random color</p><p>// not possible :<br /> alert( color.getNiceColor() ); error in console; property does not exist, because function is private.<br /> alert( color._name ); // error in console; property does not exist, because variable is private.<br /> &nbsp;</div><p>I think with this principles (maybe in combination with namespaces, see below) you can create clean javascript code.</p><h2>Packages &gt; Namespaces</h2><p>Now before using classes in javascript I had some conflicts with function and variable names because there were unwanted duplicates. I have written a little helper tool to use namespaces. It helps to prevent those conflicts. Now it looks more like Actionscript 3, only there is no such thing like &#8216;imports&#8217;. This snippet works in all browsers.</p><div class="bbCSH" style="font-family: monospace;"> /// create namespace like &#8216;com.yourcompany.projectname&#8217;<br /> function Namespace(namespace)<br /> {<br /> &nbsp; &nbsp; var parts = namespace.split(&quot;.&quot;);<br /> &nbsp; &nbsp; var root = window;<br /> &nbsp; &nbsp; for(var i = 0; i &lt; parts.length; i++)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; if(typeof root[parts[i]] == &quot;undefined&quot;) <br /> &nbsp; &nbsp; &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root[parts[i]] = {};<br /> &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; root = root[parts[i]];<br /> &nbsp; &nbsp; }<br /> }</p><p>// creating my own namespace here<br /> Namespace(&quot;nl.stroep.utils&quot;);<br /> nl.stroep.utils.Color = function(value) // create class inside package<br /> {<br /> &nbsp; this.value = value || 0xFFFFFF;<br /> }</p><p>var myRedColor = &nbsp;new nl.stroep.utils.Color(0xff0000);<br /> alert(myRedColor.value);<br /> &nbsp;</div><h2>Getters and Setters</h2><p>Now it is possible to use getters and setters. NOTE (!): Ofcourse this method does not work in IE, so this is pointless but also a bit cool to see how far we could go. Anyway there are 2 ways to define them. Read more about it <a href="http://ejohn.org/blog/javascript-getters-and-setters/">here</a>. I think the <em>__defineGetter__</em> way is better; it looks ugly but you still have access to private objects.</p><p>Take a look at this getter/setter declaration example. It&#8217;s not as clean as AS3 getters/setters but it works like a charm in my browser.</p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {</p><p>&nbsp; &nbsp;/* public getter */<br /> &nbsp; &nbsp;this.__defineGetter__(&quot;value&quot;, function()<br /> &nbsp; &nbsp;{<br /> &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;getter called; current value: &quot; +value);<br /> &nbsp; &nbsp; &nbsp; &nbsp; return value;<br /> &nbsp; &nbsp; });<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp;/* public setter */<br /> &nbsp; &nbsp;this.__defineSetter__(&quot;value&quot;, function(val)<br /> &nbsp; &nbsp;{<br /> &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;setter called; new value: &quot; +val);<br /> &nbsp; &nbsp; &nbsp; &nbsp; value = val;<br /> &nbsp; &nbsp; });<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;// public variable. For a strange reason I have to put this below the get/set definition.<br /> &nbsp; &nbsp;this.value = value || 0xFFFFFF;<br /> }</p><p>// create instance of Color<br /> var color = new Color(0xFF0000);<br /> color.value; // getter<br /> color.value = 0xff0000; // setter<br /> &nbsp;</div><h2>Constants</h2><p>Javascript has a <em>const</em> too, you could use it instead of <em>var</em>. Ofcourse this is not implemented in IE, so it is also pretty pointless to use.</p><h2>Mix them all</h2><p>Now lets use namespaces, getters/setters and private/public variables and functions together.<br /> This is a simplified ported version of <a href="http://code.google.com/p/stroep/source/browse/trunk/flashsources/utils%20classes/nl/stroep/utils/Color.as">this Color Class</a>.<br /> With this class you can modify the red,green and blue channels of a color individually. In the original class it is possible to lighten/darken the color too, but for this post I left these functions, because this only illustrates the possibilities of nice OO javascript.</p><div class="bbCSH" style="font-family: monospace;">Namespace(&quot;nl.stroep.utils&quot;);<br /> nl.stroep.utils.Color = function( color )<br /> {&nbsp; &nbsp;<br /> &nbsp; &nbsp; /* PUBLIC FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.grayscale = function( val )<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; val = val || 0;<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &lt; 0){ val = 0 }<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &gt; 255) { val = 255 }<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; return (val &lt;&lt; 16) | (val &lt;&lt; 8 ) | val;<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PRIVATE FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; function limit( val, lowerLimit, upperLimit )<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &lt; lowerLimit){ return lowerLimit }<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &gt; upperLimit) { return upperLimit }<br /> &nbsp; &nbsp; &nbsp; &nbsp; return val;<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC GETTER FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;value&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return (_red &lt;&lt; 16) | (_green &lt;&lt; 8 ) | _blue; <br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;red&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _red;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;green&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _green;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;blue&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _blue;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC SETTER FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;value&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red &nbsp;= val &gt;&gt; 16 &amp; 0xFF; // red<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green &nbsp;= val &nbsp;&gt;&gt; 8 &amp; 0xFF; // green<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = val &amp; 0xFF; // blue<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; _value = val;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;red&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red = limit( _red, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;green&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green = limit( _green, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;blue&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = limit( _blue, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PRIVATE VARIABLES */<br /> &nbsp; &nbsp; var _value = color;<br /> &nbsp; &nbsp; var _red;<br /> &nbsp; &nbsp; var _green;<br /> &nbsp; &nbsp; var _blue;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC VARIABLES */<br /> &nbsp; &nbsp; this.value = _value;<br /> &nbsp; &nbsp; <br /> };<br /> &nbsp;</div><p>With this javascript class, you could use it like this:</p><div class="bbCSH" style="font-family: monospace;"> var color = new nl.stroep.utils.Color(0xFFCC00) // define orange.<br /> color.green = 0; // remove green.. now it is red..<br /> color.blue = 255; // add some blue.. now it is purple..<br /> alert(color.value.toString(16)) // alerts FF00FF and that is purple.<br /> &nbsp;</div><p>Hope you enjoyed this article. Feel free to share or comment.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1146&amp;md5=2a3c43bbcb2a14cb40dd0ee0e476f64f" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <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>10</slash:comments> </item> </channel> </rss>
