Programming with ActionScript 3: Flashosophy Cram Session Part I (Variables and Math)
Introduction: Programming Fundamentals
This tutorial is for absolute beginners in the programming world. Action script is similar to Java and C++, but easier in some ways because it has less strict syntax. It would take way too long to teach you WHY things work the way they do. I will teach you what you need to know to get started and HOW to do it. My tutorials are all written from memory so the topics you learn appear without any specifc order.
Section 1: Programming in Flash CS3 Timeline
1) Open Flash CS3 and create a new Flash File (ActionScript 3.0)
2) Click on frame 1 on the timeline

3) Press F9 to open up the actions panel
4) Enter
trace("Hello World");
in the actions panel. I like to turn OFF script Assist and get rid of the left side panel. I recommend you do the same.
5) Press Ctrl-Enter to test run the movie

6) You've successfully created your first Flash program!
Section 2: The Basics of Variables
As you've learned in section 1, the keyword "trace" is a function that spits out something in the output panel. This is a very useful function to use when you want to find out if something is right. You can trace all variables, not just strings. This brings us to the next topic; types. Here are a few basic types of variables you can use: unsigned integer, integer, number, boolean, and string.
Unsigned Integer - This is a number that has a range from 0 to 4294967295 without decimal places.
This type saves memory; useful when you know something won't be negative.
Example: 123
Integer - This is a number that has a range from -2147483648 to 2147483647 without decimal places.
This type saves more memory than Number.
Example: -123
Number - This is a number that has a range from 4.9406564584124654e-324 to 1.79769313486231e+308.
This type is used when there are values that require decimals.
Example: 1.23
Boolean - This variable is either True or False. This is used a lot when there is a condition (if this is 'true', then do that).
You can also use an integer of 0 or 1 as substitute for booleans.
Example: False
String - You can think of String as a text. These texts are specified by surrounding them with quotes.
Example: "Hello World"
Put the following in your actions panel:
//----------------------------- start code ----------------------------
/*
Anything in between the slash-star in front of anything indicates that you are making a comment.
Comments are ignored by Flash's compiler; this does not affect the code in any way.
They are useful for developers that like to document their code.
Text after a double slash puts a comment on the current line.
Doing a star-slash ends the commenting block.
*/
var age:uint = 18; // you can put comments after a line of code like this too
var hair:int = 10000; //notice there is no comma for ten thousand. It is 10000 not 10,000.
var cash:Number = 9.30; // actionscript is case-sensitive. This means "Number" is different from "number" and "Cash" is different from "cAsH"
var rich:Boolean = false;
var jobTitle:String = "Newb";
trace(age);
trace(hair);
trace(cash);
trace(rich);
trace(jobTitle);
// you are adding all the variables in one line of output
trace( age + hair + cash + rich + jobTitle);
//you can mix custom strings with existing variables in a trace
trace("Age: " + age + " Hair: " + hair + " Cash: " + cash + " Rich: " + rich + " Job: " + jobTitle);
// the \t is called an "escape sequence", it simulates a "tab"
trace("Age: " + age + "\t Hair: " + hair + "\t Cash: " + cash + "\t Rich: " + rich + "\t Job: " + jobTitle);
//----------------------------- end code -----------------------------
If you do a crtl-enter at this point, you will get the following output:
18
10000
9.3
false
Newb
10027.3Newb
Age: 18 Hair: 10000 Cash: 9.3 Rich: false Job: Newb
Age: 18 Hair: 10000 Cash: 9.3 Rich: false Job: Newb
Remember when I said that action script is not as strict as other languages? Well you can actually do this instead:
var age = 18
var hair = 10000
var cash = 9.30
var rich = false
var jobTitle = "Newb"
Flash will automatically detect what types are being used. You don't even need a semi colen at the end. Sure that looks cleaner and saves you a lot typing, but you get a performance hit because of this. However, it doesn't matter because these tutorial programs you are making are short. And about the semi colen being at the end of every line of code, it is good practice to use it because you can do this:
var age = 18;
var hair = 10000; //you can have multiple things on a line
var age = 18
var hair = 10000 //you will get a compile error and your program won't work
Great. Lets move on.
Section 3: Escape sequences
If a string is surrounded by quotes, how do you make a quote in a string? For example, how do I show in the output: "Hello" instead of 'hello' ? An escape sequence is a way to get do this. Escape sequences are initialized with a slash \.
\t
The slash t simulates a tab
\n
The slash n simulates a next line
\\
The slash slash simualates a slash
\"
The slash quote simulates a quote
trace("\tTab \n \\ \"");
Output:]
Tab
\ "
Section 4: Math
The basic operations: add, subtract, multiply, divide.
Math Rules
- when you add, subtract, multiply,or divide an Integer with an Integer, you get an Integer
- when you add, subtract, multiply,or divide an Integer with a Number or vice versa, you get a Number
- when you add two or more Strings together, you get a String with all Strings added
Addition: Symbol +
Subtraction: Symbol -
Multiplication: Symbol *
Division: Symbol /
Example:
trace(1+1) //outputs 2
trace(1-1) //outputs 0
trace(2*3) //outputs 6
trace(3/2) //outputs 1.5
trace("1"+"1") //outputs 11
var myApples = 3;
var yourApples = 2;
var totalApples = myApples + yourApples;
trace(totalApples); //this would output 5
Flash also has a Math library that allows you to create random numbers, sine , cosine, tangent, etc. Press F1 and search for Math to see what the package offers.
Example:
trace(Math.sin(45)) //outputs 0.8509035245341184 because it is in radians by default.
trace(Math.random()) //outputs a Number between 0 and 1
You can increment and decrement numbers on a variable.
Example:
var myScore:int = 10;
myScore++; // the ++ means increment the score by 1 (so it is now 11)
myScore--; //the -- means decrement the score by 1 (so it is not back to 10)
myScore += 5; // the += means add that much to the current variable. (it is now 15)
myScore -= 3; // it is now 12
myScore *= 2; // the results are now multiplied by 2 (it is now 24)
myScore /= 8; //divide current value by 8, this gives 3
trace(myScore) //this outputs 3
Ending Summary
The trace function is your best learning tool as a beginner. It is also very useful to find out why things are not working because you can put a trace statement anywhere.
You are now +1 step closer to being a pro-programmer.
Published On: July 11, 2008
Next Chapter: Flashosophy Cram Session Part II (Conditions & Operators)
(c) 2008 Flashosophy