<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Alxandr Productions &#187; ActionScript3</title>
	<atom:link href="http://alxen.wordpress.com/tag/actionscript3/feed/" rel="self" type="application/rss+xml" />
	<link>http://alxen.wordpress.com</link>
	<description>The creations and thoughts of a high-school student.</description>
	<lastBuildDate>Sun, 08 Nov 2009 19:07:51 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='alxen.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ab443bf83cecf76fd374167435a4f514?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Alxandr Productions &#187; ActionScript3</title>
		<link>http://alxen.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://alxen.wordpress.com/osd.xml" title="Alxandr Productions" />
		<item>
		<title>Functions, Parameters and Return types</title>
		<link>http://alxen.wordpress.com/2009/02/01/functions-parameters-and-return-types/</link>
		<comments>http://alxen.wordpress.com/2009/02/01/functions-parameters-and-return-types/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 01:17:03 +0000</pubDate>
		<dc:creator>Alxandr</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://alxen.wordpress.com/?p=112</guid>
		<description><![CDATA[This article is meant for those who attend the class IKT2 at Vågsbygd VGS, yet it is still written in english in case anyone else wants to read this post. I do not claim to be an expert on the subject, yet I’ll try here to describe the basics of functions, parameters and return types [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alxen.wordpress.com&blog=4405795&post=112&subd=alxen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This article is meant for those who attend the class IKT2 at Vågsbygd VGS, yet it is still written in english in case anyone else wants to read this post. I do not claim to be an expert on the subject, yet I’ll try here to describe the basics of functions, parameters and return types in AS3.</p>
<h2>Functions</h2>
<p>A normal function in AS3 has the given syntax:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 1)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4</pre>
</td>
<td style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#339966;font-weight:bold;">function</span> funcionName<span style="color:#000000;">(</span><span style="color:#004993;">parameters</span><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span>returnType
<span style="color:#000000;">{
</span>      <span style="color:#009900;">//Do something</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>As many of you already know, you declare a function using the keyword &#8220;<em>function</em>&#8220;. The point of a function is often to stack together some code that you want executed several times. Simply to make shure that you don&#8217;t need to retype that code for every time you want it to execute. An example, if you would need to type code to check wheater or not the answer was correct to a question for every possible method of answering that question, you would need to retype the code several times, thus leading to greater chance of mistyping something. For instance, you would want the question to be checked if the user clicks the enter key, the tab key and the answer button. This is simply done with a &#8220;checkAnswer&#8221; function, but if you don&#8217;t know how to write your own functions, and how functions work you would have a problem.</p>
<p>Lets continue working on that verry problem. The answer to the question is 18, and the input-field is called “<em>answerText</em>”. The code to get the answer from the user looks as following:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 2)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">1</pre>
</td>
<td style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#6699cc;font-weight:bold;">var</span> ans<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">String</span></a> = answerText.<span style="color:#004993;">text</span>;</pre>
</td>
</tr>
</tbody>
</table>
<p>This is pretty straight forward. As you can see this only get the answer back as a string, but even though I&#8217;m interested in checking if it&#8217;s a number, I&#8217;m not going to make any mathematical opperations with the number, therefor I don&#8217;t convert it into a number. The check I want to do is also realy simple:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 3)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8</pre>
</td>
<td style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>ans == <span style="color:#990000;">"18"</span><span style="color:#000000;">)</span>
<span style="color:#000000;">{
</span>     <span style="color:#009900;">//The answer was correct</span>
<span style="color:#000000;">}</span>
<span style="color:#0033ff;font-weight:bold;">else</span>
<span style="color:#000000;">{
</span>     <span style="color:#009900;">//The answer was wrong</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This is the basic layout of our function. First we reed in the input, than we check to see if it&#8217;s correct, than we act based on that. If we now had a button that we could click to cause the answer to be checked, and that button was called “<em>checkButton</em>”, the code to do all what I have described would look as folowing:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 4)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10
11
12</pre>
</td>
<td style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#339966;font-weight:bold;">function</span> checkAnswer_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{
</span>     <span style="color:#6699cc;font-weight:bold;">var</span> ans<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">String</span></a> = answerText.<span style="color:#004993;">text</span>;
     <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>ans == <span style="color:#990000;">"18"</span><span style="color:#000000;">)
</span>     <span style="color:#000000;">{
</span>          <span style="color:#009900;">//Do something with correct answer
</span>     <span style="color:#000000;">}
</span>     <span style="color:#0033ff;font-weight:bold;">else
</span>     <span style="color:#000000;">{
</span>          <span style="color:#009900;">//Do something with wrong answer
</span>     <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>If you&#8217;ve read my article &#8220;<em>About an event</em>&#8221; this should look familiar to you. What this function basically does is wait for a button to be clicked, than it reads in the answer and controls that it is equal to the string “18”, than it acts based on that. Now what if you wanted to do the same thing if you pressed the enter-key as well? You would have to retype the whole function once again. If you ever have to do that, there should ring a bell for you. If you ever find yourself retyping functionallity, put it in a function!</p>
<p>Ok, lets extract the functionallity out of this function (witch is basically everything) and put it in a seperate function like this:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 5)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10
11
12</pre>
</td>
<td style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#339966;font-weight:bold;">function</span> checkAnswer<span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{
</span>       <span style="color:#6699cc;font-weight:bold;">var</span> ans<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">String</span></a> = answerText.<span style="color:#004993;">text</span>;
       <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>ans == <span style="color:#990000;">"18"</span><span style="color:#000000;">)
</span>       <span style="color:#000000;">{
</span>           <span style="color:#009900;">//Do something with correct answer
</span>       <span style="color:#000000;">}
</span>       <span style="color:#0033ff;font-weight:bold;">else
</span>       <span style="color:#000000;">{
</span>           <span style="color:#009900;">//Do something with wrong answer
</span>       <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>As you can see this code looks nearly exactly the same as the code I posted above. Now one could simply put “<em>checkAnswer()</em>” inside the event-function.</p>
<h2>Parameters</h2>
<p>Parameters is used to pass data to functions. You’ve alreaddy garanteed used them, but you may not know about it. When you use the function “<em>Math.sqrt</em>” for instance, you pass it one parameter, a number, that it returns the square root of. When you use “<em>addChild</em>” you pass a parameter of the type MovieClip or Sprite or any other IDisplayable object. Parameters are simply what you put between the parenthesis of a function-call. This means that in the event-function i posted (example 4), the evt:MouseEvent is the only parameter. Parameters are seperated by a comma, so if I wanted more parameters I’dd just write “<em>param1:Type, param2:Type, …, paramN:Type</em>”. When I create functions like this I don’t like to fix what the input are like I’ve done in example 5. The example 5 only generates an answer based on the input in the answerText text-field.</p>
<p>Now, what if I wanted to be able to check several text-fields whith this one function? How would I go about doing that? First of all, the function would need a parameter. This parameter would need to be a string (we are to check if a given string is equal to “18”). This can be done pretty simply. The next example shows you how.</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 6)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10
11</pre>
</td>
<td style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#339966;font-weight:bold;">function</span> checkAnswer<span style="color:#000000;">(</span>ans<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">String</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{
</span>       <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>ans == <span style="color:#990000;">"18"</span><span style="color:#000000;">)
</span>       <span style="color:#000000;">{
</span>           <span style="color:#009900;">//Do something with correct answer</span>
       <span style="color:#000000;">}
</span>       <span style="color:#0033ff;font-weight:bold;">else
</span>       <span style="color:#000000;">{
</span>           <span style="color:#009900;">//Do something with wrong answer</span>
       <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This too is pretty straight forward. The code is nearly exactly the same. Now you would need to use “<em>checkAnswer(answerText.text);</em>” to act based on the answer. This is a good thing, because if we later on wanted to use the answerText2 input-field, all we had to do was call “<em>checkAnswer(answerText2.text);</em>”. This would mean less coding, whitch in general is a good thing. The second good thing about this is that one could easely modify that you would check to see that it’s “19” instead of “18” by replacing it inside this one function. If you had several functions dooing the same thing you would have to search for every “18” there was, and replace it with “19”.</p>
<h2>Return types</h2>
<p>Return types is used to tell what a function is to return. As with a variable you can set the type of a function. In fact, with the new version of flex I think you have to use return types. All of the functions I have posted above have the return type “<em>void</em>”. This means that the function dosn’t return anything. Void is probably the most common type of function. In the previous example (example 6) we made a function that takes a parameter and acts based on that parameter. For instance we could have it set the text at resultText1 to “<em>Correct</em>” or “<em>False</em>” depending on the if-test. However; if we are to use this function to controll several input-boxes, that would look bad. No matter if we had 3 input text-fields it and 3 result text-fields it would always be the first result text-feild that would display “<em>Correct</em>” or “<em>False</em>”. One thing we could do to make this better is add a second parameter to the function. This parameter would be of type “TextField” and we would pass in the text-field we would like to change the text of. This could be done quiet easely like this:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 7)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31</pre>
</td>
<td style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#009900;">// checkAnswer function</span>
<span style="color:#339966;font-weight:bold;">function</span> checkAnswer<span style="color:#000000;">(</span>ans<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">String</span></a>, resultTxtFld<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">TextField</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{
</span>        <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>ans == <span style="color:#990000;">"18"</span><span style="color:#000000;">)
</span>        <span style="color:#000000;">{
</span>            resultTxtFld.<span style="color:#004993;">text</span> = <span style="color:#990000;">"Correct"</span>;
        <span style="color:#000000;">}
</span>        <span style="color:#0033ff;font-weight:bold;">else</span>
        <span style="color:#000000;">{
</span>            resultTxtFld.<span style="color:#004993;">text</span> = <span style="color:#990000;">"False"</span>;
        <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span>

<span style="color:#009900;">//Setup listeners</span>
answerButton1.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, ansBtn1_Click<span style="color:#000000;">)</span>;
answerButton2.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, ansBtn2_Click<span style="color:#000000;">)</span>;
answerButton3.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, ansBtn3_Click<span style="color:#000000;">)</span>;

<span style="color:#009900;">//Click events</span>
<span style="color:#339966;font-weight:bold;">function</span> ansBtn1_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{
</span>        checkAnswer<span style="color:#000000;">(</span>answerText1.<span style="color:#004993;">text</span>, resultText1<span style="color:#000000;">)</span>;
<span style="color:#000000;">}</span>
<span style="color:#339966;font-weight:bold;">function</span> ansBtn2_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{
</span>        checkAnswer<span style="color:#000000;">(</span>answerText2.<span style="color:#004993;">text</span>, resultText2<span style="color:#000000;">)</span>;
<span style="color:#000000;">}</span>
<span style="color:#339966;font-weight:bold;">function</span> ansBtn3_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{</span>
        checkAnswer<span style="color:#000000;">(</span>answerText3.<span style="color:#004993;">text</span>, resultText3<span style="color:#000000;">)</span>;
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This example shows one way to do the trick. However, what if we wanted to go to frame 2 when the the answerButton3 was clicked and the answer in answerText3 was correct? How would we go about doing that? This is a classic example of when it’s a good idea to use another return type than void. Here we would like to know wheater or not the test actually passed. So basically we need the function to return true if the test passed and false if the test failed. A true-false variable is called a Boolean. To return somthing from a function you simply have to type “<em>return &lt;var&gt;;</em>”. One thing that is worth mentioning is that when the code reaches a return statement, it stops executing that function. This means that any code below in that same function will not be exicuted. You can think of it like a break (<a title="http://alxen.wordpress.com/2009/01/27/the-differences-and-similarities-between-for-while-and-do_while-as3/" href="http://alxen.wordpress.com/2009/01/27/the-differences-and-similarities-between-for-while-and-do_while-as3/">The differences and similarities between for, while and do…while (AS3)</a>), only for functions.</p>
<p>This in practice means that insted of using “<em>if() { return true; } else { return true; }</em>”, we can use “<em>if() { return true; } return false;</em>”, because when the executor reaces the return-statement no more code is executed. This means that if the test runs clear, the executor reads the code “<em>return true;</em>”, than it returns true and breaks the function. If not the test clears, the executor reads the “<em>return false;</em>”. It than so does. This saves us a couple of lines with code. The next example shows how I could rewrite the previous one to fill my new needs.</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 8 )</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49</pre>
</td>
<td style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#009900;">// checkAnswer function</span>
<span style="color:#339966;font-weight:bold;">function</span> checkAnswer<span style="color:#000000;">(</span>ans<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">String</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Boolean</span></a>
<span style="color:#000000;">{</span>
        <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>ans == <span style="color:#990000;">"18"</span><span style="color:#000000;">)</span>
        <span style="color:#000000;">{
</span>            <span style="color:#0033ff;font-weight:bold;">return</span> <span style="color:#0033ff;font-weight:bold;">true</span>;
        <span style="color:#000000;">}
</span>        <span style="color:#0033ff;font-weight:bold;">return</span> <span style="color:#0033ff;font-weight:bold;">false</span>;
<span style="color:#000000;">}

</span><span style="color:#009900;">//Setup listeners</span>
answerButton1.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, ansBtn1_Click<span style="color:#000000;">)</span>;
answerButton2.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, ansBtn2_Click<span style="color:#000000;">)</span>;
answerButton2.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, ansBtn3_Click<span style="color:#000000;">)</span>;

<span style="color:#009900;">//Click events</span>
<span style="color:#339966;font-weight:bold;">function</span> ansBtn1_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{</span>
        <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>checkAnswer<span style="color:#000000;">(</span>answerText1.<span style="color:#004993;">text</span><span style="color:#000000;">)</span><span style="color:#000000;">)
</span>        <span style="color:#000000;">{
</span>            resultText1.<span style="color:#004993;">text</span> = <span style="color:#990000;">"Correct"</span>;
        <span style="color:#000000;">}
</span>        <span style="color:#0033ff;font-weight:bold;">else</span>
        <span style="color:#000000;">{
</span>            resultText1.<span style="color:#004993;">text</span> = <span style="color:#990000;">"False"</span>;
        <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span>
<span style="color:#339966;font-weight:bold;">function</span> ansBtn2_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{</span>
        <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>checkAnswer<span style="color:#000000;">(</span>answerText2.<span style="color:#004993;">text</span><span style="color:#000000;">)</span><span style="color:#000000;">)
</span>        <span style="color:#000000;">{</span>
            resultText2.<span style="color:#004993;">text</span> = <span style="color:#990000;">"Correct"</span>;
        <span style="color:#000000;">}</span>
        <span style="color:#0033ff;font-weight:bold;">else</span>
        <span style="color:#000000;">{
</span>            resultText2.<span style="color:#004993;">text</span> = <span style="color:#990000;">"False"</span>;
        <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span>
<span style="color:#339966;font-weight:bold;">function</span> ansBtn3_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{</span>
        <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>checkAnswer<span style="color:#000000;">(</span>answerText3.<span style="color:#004993;">text</span><span style="color:#000000;">)</span><span style="color:#000000;">)
</span>        <span style="color:#000000;">{</span>
            <span style="color:#004993;">gotoAndStop</span><span style="color:#000000;">(</span><span style="color:#000000;font-weight:bold;">2</span><span style="color:#000000;">)</span>;
        <span style="color:#000000;">}</span>
        <span style="color:#0033ff;font-weight:bold;">else</span>
        <span style="color:#000000;">{</span>
            resultText3.<span style="color:#004993;">text</span> = <span style="color:#990000;">"False"</span>;
        <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Like this we have much higher controll over what happens at the cost of some more coding. Notice however that I put the checkAnswer functioncall directly inside the if-statement. This is because the checkAnswer returns a Boolean, witch is what the if-statement expects. In fact, when you use “<em>==</em>”, “<em>&lt;</em>”, “<em>&gt;</em>”, “<em>&gt;=</em>”, “<em>&lt;=</em>” or anything similar to that, what you do is return a Boolean. If the checkAnswer function did indeed return a string i could use “<em>if(checkAnswer(‘18’) == ‘test’) { /* Do something */ }</em>”. This said, there is one small change I could do in the checkAnswer-function to remove some lines of code. To make the checkAnswer-function as small as posible I would change it to this:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;background-color:#ffffdd;width:100%;border-collapse:collapse;font-family:monospace;color:#000022;font-size:12px;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;background-color:#dfdfad;font-weight:bold;" colspan="2">ActionScript 3 code (example 9)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="text-align:right;width:1px;vertical-align:top;margin:0;padding:0 2px;">
<pre style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;">1
2
3
4</pre>
</td>
<td style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;">
<pre style="font:1em/1.2em monospace;background:none transparent scroll repeat 0 0;vertical-align:top;margin:0;padding:0;"><span style="color:#339966;font-weight:bold;">function</span> checkAnswer<span style="color:#000000;">(</span>ans<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">String</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Boolean</span></a>
<span style="color:#000000;">{
</span>        <span style="color:#0033ff;font-weight:bold;">return</span> <span style="color:#000000;">(</span>ans == <span style="color:#990000;">"18"</span><span style="color:#000000;">)</span>;
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>That&#8217;s all for this time. If you&#8217;ve got any questions, please leave a comment.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alxen.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alxen.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alxen.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alxen.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alxen.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alxen.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alxen.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alxen.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alxen.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alxen.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alxen.wordpress.com&blog=4405795&post=112&subd=alxen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://alxen.wordpress.com/2009/02/01/functions-parameters-and-return-types/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5a87311ea4c9950793397f01eb208830?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alxandr</media:title>
		</media:content>
	</item>
		<item>
		<title>About an Event</title>
		<link>http://alxen.wordpress.com/2009/01/29/about-an-event/</link>
		<comments>http://alxen.wordpress.com/2009/01/29/about-an-event/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 10:51:36 +0000</pubDate>
		<dc:creator>Alxandr</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://alxen.wordpress.com/?p=83</guid>
		<description><![CDATA[This article is meant for those who attend the class IKT2 at Vågsbygd VGS, yet it is still written in english in case anyone else wants to read this post. I do not claim to be an expert on the subject, yet I&#8217;ll try here to describe the basics of events in AS3. Also I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alxen.wordpress.com&blog=4405795&post=83&subd=alxen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This article is meant for those who attend the class IKT2 at Vågsbygd VGS, yet it is still written in english in case anyone else wants to read this post. I do not claim to be an expert on the subject, yet I&#8217;ll try here to describe the basics of events in AS3. Also I will touch the subject of Object Orientet Programming (known as OOP).</p>
<h2>Eventbased programming</h2>
<p>When you&#8217;re programming in AS3 against Flash or Flex you want to use an event oriented approach. If I recall correctly the other way of doing things is called flow-based, and you do not want to use that approach in flash. If you are, than I am almost certain you are doing something wrong. Event-based programming means you&#8217;re not actually doing anything until an event happens. This might be that a user clicks a button, or a file is finished loading. Events all have certain similarities, their all Event. To truly understand what this means you need to have a basic understanding of the Object Oriented Programming. I&#8217;ll explain a bit about that later, but for now, just know that all events are (in flash) Event. The great thing about all events being Event is that once you learn to use one of them, you should in theory understand them all. One similarity all event-funtions have (at least all of witch I&#8217;ve seen) is that they all except one parameter of the type Event, and they all return void (nothing). The basic syntax of a event-function is as following:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code (example 1)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#339966;font-weight:bold;">function</span> eventFunc<span style="color:#000000;">(</span>param1<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Event</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{</span>
    <span style="color:#009900;">//Do something</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This is a normal event and could be used for every event-type there is. Many of you would probably call the local variable evt instead of param1, I only called it that because I would like to show you that the naming of the local variables does not affect the code. I could have called param1 anything that is a valid variable name. If i wanted to I could call it eventParam1, or e, or nearly anything else. I will explain this furter in the article &#8220;<a title="Functions, Parameters and Return types" href="http://alxen.wordpress.com/2009/02/01/functions-parameters-and-return-types/">Functions, Parameters and Return types</a>&#8220;. Just know for now that you can name it whatever you like.</p>
<p>Another thing probably none of you have seen before is the :void at the end of the declaration of the function. This tells what the function should return. Now this too I will talk about in &#8220;Functions, Parameters and Return types&#8221;, but know for now that nearly every event (I haven&#8217;t seen any exceptions on that yet) returns void (witch means nothing).</p>
<h2>Object Orientet Programming (OOP)</h2>
<p>AS3 is an Object Orientet Programming language. The OOP aproch tries to use a object oriented model that looks like the one that exists in the real world. The point is that objects inherit from eachother. Like in the real world. A cat is an animal, and contains every feature and actions that an animal contain, and animal is a living creature and so on. This has several benefits. For instance, if I asked you to come to my party and bring a cat, any cat would do. I wouldn&#8217;t care if the cat was black or white, small or big. The same way if I asked you to bring an animal, you could bring any animal you could think of. OOP acts the same way. If it expects an animal, and you bring it a cat (witch obviously is a animal), AS3 wouldn&#8217;t even think twice before proceeding with what it should do. However, if AS3 is expecting a cat, and you give it an animal you would get an error.</p>
<p>There is another great benefit of using a OOP-language. Because the language is OOP and objects inherit from each other, there is also a building-block witch is at the bottom. A object-type that every other object-type inherits from. And this is the Object. Every object-type in AS3 inherits from Object. That means that that every variable we use in AS3 <em>is an</em> Object. That means that the following code is valid:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code (example 2)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#6699cc;font-weight:bold;">var</span> var1<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Object</span></a> = <span style="color:#990000;">"123"</span>;
<span style="color:#6699cc;font-weight:bold;">var</span> var2<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Object</span></a> = <span style="color:#000000;font-weight:bold;">123</span>;
<span style="color:#6699cc;font-weight:bold;">var</span> var3<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Object</span></a> = <span style="color:#0033ff;font-weight:bold;">new</span> <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MovieClip</span></a><span style="color:#000000;">(</span><span style="color:#000000;">)</span>;</pre>
</td>
</tr>
</tbody>
</table>
<p>You wouldn&#8217;t expect that, now would you? Object itself also have som methods. The most usefull of them is the method toString(). This means that all objects can be represented as a string. This is actually what happens when you run a trace statement on an object. Trace calls toString() on the object and then displays the result in the output-window. The next code is also valid (given that you&#8217;ve alreaddy ran the the previous code):</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code (example 3)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#004993;">trace</span><span style="color:#000000;">(</span>var1.<span style="color:#004993;">toString</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>; <span style="color:#009900;">//123</span>
<span style="color:#004993;">trace</span><span style="color:#000000;">(</span>var2.<span style="color:#004993;">toString</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>; <span style="color:#009900;">//123</span>
<span style="color:#004993;">trace</span><span style="color:#000000;">(</span>var3.<span style="color:#004993;">toString</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>; <span style="color:#009900;">//[object MovieClip]</span></pre>
</td>
</tr>
</tbody>
</table>
<p>The comment shows what each line will output. Object also contains some other methods, but I wont get into those now. I rarely use them anyway.</p>
<h2>Event</h2>
<p>As I said in the start, every event is an Event. That meas that every possible event (MouseEvent, KeyboardEvent, ProgressEvent++) inherits from Event. That means that the function I wrote at the top of this page (example 1), can be used for any event there is. The Event object has some methods and properties that truly are usefull. I won&#8217;t discuss all of them, but I&#8217;ll try to show you the ones I find most usefull.</p>
<h3>Properties</h3>
<dl>
<dt><a href="http://livedocs.adobe.com/flex/201/langref/flash/events/Event.html#target">target</a></dt>
<dd>&#8220;<em>The event target. This property contains the target node. For example, if a user clicks an OK button, the target node is the display list node containing that button.</em>&#8221; (Source Adobe Live Docs). This basically means that target contains the target of the event. If the event is that a user clicked a button, the target property of the event would contain that very button. </dd>
</dl>
<h3>Methods</h3>
<dl>
<dt><a href="http://livedocs.adobe.com/flex/201/langref/flash/events/Event.html#preventDefault()">preventDefault()</a></dt>
<dd>&#8220;<em>Cancels an event&#8217;s default behavior if that behavior can be canceled.</em>&#8221; (Source Adobe Live Docs). The preventDefault is used for instance if you have a link, that when you click it opens a web page, than one time you discover that if var a &lt; 5 you don&#8217;t want to open that web page. If that is the case, simply check if a &lt; 5, than call preventDefault(); </dd>
<dt><a href="http://livedocs.adobe.com/flex/201/langref/flash/events/Event.html#stopPropagation()">stopPropagation()</a></dt>
<dd>&#8220;<em>Prevents processing of any event listeners in nodes subsequent to the current node in the event flow. This method does not affect any event listeners in the current node (currentTarget). In contrast, the stopImmediatePropagation() method prevents processing of event listeners in both the current node and subsequent nodes. Additional calls to this method have no effect. This method can be called in any phase of the event flow.</em>&#8221; (Source Adobe Live Docs). The stopPropagation method is used to make sure that the event is not called again. For instance, if you put a button inside a movieclip that lies on the stage and you click the button, click is first called on the button, than on the movieclip, than on the stage. However, if you call stopPropagation on the movieclip, click is never called on the stage. </dd>
</dl>
<h2>Event-functions</h2>
<p>The functions that is used at events are in many ways similar. I showed you one at the top of this article that could be used to any event at all, yet I would like to show another example. The next example is a flash-movie with two movieclips (mc1 and mc2). When I click any of the movieclips I want the clicked one to schrink by 10px width and 5px height. The code for this could look like this:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code (example 4)</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">mc1.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, mc_Click<span style="color:#000000;">)</span>;
mc2.<span style="color:#004993;">addEventListener</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=mouseevent%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:mouseevent.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MouseEvent</span></a>.<span style="color:#004993;">CLICK</span>, mc_Click<span style="color:#000000;">)</span>;
<span style="color:#339966;font-weight:bold;">function</span> mc_Click<span style="color:#000000;">(</span>evt<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=event%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:event.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Event</span></a><span style="color:#000000;">)</span><span style="color:#000000;font-weight:bold;">:</span><span style="color:#0033ff;font-weight:bold;">void</span>
<span style="color:#000000;">{</span>
    <span style="color:#0033ff;font-weight:bold;">try</span>
    <span style="color:#000000;">{</span>
        <span style="color:#6699cc;font-weight:bold;">var</span> mc<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MovieClip</span></a> = <a href="http://www.google.com/search?q=movieclip%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:movieclip.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">MovieClip</span></a><span style="color:#000000;">(</span>evt.<span style="color:#004993;">target</span><span style="color:#000000;">)</span>;
        mc.<span style="color:#004993;">width</span> <span style="color:#000000;font-weight:bold;">-</span>= <span style="color:#000000;font-weight:bold;">10</span>;
        mc.<span style="color:#004993;">height</span> <span style="color:#000000;font-weight:bold;">-</span>= <span style="color:#000000;font-weight:bold;">5</span>;
    <span style="color:#000000;">}</span>
    <span style="color:#0033ff;font-weight:bold;">catch</span><span style="color:#000000;">(</span>e<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=error%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:error.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Error</span></a><span style="color:#000000;">)</span>
    <span style="color:#000000;">{</span>
        <span style="color:#004993;">trace</span><span style="color:#000000;">(</span><span style="color:#990000;">"An error occurred!"</span><span style="color:#000000;">)</span>;
    <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This example is pretty simple. What most of you probably havn&#8217;t seen is the try{} catch(e:Error){} block. Try works as follow:</p>
<ul>
<li>The program tries to execute the code in the try-block. If anything fails it jumps to the catch-block right away.</li>
<li>The catch-block is never executed if no error occurred in the try-block.</li>
</ul>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alxen.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alxen.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alxen.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alxen.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alxen.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alxen.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alxen.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alxen.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alxen.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alxen.wordpress.com/83/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alxen.wordpress.com&blog=4405795&post=83&subd=alxen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://alxen.wordpress.com/2009/01/29/about-an-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5a87311ea4c9950793397f01eb208830?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alxandr</media:title>
		</media:content>
	</item>
		<item>
		<title>The differences and similarities between for, while and do&#8230;while (AS3)</title>
		<link>http://alxen.wordpress.com/2009/01/27/the-differences-and-similarities-between-for-while-and-do_while-as3/</link>
		<comments>http://alxen.wordpress.com/2009/01/27/the-differences-and-similarities-between-for-while-and-do_while-as3/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 16:48:58 +0000</pubDate>
		<dc:creator>Alxandr</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://alxen.wordpress.com/?p=57</guid>
		<description><![CDATA[This article is meant for those who attend the class IKT2 at Vågsbygd VGS, yet it is still written in english in case anyone else wants to read this post. I do not claim to be an expert on the subject, yet I will try to describe the differences and similarities between the for-loop, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alxen.wordpress.com&blog=4405795&post=57&subd=alxen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This article is meant for those who attend the class IKT2 at Vågsbygd VGS, yet it is still written in english in case anyone else wants to read this post. I do not claim to be an expert on the subject, yet I will try to describe the differences and similarities between the for-loop, the while-loop and the do&#8230;while-loop. I will also comment on the switch-case. Last I&#8217;ll briefly go trough break and continue. If anyone has any questions or comments, please write them in english, or they will not be answered.</p>
<p>The first one you learn and the easiest one to use is the while-loop. It is pretty straight forward. It is used to do something (whatever you like) while a statement is true. The syntax of the while-loop is like this:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#0033ff;font-weight:bold;">while</span><span style="color:#000000;">(</span>condition<span style="color:#000000;">)</span>
<span style="color:#000000;">{</span>
    <span style="color:#009900;">//Do something</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>The while loop can be used to do something a number of times. For example the next code will trace out every number between 0 and 15 (including 0, not including 15).</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#6699cc;font-weight:bold;">var</span> i<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Number</span></a> = <span style="color:#000000;font-weight:bold;">0</span>;
<span style="color:#0033ff;font-weight:bold;">while</span><span style="color:#000000;">(</span>i <span style="color:#000000;font-weight:bold;">&lt;</span> <span style="color:#000000;font-weight:bold;">15</span><span style="color:#000000;">)</span>
<span style="color:#000000;">{</span>
    <span style="color:#004993;">trace</span><span style="color:#000000;">(</span>i<span style="color:#000000;">)</span>;
    i<span style="color:#000000;font-weight:bold;">++</span>;
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>However, when itterating through something like this it is much more common to use the for-loop. In cases like this, there is no difference except the syntax. This example does exactly the same as the previous one, only it uses the for-loop instead of the while-loop.</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#0033ff;font-weight:bold;">for</span><span style="color:#000000;">(</span><span style="color:#6699cc;font-weight:bold;">var</span> i<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Number</span></a> = <span style="color:#000000;font-weight:bold;">0</span>; i <span style="color:#000000;font-weight:bold;">&lt;</span> <span style="color:#000000;font-weight:bold;">15</span>; i<span style="color:#000000;font-weight:bold;">++</span><span style="color:#000000;">)</span>
<span style="color:#000000;">{</span>
    <span style="color:#004993;">trace</span><span style="color:#000000;">(</span>i<span style="color:#000000;">)</span>;
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>As you can see, this is done with a lot less code, yet the result is exactly the same. The syntax of the for-loop is as following:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#0033ff;font-weight:bold;">for</span><span style="color:#000000;">(</span>initilization; condition; incrementation<span style="color:#000000;">)</span>
<span style="color:#000000;">{</span>
    <span style="color:#009900;">//Do something</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>I realize that the name &#8220;incrementation&#8221; is not correct, yet it&#8217;s the best I can come up with. The point is that you do something several times. I usually use the for-loop instead of the while-loop, except when it&#8217;s something I can&#8217;t count. For example if I&#8217;m reading a file, I use a while loop as long as there&#8217;s still something left to read. Other than that I use the for-loop.</p>
<p>Occationally you want to make shure something happens at least once. In that case you&#8217;ll want to use the do&#8230;while-loop. A good example of this is if you wan&#8217;t to calculate a random number between 1 and 6, yet you don&#8217;t want it to be 5 or 3. I would do that like:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#6699cc;font-weight:bold;">var</span> rndNum<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Number</span></a>;
<span style="color:#0033ff;font-weight:bold;">do</span>
<span style="color:#000000;">{</span>
    rndNum = <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Math</span></a>.<span style="color:#004993;">floor</span><span style="color:#000000;">(</span><a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Math</span></a>.<span style="color:#004993;">random</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span> <span style="color:#000000;font-weight:bold;">*</span> <span style="color:#000000;font-weight:bold;">6</span><span style="color:#000000;">) + 1</span>;
<span style="color:#000000;">}</span>
<span style="color:#0033ff;font-weight:bold;">while</span><span style="color:#000000;">(</span>rndNum <span style="color:#000000;font-weight:bold;">!</span>= <span style="color:#000000;font-weight:bold;">5</span> <span style="color:#000000;font-weight:bold;">&amp;&amp;</span> rndNum <span style="color:#000000;font-weight:bold;">!</span>= <span style="color:#000000;font-weight:bold;">3</span><span style="color:#000000;">)</span>;</pre>
</td>
</tr>
</tbody>
</table>
<p>This first creates an empty variable called<em> rndNum</em>, then it keeps generating a new random number until rndNum is not equal to rndNum 5 or 3. Here I use the while-loop because I don&#8217;t know how many times I need to recalculate rndNum. It might be none or it might be a hundred.</p>
<p>Last I&#8217;d like to give a simple example of the switch-case condition. The switch-case is only used for special cases. I only use the switch-case when I KNOW that the input is going to be one in a range of others. For instance, if I asked you for a number between 1 and 10, and would treat you differently based on your result, I&#8217;d use the switch-case. I better example is a simple program where the user is asked to enter a number (a choice) between one and three. This could be used for a menu (press 1 to save, press 2 to load and press 3 to exit). For example like this:</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#6699cc;font-weight:bold;">var</span> input<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Number</span></a> = <a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Number</span></a><span style="color:#000000;">(</span>inputField.<span style="color:#004993;">text</span><span style="color:#000000;">)</span>;
<span style="color:#0033ff;font-weight:bold;">switch</span><span style="color:#000000;">(</span>input<span style="color:#000000;">)</span>
<span style="color:#000000;">{</span>
    <span style="color:#0033ff;font-weight:bold;">case</span> <span style="color:#000000;font-weight:bold;">1</span><span style="color:#000000;font-weight:bold;">:</span>
        <span style="color:#009900;">//Save</span>
        <span style="color:#0033ff;font-weight:bold;">break</span>;
    <span style="color:#0033ff;font-weight:bold;">case</span> <span style="color:#000000;font-weight:bold;">2</span><span style="color:#000000;font-weight:bold;">:</span>
        <span style="color:#009900;">//Load</span>
        <span style="color:#0033ff;font-weight:bold;">break</span>;
    <span style="color:#0033ff;font-weight:bold;">case</span> <span style="color:#000000;font-weight:bold;">3</span><span style="color:#000000;font-weight:bold;">:</span>
        <span style="color:#009900;">//Exit</span>
        <span style="color:#0033ff;font-weight:bold;">break</span>;
    <span style="color:#0033ff;font-weight:bold;">default</span><span style="color:#000000;font-weight:bold;">:</span>
        <span style="color:#009900;">//Display "incorrect input"</span>
        <span style="color:#0033ff;font-weight:bold;">break</span>;
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Thats basically it about the switch-case.</p>
<p>Two more things that is needed to be fully able to use loops and switch-case statements is the understanding of <em>break</em> and <em>continue</em>. The break is used to exit a loop. For instance the next loop will only trace out the numbers 0 to 10 (including 10), even though the loop itself looks like it is to trace out 11, 12, 13 and 14 as well.</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#0033ff;font-weight:bold;">for</span><span style="color:#000000;">(</span><span style="color:#6699cc;font-weight:bold;">var</span> i<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Number</span></a> = <span style="color:#000000;font-weight:bold;">0</span>; i <span style="color:#000000;font-weight:bold;">&lt;</span> <span style="color:#000000;font-weight:bold;">15</span>; i<span style="color:#000000;font-weight:bold;">++</span><span style="color:#000000;">)</span>
<span style="color:#000000;">{</span>
    <span style="color:#004993;">trace</span><span style="color:#000000;">(</span>i<span style="color:#000000;">)</span>;
    <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>i == <span style="color:#000000;font-weight:bold;">10</span><span style="color:#000000;">)</span>
    <span style="color:#000000;">{</span>
        <span style="color:#0033ff;font-weight:bold;">break</span>;
    <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>There actually isn&#8217;t anything more to it. All the break does is exit the loop it&#8217;s in. One thing worth mentioning though is that the switch-case is also treated as a loop in that aspect. That&#8217;s why we always use break before the next case, to make shure that when it has done what it was meant to, it exits the switch-case.</p>
<p>The last topic is the continue statement. It is used to skip the current loop-run. This next code will trace out every number from 0 to (including) 14 exept the number 5.</p>
<table class="actionscript3" style="border:1px solid #dfdfad;font-size:12px;width:100%;color:#000022;font-family:monospace;border-collapse:collapse;background-color:#ffffdd;margin:0;" border="0">
<thead>
<tr>
<td style="text-align:center;font-weight:bold;background-color:#dfdfad;" colspan="2">ActionScript 3 code</td>
</tr>
</thead>
<tbody>
<tr class="li1">
<td style="vertical-align:top;width:1px;text-align:right;margin:0;padding:0 2px;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">1
2
3
4
5
6
7
8</pre>
</td>
<td style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;">
<pre style="font:normal normal 1em/1.2em monospace;background:none;vertical-align:top;margin:0;padding:0;"><span style="color:#0033ff;font-weight:bold;">for</span><span style="color:#000000;">(</span><span style="color:#6699cc;font-weight:bold;">var</span> i<span style="color:#000000;font-weight:bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html&amp;filter=0&amp;num=100&amp;btnI=lucky"><span style="color:#004993;">Number</span></a> = <span style="color:#000000;font-weight:bold;">0</span>; i <span style="color:#000000;font-weight:bold;">&lt;</span> <span style="color:#000000;font-weight:bold;">15</span>; i<span style="color:#000000;font-weight:bold;">++</span><span style="color:#000000;">)</span>
<span style="color:#000000;">{</span>
    <span style="color:#0033ff;font-weight:bold;">if</span><span style="color:#000000;">(</span>i == <span style="color:#000000;font-weight:bold;">5</span><span style="color:#000000;">)</span>
    <span style="color:#000000;">{</span>
        <span style="color:#0033ff;font-weight:bold;">continue</span>;
    <span style="color:#000000;">}</span>
    <span style="color:#004993;">trace</span><span style="color:#000000;">(</span>i<span style="color:#000000;">)</span>;
<span style="color:#000000;">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>The continue is hard to explain, and from my experiece rarely used, yet it might be useful to know. However, you should be careful when using it with the while-loop.</p>
<p>That&#8217;s about it for this time. If you have any questions, please leave me a comment.</p>
<p>Special thanks to:</p>
<ul>
<li><em>Eirik Løhaugen Fjærbu</em></li>
<li><em>Johanne Rasmussen </em><em></em></li>
</ul>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alxen.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alxen.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/alxen.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/alxen.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/alxen.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/alxen.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/alxen.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/alxen.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/alxen.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/alxen.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alxen.wordpress.com&blog=4405795&post=57&subd=alxen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://alxen.wordpress.com/2009/01/27/the-differences-and-similarities-between-for-while-and-do_while-as3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5a87311ea4c9950793397f01eb208830?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Alxandr</media:title>
		</media:content>
	</item>
	</channel>
</rss>