The+Very+Very+Very+Basics.

==

=Hey guys, welcome to the **basics section!!**=

Here, you may find some VERY helpful information to aid you in your beginner Flash programming endeavours. If you have ANY QUESTIONS please post them, and we'll see if they're worthwhile enough to put the answers into this section.

So, let's begin.

**Variables:**
Variables are pretty much the essence of programming. They're basically entities that you create to hold a variable value(essentially a value that may change later on in your program).

Variable names can be anything: tree, three, free, tea, key, knee, fee, etc. (It is unlikely that you would actually name variables like that.) **But** there are some symbols or names you can't put as your variable name. These are key words (names that are already used by flash. Ex: if, while, var, _root, mouse, function), and variable names beginning with numbers or symbols. You cannot use a space to separate two words for a variable name, an underscore can be used in place of the space. (Ex. Fanda Lan is wrong. You can put Fanda_Lan instead). It is highly recommended that your variable name should be detailed (like "height" and "angle") and not just 'a' or 'b' or "abc" because you don't want to forget what your variable represents especially when your code is 1000+ lines.

An example of a variable: code speed = 5; code So what I did there was **//declare a variable// named //speed// and gave it an //initial value// of 5.** Notice the **semicolon** at the end of the statement. Later on in the program, I may want to decrease the speed, and I could easily change it by simply typing: code speed = 4; code So now, the speed is 4. The **value has now changed.** If you want it to go back to 5, or change it again, you must **write another statement**. There are many ways you can change a value, and you can pretty much do all the things you've learned in Math. For example: code distance = 10; time = 5; speed = distance/time; code That was just simple arithmetic to determine the variable speed. Now, I could mess around with the distance and time values, and the speed will reflect these changes automatically. So right now, the speed would be 2 (distance/time = (10)/(5) = 2). If I did something to decrease, let's say, distance to 5. Then the speed would change to 1. See where I'm going with this?
 * You must have a semicolon at the end of every statement.** Or else it won't work.

**If/ Else if/ E****lse Statements:**
//If statements// are the next most important thing you can have in a program. It is what makes a program work. Example of an //if statement//: code if (speed>5) { speed = 5; } code What this //if statement// does is set a maximum value for the variable speed. If the speed ever gets higher than 5, then it will be readjusted to be 5. For instance, if speed ever becomes 10, it will automatically be changed to 5 when that piece of code is executed. Another thing that you need to know is that with every //if statement//, there can also be an //else statement//: code if (speed>5) { speed = 5; } else { speed = speed + 1; }

code Here, if speed is not greater than 5 the //else statement// would be executed and the speed would increase by 1. Remember there can only be ONE //else statement// attached to the initial //if statement//. So...if the initial value of speed was 2, it would become 3 when this piece of code is executed. So now you ask: What happens if I want to have more conditions to test for? This is where the //else if statement// comes in. code if (speed>5) { speed = 5; } else if (speed<0) { speed = 0; } else { speed = speed + 1; } code Notice that the //else if statement// is between the //if statement// and the //else statement//. You can have infinite //else if statements// but remember that the code gets executed **in order from top to bottom**! So let's say you write: code if (speed>5) { speed = 5; } else if (speed<10) { speed = 8; } code If speed was 7, what would its final value be? 7 is both greater than 5 and less than 10. But remember that it's executed from top to bottom. So it runs if (speed > 5), and if that's **true** (which it is), it will **skip the rest of the piece of code!!** This means it will ignore the else if and else statements that come after it. Conversely, if speed was 4, its final value will become 8. Wonder why? Because the first if statement turned out false, so the program moves on to the subsequent else if statements. Understand, class? If not, **post questions.**

=﻿Your First Days in Flash:=

So now that you've learned a bit about programming and the logic associated with it, I think it's about time we moved onto **Flash ActionScript 2.0** specifically. The sections we've put up above are pretty much universal, and apply to pretty much every type of programming that you're ever going to do. Now, to begin with Flash...

How to start:
This can be expressed simply as a list of steps:

1) Create any shape you need, wherever you need it, by using the **Drawing** tool. 2) Switch to the **Selection** tool (looks like a black arrow) and select your shape. 3) Press Ctrl and the letter "G" simultaneously (at the same time, or hold one while pressing the other) to group your shape. ***If you do not do this your shape will remain as a fill and a border!! You don't want that!!!** 4) Press F8 ONCE. This opens up the "Symbols" menu. Name your object, and select it to be converted to a **Movie Clip. You also have to export to ActionScript.** 5) Press F9 ONCE. This opens up the "Actions" menu. 6) For Flash MX users: **you may need to select something like "Expert Mode" in the bottom right-hand corner of your "Actions" menu. Otherwise it will only allow you to use pre-made code snippets.**

Now you can start coding!!

Setting up your code:
In order to start coding, or at least get your code to start doing stuff, you must understand some things...

code onClipEvent (load) { speed = 5; } code This is the first thing that you type in your code most of the time. Only the code in between the curly brackets "{}" is a part of the event. The curly brackets determine which pieces of code are included in the functions of this event. For the sake of taking up space and showing an example, I've typed up "speed = 5;" This is included in the event functions because it's inside the curly brackets. Essentially, any code you want in the event must be in the curly brackets.
 * Events:** I guess I'll give an example first:

code onClipEvent (load) { } onClipEvent (enterFrame) { } onClipEvent (mouseDown) { } onClipEvent (mouseUp) { } onClipEvent (keyDown) { } onClipEvent (keyUp) { } code There are many events that you can check for and here are the most useful ones:
 * More Events:**
 * //load// means the code gets run once when it first loads the program.
 * //enterFrame// means that the code gets run ___ times per second. The frequency can be changed by the **FPS** (frames per second **not** first person shooter) settings. (Never leave it at 12 FPS)
 * //mouseDown// is triggered when the **left mouse button** is pressed down and it does **not** get triggered anymore while the button is held down. Flash does **not** support right clicks.
 * mouseUp is triggered when the left mouse button is released
 * //keyDown// is triggered when a key is pressed down. Its code does not get repeatedly executed if the key is held down.
 * //keyUp// is triggered when a key is let go.

=Loops:=

Loops are pieces of code that get executed repeatedly until a certain condition is met. If the condition is never met, the loop will be an infinite/endless loop and it may lead to your program not responding. There are 3 types of loops:

1. //for loop// code for (a = 0; a<5; a++) { }

code The code above is called a //for loop// and it works by initializing a variable, then setting a limit to the variable, and finally changing the variable once per cycle. The code runs until the condition is met and in this case, the variable 'a' will increase by 1 until it gets to 5. Then the loop stops running.

2. //while loop// code while (a<5) { } code This code is called a while loop and it requires a variable to set before running the loop. Then it tests for the condition and runs until the condition is false.

3. //do while loop// code do { } while (a<5); code This code is similar to the while loop but it runs the code in the brackets once before testing for the condition.

=Arrays:=

An array is like a string of variables that have similar content and are grouped together under one name. They are very useful if you need to declare many variables. Instead of making different names for each and every one of the variables, you can just have them under one name in one line.

Example: code speed = new Array(20); code Here I have created an array of speed variables. The data that will be stored will all be speeds of some object and I can store up to 20 speeds in this array. The best part of arrays is that they can be efficiently manipulated with //for loops//.

In order to manipulate an array, you should use the //for loops//. As to how, here is an example: code for (a = 0; a<20; a++) { speed[a] = 5; }

code (Note: Arrays should always start at 0 to make your programming life easier! I will now explain what this means.)

Here, in the example, I have an array named //speed//. Then, I used a //for loop// to cycle through all 20 variable in the array. Since the //for loop// exits after 20 cycles, it would have assigned values to all of the variables under the name //speed//. Notice that 20 is not included in the //for loop//. That is because array indices (plural of index) always start at 0 and ends at 1 before the stated length. In this case, the index of the array //speed// go from 0 to 19.

Many of you will now think: Arrggh it is too annoying to have to remember the lengths of all my arrays. But there is a function called: //array.length// Here is how you use it: code for (a = 0; a<speed.length; a++) { speed[a] = 5; } code Instead of limiting 'a' to less than 20, this function finds the length of the array for you! This is extremely convenient when half-way through making your program, you decide to change the length of some arrays. If your for loops already use the //length function//, then you save lots of time. (Or else you need to manually change the conditions of every single //for loop// that uses that array.)

To assign values in the array, the code format is: //variablename[index]//. In this case, my variable name is //speed//, and my index is the value 'a'. Therefore all 20 variables will have a value of 5.

**Movie Clips**
The shapes that you guys drawn are called movie clips. The position of a movie clip can be controlled using the properties //movieClip._x// and //movieClip////._y//. The value of these two properties are the numbers of pixels from the top-left corner. Here's an example: code this._x = 100; this._y = 50; code In this example, the current object will be 100 px from the left and 50 px from the top.

For more information on movie clip properties: go to this link: []

(stay tuned for more next week or when someone else edits this page)