<?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"
	>

<channel>
	<title>Flashyard</title>
	<atom:link href="http://flashyard.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://flashyard.com</link>
	<description>All things Flash</description>
	<pubDate>Tue, 02 Sep 2008 10:00:34 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Programming ActionScript 3 for Beginners</title>
		<link>http://flashyard.com/2008/09/programming-actionscript-3-for-beginners/</link>
		<comments>http://flashyard.com/2008/09/programming-actionscript-3-for-beginners/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 09:58:40 +0000</pubDate>
		<dc:creator>Roy</dc:creator>
		
		<category><![CDATA[ActionScript 3]]></category>

		<category><![CDATA[Tutorials]]></category>

		<category><![CDATA[Animation]]></category>

		<category><![CDATA[Beginner]]></category>

		<category><![CDATA[Fish]]></category>

		<category><![CDATA[Flash CS3]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://flashyard.com/?p=58</guid>
		<description><![CDATA[As I&#8217;m sure you already know, Flash is a great tool for making your very own games, but when you&#8217;re just starting out it can be hard to know where to begin.
In this tutorial I&#8217;ll give you a quick introduction to ActionScript 3. I&#8217;m going to assume that you know the basics of drawing in [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;m sure you already know, Flash is a great tool for making your very own games, but when you&#8217;re just starting out it can be hard to know where to begin.</p>
<p>In this tutorial I&#8217;ll give you a quick introduction to ActionScript 3. I&#8217;m going to assume that you know the basics of drawing in Flash, and that you&#8217;re familiar with layers, movie clips, the stage, the timeline and frames. However, you need little or no programming experience. The shortcuts I list are for the PC, but they&#8217;re basically the same on the mac, just replace Control with Command. For this tutorial you&#8217;ll need Flash CS3 or higher.</p>
<p><span id="more-58"></span><br />
<strong>Contents:</strong></p>
<p>Part 1: Getting ready<br />
Part 2: Hello world!<br />
Part 3: Comments<br />
Part 4: Variables<br />
Part 5: Animating with ActionScript</p>
<p><strong>Part 1: Getting ready</strong></p>
<p>Fire up Flash CS3, and create a new &#8220;Flash File (ActionScript 3.0)&#8221; (Ctrl+N).</p>
<p>You should now have a fresh project with a single layer named <em>Layer 1</em>.</p>
<p><img class="alignnone size-medium wp-image-68" title="layer_1_timeline" src="http://flashyard.com/blog/wp-content/uploads/2008/08/layer_1_timeline.png" alt="" width="550" height="150" /></p>
<p>Rename that layer (by double clicking on it) something like <em>actions</em> to keep track of where you&#8217;ll write your ActionScript, and lock it (by clicking on the dot under the tiny lock icon) so that you can&#8217;t place any graphic elements on that layer. All this isn&#8217;t strictly necessary, but it&#8217;s nice to keep things tidy. Now save your file (Ctrl+S) and give it a catchy name, like <em>programming_basics.fla</em>.</p>
<p><img class="alignnone size-medium wp-image-69" title="actions_timeline" src="http://flashyard.com/blog/wp-content/uploads/2008/08/actions_timeline.png" alt="" width="550" height="150" /></p>
<p>Now select the single keyframe in the timeline, and bring up the actions panel by hitting F9. We&#8217;ll write all our code on this keyframe.</p>
<p>That&#8217;s it, we&#8217;re now ready to start programming!</p>
<p><strong>Part 2: Hello world!</strong></p>
<p>With the actions panel still open, write the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Hello world!&quot;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Notice that there&#8217;s a small <em>a</em> on the keyframe you just wrote the code on. Whenever you want to get back to this code, you have to select the keyframe with the <em>a</em>, and press F9 to bring up the actions panel if it&#8217;s closed.</p>
<p><img src="http://flashyard.com/blog/wp-content/uploads/2008/09/actions_keyframe.png" alt="" title="actions_keyframe" width="550" height="150" class="alignnone size-medium wp-image-107" /></p>
<p>Now test your movie, by hitting Ctrl+Enter. Along with the blank movie, the output window should pop up with the words &#8220;Hello world!&#8221; showing.</p>
<p><img class="alignnone size-medium wp-image-74" title="output_panel_hello_world" src="http://flashyard.com/blog/wp-content/uploads/2008/08/output_panel_hello_world.png" alt="" width="550" height="80" /></p>
<p>If you see this message in your output window, congratulations! You&#8217;ve just written a working piece of code. <img src='http://flashyard.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let me explain a bit more. A program is just a series of instructions - which we call <em>statements</em>. The <code>trace</code> statement outputs everything you place inside the parenthesis. Notice that we wrapped <em>Hello world!</em> in quotes. If you want it to output text, you need to put it in quotes. If you don&#8217;t, actionscript will assume it&#8217;s a number or variable (we&#8217;ll get to variables later) and you will get an error message. The semicolon indicates the end of the statement.</p>
<p>Try to trace a number instead of text. Remeber that you don&#8217;t need quotes when you&#8217;re only using numbers:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">47</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>I&#8217;ll show you why the trace statement is so useful in a bit.</p>
<p><strong>Part 3: Comments</strong></p>
<p>There are ways you can write notes to yourself or others in your code, which we call comments. This can help you keep track of your code. When you have a lot of code it becomes increasingly important to comment your code well.</p>
<p>There are two types of comments - single-line, and multi-line. You must begin a single-line comment with two forward slashes, and the multi-line comment starts with <code>/*</code> and ends with <code>*/</code>, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #009900;">// Everything on this line following the slashes will be commented out.</span>
&nbsp;
<span style="color: #3f5fbf;">/* Everything between these
slashes will be
commented out. */</span></pre></div></div>

<p>Comments are not only handy for writing notes, they are also great if you want to disable a piece of code temporarily. That way you can just &#8220;comment out&#8221; the desired piece of code, and you wouldn&#8217;t have to erase it completely. ActionScript will skip anything that&#8217;s commented out.</p>
<p>Now clear all your previous code and let&#8217;s continue to the next step.</p>
<p><strong>Part 4: Variables</strong></p>
<p>Variables are essential to keeping track of information in your code. They can be thought of as named containers. They can contain numbers, text or even objects. Let&#8217;s have a closer look. We declare - or create - a variable using the <code>var</code> keyword, followed by the name we want it to have, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> myOwnVariable;</pre></div></div>

<p>Make sure your variable name is unique so it won&#8217;t conflict with other variables. It&#8217;s common (but not necessary) to make the first word in a variable name all lower case, and then start each successive word with an upper case, like in the code above. Also remember to end your statement with a semicolon.</p>
<p>Right now that variable doesn&#8217;t contain anything. To assign it a value, we write the name of the variable followed by an equals sign, and then the value we want to assign:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> myOwnVariable;
&nbsp;
myOwnVariable = <span style="color: #990000;">&quot;Hello world!&quot;</span>;</pre></td></tr></table></div>

<p>Again, we close our statement with a semicolon. Now let&#8217;s use the <code>trace</code> statement you learned earlier, to output the contents of our newly created variable. Here&#8217;s how to do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> myOwnVariable; <span style="color: #009900;">// creates the variable</span>
&nbsp;
myOwnVariable = <span style="color: #990000;">&quot;Hello world!&quot;</span>; <span style="color: #009900;">// assigns the string (text value) &quot;Hello world!&quot; to the variable</span>
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>myOwnVariable<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// outputs the contents of the variable</span></pre></td></tr></table></div>

<p>Note that I added some comments in the code. Test your movie (Ctrl+Enter) to see that the text &#8220;Hello world!&#8221; shows in the output window. Pretty good! As you can see, you can use the <code>trace</code> statement to show you the contents of a variable. This comes in handy when you don&#8217;t know what a variable contains at any given time. It&#8217;s a must when debugging your program.</p>
<p>Now try this - put quotes around myOwnVariable in the <code>trace</code> statement like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> myOwnVariable;
&nbsp;
myOwnVariable = <span style="color: #990000;">&quot;Hello world!&quot;</span>;
&nbsp;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;myOwnVariable&quot;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>Now test your movie, and notice that it doesn&#8217;t output the contents of the variable, but the string &#8220;myOwnVariable&#8221; instead. This is because - as I told you earlier - when you use quotes, ActionScript assumes it&#8217;s a string (text), otherwise it assumes it&#8217;s a number or a variable.</p>
<p>Okay, you should now know a little about trace, variables and commenting. It&#8217;s not quite enough to make a game, but it&#8217;s a good start. Okay, let&#8217;s make a simple animation using ActionScript.</p>
<p><strong>Part 5: Animating with ActionScript</strong></p>
<p>Make sure you clear out your previous code. Now close the actions panel, and we&#8217;re back on the stage. Make a new layer and draw a fish.</p>
<p><img src="http://flashyard.com/blog/wp-content/uploads/2008/08/fish.png" alt="" title="fish" width="550" height="80" class="alignnone size-medium wp-image-83" /></p>
<p>It doesn&#8217;t have to be a masterpiece. If you want to use my fish, you can download my project <a href="http://flashyard.com/tutorials/programming_basics.fla">here</a>.</p>
<p>When you&#8217;re done, convert it into a movie clip symbol by selecting it and hitting F8. Give it a good solid name, like <em>Fish</em>.</p>
<p><img src="http://flashyard.com/blog/wp-content/uploads/2008/08/convert_fish_to_symbol.png" alt="" title="convert_fish_to_symbol" width="550" height="150" class="alignnone size-medium wp-image-80" /></p>
<p>Now delete it off the stage and look in your Library panel, your <em>Fish</em> symbol should be there. Right click it in the Library panel, and choose &#8220;Linkage&#8230;&#8221;. Check off &#8220;Export for ActionScript&#8221;. Notice that it says <em>Fish</em> in the &#8220;Class&#8221; text field. We&#8217;ll use this in our code to make, or instantiate, a fish. I&#8217;ll show you what this means.</p>
<p><img src="http://flashyard.com/blog/wp-content/uploads/2008/08/fish_linkage_properties.png" alt="" title="fish_linkage_properties" width="550" height="300" class="alignnone size-medium wp-image-81" /></p>
<p>Test your movie to see that it&#8217;s completely blank. Now select the keyframe on the actions layer, open up the actions panel again (F9), and let&#8217;s change that. Let&#8217;s make your fish appear on the stage. Make a variable with a unique name, and remember to start it with a lowercase letter.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> myCoolVariable;</pre></td></tr></table></div>

<p>Let&#8217;s instantiate the <em>Fish</em> using that variable. You can think of it as putting the fish inside the variable, or assigning the fish a name (the variable name) so we can give it commands.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> myCoolVariable;
&nbsp;
myCoolVariable = <span style="color: #0033ff; font-weight: bold;">new</span> Fish<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>Notice that it&#8217;s almost the same as putting a number or string inside the variable, only this time we make a new object - the Fish - and put it in the variable instead. The fish now has the name <em>myCoolVariable</em>. What we need to do now, is add this fish to the stage.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> myCoolVariable;
&nbsp;
myCoolVariable = <span style="color: #0033ff; font-weight: bold;">new</span> Fish<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>myCoolVariable<span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>We use <code>addChild();</code> to add something to the stage. Think of the objects on stage, as &#8220;children&#8221; of the stage. Note that we didn&#8217;t write addChild(Fish); but we use its new name, <em>myCoolVariable</em>. Test the movie to see the fish in the upper left corner. This is because it&#8217;s automatically placed on the x, y coordinates of 0, 0 as this is the default.</p>
<p>There&#8217;s only one problem with that code - the name I assigned to the fish doesn&#8217;t make much sense. It&#8217;s just to illustrate that the name can be anything you want. I&#8217;m going to change it to <em>fish</em>, and I suggest you do the same. Note that lowercase fish is different from Fish, so those names won&#8217;t cause conflicts. Always make sure your names are unique!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> fish;
&nbsp;
fish = <span style="color: #0033ff; font-weight: bold;">new</span> Fish<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>fish<span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>Test your code to make sure it&#8217;s still working.</p>
<p>Now let&#8217;s try moving the fish to a different position. Here&#8217;s how:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> fish;
&nbsp;
fish = <span style="color: #0033ff; font-weight: bold;">new</span> Fish<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>fish<span style="color: #000000;">&#41;</span>;
&nbsp;
fish.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">200</span>;
fish.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>;</pre></td></tr></table></div>

<p>Now test the movie, and notice that the fish has moved to the x, y coordinates of 200, 100. Pretty easy eh? I&#8217;m glad you think so, because now we&#8217;re going to make it move. You&#8217;re probably already familiar with frames, framerate and animating in Flash. To make the fish move, we have to make our code listen for each time Flash skips to the next frame. Even though you only have a single frame in your movie, Flash will signal each time it&#8217;s supposed to skip to the next frame. How often this happens depends on the movie&#8217;s set framerate. You can adjust the framerate in the properties panel. To get to it, just click somewhere on the stage. If you&#8217;ve somehow closed it, you can bring it back with Ctrl+F3.</p>
<p><img src="http://flashyard.com/blog/wp-content/uploads/2008/08/properties_panel.png" alt="" title="properties_panel" width="550" height="150" class="alignnone size-medium wp-image-66" /></p>
<p>The default framerate is 12, but when making games you should set it to 30 - 40 or higher so the game animations will be smooth. To make ActionScript listen for an event, we must use something called an <em>event listener</em>. Here&#8217;s the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> fish;
&nbsp;
fish = <span style="color: #0033ff; font-weight: bold;">new</span> Fish<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>fish<span style="color: #000000;">&#41;</span>;
&nbsp;
fish.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">200</span>;
fish.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>;
&nbsp;
<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p><code>Event.ENTER_FRAME</code> is the kind of event we want to listen for. You&#8217;ll se that as you write <code>Event.</code> Flash will give you a list of possible events to listen for. If you wanted to listen for a mouse click, you&#8217;d have to replace <code>Event</code> with <code>MouseEvent</code>. When you write <code>MouseEvent.</code> you&#8217;ll also get a list of possible events to choose from - CLICK being one of them. After we specify the event, there&#8217;s a comma and then the name of a function that we haven&#8217;t created yet. A function is simply a reusable block of code - some code that can be run over and over again. So: We&#8217;re going to make a function called <em>update</em>, and the event listener will run that function each time Flash skips to the next frame.</p>
<p>Hope that wasn&#8217;t too heavy for you. Here&#8217;s how we make the <em>update</em> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> fish;
&nbsp;
fish = <span style="color: #0033ff; font-weight: bold;">new</span> Fish<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>fish<span style="color: #000000;">&#41;</span>;
&nbsp;
fish.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">200</span>;
fish.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>;
&nbsp;
<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// any code we write inside this function</span>
	<span style="color: #009900;">// will be run each time flash skips a frame</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><em>update</em> is the name of the function, and in parenthesis is a variable that I named <em>e</em>, that&#8217;s going to recieve the event information from the listener. We&#8217;re not going to go any deeper into this in this tutorial, just know that it&#8217;s necessary, because if the information from the event listener has nowhere to go, you&#8217;ll get an error message.</p>
<p>Hang in there, only one line of code left until the fish will move. We want the fish to move a little to the right each frame. Remember the <code>fish.x</code> property? If we add a small value like <em>3</em> to this each frame, it means the fish will move 3 pixels to the right each frame. Eureka!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
15
16
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// any code we write inside this function</span>
	<span style="color: #009900;">// will be run each time flash skips a frame</span>
	fish.<span style="color: #004993;">x</span> = fish.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">3</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I suggest you try to copy the way I&#8217;m using TAB to make my code easier to read. It&#8217;s a common thing to do, and it helps a lot when you have a lot of code to read through. Now test your movie. It&#8217;s alive! Notice that the fish doesn&#8217;t stop when it reaches the edge of the stage, because we haven&#8217;t made any code for this yet. Let&#8217;s make the fish wrap around to the other side of the stage when it goes off the stage to the right. For this we need to use an <code>if</code> statement. Here&#8217;s how we&#8217;ll do it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// any code we write inside this function</span>
	<span style="color: #009900;">// will be run each time flash skips a frame</span>
	fish.<span style="color: #004993;">x</span> = fish.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">3</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fish.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		fish.<span style="color: #004993;">x</span> = 0;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Each frame, we ask if the fish&#8217;s x position is more than 550, which is the default width of our stage. If this is true, then we set the x position to 0. Let&#8217;s test the movie. The fish skips back to position 0, but it doesn&#8217;t look quite right because the fish just appears at position 0, it doesn&#8217;t look like it swims in from the left side of the stage. To fix this we have to change the code so the fish will snap to something like -70 (which is about the width of my fish) instead of just 0. Let&#8217;s change the code and test it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="actionscript3 actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">// any code we write inside this function</span>
	<span style="color: #009900;">// will be run each time flash skips a frame</span>
	fish.<span style="color: #004993;">x</span> = fish.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">3</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fish.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		fish.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">70</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Looks perfect! This should give you a basic idea of how ActionScript works. The most important things you should try to learn about are:</p>
<ul>
<li>Variables</li>
<li>Functions</li>
<li>Event listeners</li>
<li>If statements</li>
</ul>
<p> Try to read up on these things, and I hope you&#8217;ll come back later for more tutorials!</p>
<p>You can download the complete project <a href="http://flashyard.com/tutorials/programming_basics_done.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://flashyard.com/2008/09/programming-actionscript-3-for-beginners/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
