ActionScript:Introduction

From Wikiversity
Jump to: navigation, search

Welcome to the first ActionScript lesson, part of the ActionScript program here at Wikiversity. Instead of creating one of those silly "Hello World" introductions, I'm going to jump right into the structure and basics of ActionScript.

Basics of ActionScript 2.0[edit]

Functions[edit]

If not for functions, ActionScript 2.0 (which will be from here on refered to as AS) would be nearly useless (see Event Handlers below). A function is essentially a set of commands you are giving to Adobe Flash. You can do pretty much anything in a function. Writing a function is simple:

function function_name (parameters) {
   statements
}

You'll notice five essential elements to creating a function. First, you must write function, which lets AS know that you are creating a function. The next three elements must be included for the function to be created properly.

Let's jump into it and write a line of code now. In the Action Panel write the following:

objname.onLoad = function()
 {
 trace("hello world");
 }

Function Name[edit]

The function's name is arbitrary. You can call a function whatever you want, as long as you adhere to these rules:

  • The function's name cannot begin with a number!
  • The function's name cannot contain any other symbol than a letter, an underscore or a number.
  • The function's name cannot have any spaces. While new_function would be ok, new function would generate an error.
  • The function's name is case sensitive! Whenever you use a function, every character must be the same. So trying to use FunctionOne when it's actually called functionone will not work.

Parameters[edit]

Parameters are an incredibly useful part of a function, but for now we won't be going into that. They pass on information to the function when it is called. Sound complicated? That's why we'll be doing it later! They are always contained in parentheses. If you do not want to use parameters (we won't be for now), you still have to leave in the empty parentheses, like this (compare above function):

function newfunction () {
   statements
}

Statements[edit]

Without statements, the function would essentially accomplish nothing. If a function is a shopping list, then a statement is something you need to get. Statements are separated using a semi-colon (;). Using the example of the shopping list, most functions look a bit like this:

function shoppinglist () {
   bananas;
   oranges;
   bread;
   milk;
   etc...;
}

Curly Brackets[edit]

Finally, the curly brackets ({}), also known as curly braces. They make the difference between the function you are creating and the rest of your code, kind of like a fence makes the difference between two properties. Without brackets, you are going to get a pile of syntax errors in your code! They must be placed after the function name and parameters, and on a new line after you are done writing statements, like so:

function functionname () {
   statements;
   statements;
   statements;
}

Calling a Function[edit]

So there you go, now you know how to write a function! As I mentioned above, we will learn how to use parameters later on. Most Flash programs contain a handful of functions, depending on how many different sets of actions need to be used. In AS, if you write statements outside of a function, they are executed immediately, which is not always what you want. So what's the point of creating a function? So you can call it whenever you want! Calling a function is also simple.

functionname();

To call a function, you must first write the function's name, then it's parameters within parentheses. Just like when you were writing a function, you still need to add the parentheses even if you don't have any parameters. You can call a function whenever you want: you can even call a function within another function, kind of like this:

function firstfunction() {
   //do a;
   //bunch of;
   //stuff;
   secondfunction();
}
function secondfunction() {
   //do a;
   //bunch of;
   //other stuff;
}
firstfunction();

In the above code, AS will essentially run the firstfunction, which will do a bunch of stuff, and then run secondfunction, which will do a bunch of other stuff. Once we get into Event Handlers and Parameters, you'll see how this can be very useful.

Variables[edit]

Variables are, in their most basic form, containers for information. Creating a variable is simple:

variablename = value

As you can see here, all you need to do is give the variable a name, put the = sign, and give it a value! The variable's name must be written like a function: don't start with a number, only characters, numbers and underscores are allowed, no spaces, and remember, it's case sensitive! A value is whatever the variable contains. A variable can contain four different things: an array (a list of information), a number, a string or an object.

Also worth noting is the variable declaration syntax of Actionscript 3.0.

var myText:String = "this is the content";
var myNum:Number = 3.14;
var myArray:Array = ["apple", "pear", "orange"];
var myObject:Sprite = new Sprite; 

These are different from loose declarations in that they begin with var then their name, myText for example, then a colon and their data type, lastly an "=" and then their value.

Strings[edit]

A string is basically a bunch of words. Strings always have "quotation marks" around them. Strings are usually used to display information. They are not commands within AS, but instead, literal words. For example, if I write:

var1 = 3.14159
var2 = var1 + 5

AS will recognize var2 as being worth 8.14159. On the other hand, if I write:

var1 = "3.14159"
var2 = var1 + 5

AS will recognize var2 as being worth 3.141595. Instead of adding 5 as a value, it is literally added to var1. As you continue to use ActionScript, you will realize how important it is to make the difference between strings and everything else.

Integers[edit]

Integer is simply another word for number. As in the example above, variables can contain integers for various reasons. Integers are never surrounded by anything alone. Though integers are used surrounded by other objects. If you add to variables containing integers together, you will get the mathematical sum of those two numbers. For example:

var1 = 5
var2 = 3
var3 = var1 + var2

var3 is equal to 8.

Other Variables[edit]

Variables can contain other variables, as in the above examples. When you create a variable that is equal to another variable, the new variable simply copies the old variables information. So if I write:

var1 = "chocolate"
var2 = var1

var2 is equal to "chocolate". Since we usually separate variables into strings or integers, you can make variables interact with each other when creating a new variable depending on what they are. Integer variables can be added (+), subtracted (-), multiplied (*) and divided (/), while string variables can only be added, or Concatenated, onto each other. For example:

var1 = 3
var2 = 5
var3 = var1 * var2
var4 = "hello my name"
var5 = "is Lollercakes."
var6 = var4 + var5

var3 will be equal to 15, while var6 will be equal to "hello my nameis Lollercakes." Note that there are no spaces automatically included when adding variables. To accomplish that, we'd have to add a space within the string declaration like this:

var1 = "The battle"
var2 = " of the Bulge."
var3 = var1 + var2

var3 is therefore equal to "The battle of the Bulge."

Methods[edit]

Flash is an object-oriented program. Most of the ActionScript you will write will be used to manipulate objects within Flash, whether they're text, objects drawn using Flash, or animations within those objects. Methods are object-oriented actions, kind of like functions already included in Flash. Here are a few examples of methods:

  • String.split(delimiter:String, [limit:Number]) -- Splits a String object into substrings by breaking it wherever the specified delimiter parameter occurs and returns the substrings in an array. If you use an empty string ("") as a delimiter, each character in the string is placed as an element in the array.
  • Sound.start([secondOffset:Number], [loops:Number]) -- Starts playing the last attached sound from the beginning if no parameter is specified, or starting at the point in the sound specified by the secondOffset parameter.
  • Movieclip.duplicateMovieClip(name:String, depth:Number, [initObject:Object]) -- Creates an instance of the specified movie clip while the SWF file is playing. Duplicated movie clips always start playing at Frame 1, no matter what frame the original movie clip is on when the duplicateMovieClip() method is called...

As you can see, each of these methods have parameters (ah ah, you'll learn about those soon!), just like functions. The difference is that, while a function can simply be called whenever and wherever, most methods need to be based on an existing object. So really, methods are like functions created by Flash and usually must be run with an object, while functions are user-defined. For example, if you look at the second example shown above, Sound.start(), replacing the word Sound with the name of a sound object created in Flash will make that sound object play whatever sound has been attached to it. There are three major types of methods.

Return Methods[edit]

When you need to get information concerning a certain object, you use a return method. Return methods can be used to create new objects based on old ones, or to create variables that contain information about objects. Let's look at one of the examples above, which is String.split().

When you use String.split(), you are essentially taking a variable and separating it using a delimiter which you define in the parameters section. Although this sounds confusing, the result is essentially an array, which is like a list of variables. The point is, when you use the String.split() command, it does NOT do anything to the variable itself... it simply sends information to wherever you need it about the consequences of whatever the method is. Commonly, String.split() would be used something like this:

string1 = "Bananas, oranges, apples"
array1 = string1.split(",")

In this scenario, array1 would be equal to bananas, oranges, apples. We're going to learn more about arrays later on, but for now all you need to know is how return methods work!

Effect Methods[edit]

Contrary to return methods, change methods actually create a change within the object. We'll use the example of Sound.start() for this one. When using Sound.start(), it does something to the object (which is a sound file): it starts playing it! Effect methods are also used in a variety of other contexts, such as when drawing an object within Flash or starting or stopping animations. Effect methods are simple, truth be told.

Constructor Methods[edit]

Constructor methods, as the name implies, "construct" objects, sounds, variables, arrays, etc. within Flash. When creating a function, you are using the function constructor. When creating a new Sound object or Movie clip, it looks like this:

mySound = new Sound();
_root.createEmptyMovieClip("name", 1);

Constructor methods are useful for creating content within a Flash application that is context sensitive, such as a user-defined sound effect or drawing. Whether or not you use constructor methods depends on what you're trying to accomplish in Flash, although most user-friendly, context-sensitive Flash applications (what Flash is truly made for!) make extensive use of constructor methods.

Advanced ActionScript Structure[edit]

Parameters[edit]

Finally! Parameters can be used in a number of different situations: when writing and calling a function, when using a method, etc. To be simple, parameters hold context-sensitive information that is passed on to a function or method when used. Parameters are kind of like variables: they hold information that can be accessed at any time.

The way a parameter works is the same with both functions and methods. Many functions and methods need parameters to run. For example, if you are using a createEmptyMovieClip method, you need to specify what the new Movie Clip that is created will be named, and on which level in AS it will appear. The parameters are contained in parentheses. When writing code, you can check out the AS Reference to know which information is passed on to which parameter. Using the example of createEmptyMovieClip, here is how it is usually written:

_root.createEmptyMovieClip(parameter1, parameter2)

Parameter1, in this case, is the name of the new Movie Clip. Therefore, it has to be a string. Parameter2 is the level on which the Movie Clip is created, so it has to be a number. Filling out the parameters looks like this:

_root.createEmptyMovieClip("myMovieClip", 5)

Remember that all different methods have different parameters that you have to check using the AS Reference!

Now, if we go back to user-defined Functions, you'll notice that you can define your own parameters for your functions. This is a very powerful feature in Flash! Here's an example of a function that uses parameters and is called with parameters.

function myFunc(param1, param2) {
   trace (param1)
   trace (param2)
}
myFunc(3, 16)

What this little piece of code does is create a function with two parameters called param1 and param2, then calls the function and changes the value of param1 to 3 and param2 to 16. So using parameters is sort of like creating an infinite number of variables inside of a function, since they can be called inside the function at any time. Here's another example using both a method and function parameters.

function createMC(name, depth) {
   _root.createMovieClip(name, depth)
}
createMC("myMC", 5)

This creates a Movie Clip with the name myMC and a depth of 5.

As a final note, remember to keep in mind three things when creating function parameters:

  • Each parameter must have a different name which adheres to the usual rule of creating variables and functions.
  • Although it is not necessary to define all parameters within a function (for example, the function has three parameters but only two are defined when it is called), you cannot define more information than the function can hold (for example, the function has two parameters and three are defined when it is called).
  • The parameter cannot be accessed outside of the function.

Questions? Need some extra help? Can't figure something out? Then go to: Talk:ActionScript:Introduction