AS: Beginning
Though AS:Main has become an invaluable asset to actionscripters I think the hardest thing to do is actualy begin programing.
How do you know where to start? Well this AS:Thread (or probally series of threads) is for those of you whom are confused by
the hundreds of topics explaining every little thing in detail. This thread will give you a basic understanding of
actionscript and point you in the right direction for expanding your skills.
Your first step
Okay first thing is first open flash. I am using Flash 8 for this but Flash MX 2004 should be fine as well as Flash MX. I do
suggest you upgrade to Flash 8 though.
Okay once youve opened flash create a new document and saved it; call it "my_flash" for now). Next open the actions panel by
pressing F9. Okay here you will see 4 boxes above a window where you can input your code. From left to right they are:
1) Add new Item to script - we will not be using this because all it does is types the command for you
2) Find/Replace - This can be used to quickly find and/or replace keywords within your code
3) Target path - This is very useful as it allows us to find the target path between 2 movieClips relativly or absolutely
4) Check syntax - This is the most useful thing up there because it tells you if your code is wrong and whats wrong with it
5) Auto format - Very useful it neatens up the code
6) Show code hint - Very useful when you forget the input parameters for functions
7) Debug options - This is just for editing breakpoints etc and we wont be using it in this tutorial
Okay so now we are familiar with the actions panel (Fig 1) we can begin with some AS
Your first lines of code
Okay now click on the first frame and go onto the actions panel. Type in:
trace ("hello world")
Press Ctrl+Enter to test your movie and how exciting you should get a window saying "hello world"
Excited? Lets go through the code
the trace function
Trace is a function used only when debugging your flash document and wont show up in the final .swf file. Trace basically
displays whatever you tell it to in the output panel. In this case we put in "hello_world" which is enclosed by "" meaning it
is a string.
Difference between strings and variables in the tracefunction
You can trace strings which are always constant ie "hello_world" would output hello_world or varables by not using "". Lets
take a look at variables
Variables
When declaring a variables you must give it 3 things.
- A name
- A type
- A value
and we use the keyword Var to declare a variable
So in frame 1 put
var message:String = "hello_world"
See what we did there
we gave it the name message the type String (you have to put a : after the name) and gave it the value of "hello_world". So...
var message:String = "hello_world"
trace (message)
When we tested it it would output
hello_world
Are you amazed?
+-/*=
We can also use +, -, / and * to manipulate variables. + is the most important one for Strings because you could do
var message:String = "hello_world"
trace (message + " this is my first bit of AS)
And that would output
"Hello_world this is my first peice of AS"
Numbers
Now lets say we had
var num1:Number = 3
var num2:Number = 5
We can use the +, -, / and * to make it do different things
for example
num1 + num2 = 8
num1 - num2 = -2
num1 / num2 = 0.6
num1 * num2 = 15
And we could even trace them or better still put them into another variable by doing something like the following
var num1:Number = 3
var num2:Number = 5
var num3:Number = num1 + num2
Quick note: Remember that you dont have to assign a value to a variable when it is declared you can always just put
var goatse:String
Okay so now we know alot about variables and strings and how to use trace to debug your code I'll let you have a play with
those until next time
To expand on your knowlage of these things here are some links you should study before going on to part 2 of this tutorial
Debugging syntax
Maths basic
Variables
Fig 1 below