The Best Variables and Functions
Actionscript Tutorial
Variables
It is about time you learn about Actionscript. I will teach you in a way that you haven’t heard.
First, what comes to mind when you think, “script?” When I think of the word script I think of a play or a movie script. This is what Actionscript is like. Every movie script has actors and actions and objects and cues and timing and directions, and so does Actionscript. So, this is definition of Actionscript:
Actionscript(is like)-- a bunch of directions given to actors, actresses and objects that altogether make a full movie.
Remember this.
So, you can’t have a movie without actors. The actors of an Actionscript are called variables.
Variables-- the actors of Actionscript
Remember this.
A play script may look like this:
John: {enters from screen left} Did he come this way?
The actor’s name, in the movie, is John. He is told, by the script, to enter from the left of the screen. He is directed to say, “Did he come this way?” When the director says, “action,” John knows what to do simply by reading the script.
A variable’s script in Actionscript can look like this:
var john: Number=3;
trace(john);
The name of our actor (variable) is john. “john” is a number. “john” is the number 3 . The direction given to “john” is to trace. But trace what? Well, if “john” is the number 3, then the script is telling him to trace himself. And so, the number 3 will show up when the movie starts. It will not be much of a movie though. Let’s make another actor:
var jWords: String=“Did he come this way?”
This actor’s name is “jWords.” “jWords” is a string (a string is a bunch of letters strung together). And “jwords” is the string, “Did he come this way?” “jWords” is not a human actor, but it has a role in our movie, sort of like the text in a movie title.
Strings are put in quotes(just like quotes in a book).
So, when making a script, we first want to make a new actor. Then we name the actor, then we tell the actor what type of part or role he will play, and then we give him his lines. Depending on what type of role the variable will play, his lines may vary. If he is playing the role of a rock, not much will need to be typed for him. Let’s create some more variables(actors):
var numberMan: Number= 100;
var stringWoman: String = “I’m gonna tie you down!”;
var labooleanDynomite: Boolean = false;
var arrayOfGoldenSun: Array = [“do”,“re”,“me”,“fa”,“so” ,“la”,“tee”,do”];
(you can read the colons as, “plays this role...” Number, String, Boolean, and Array are types of roles variables can play).
(notice that each part of the name starts with a capital letter, except the first part. This is how variables should be named. It helps us tell the where the next word is, since variable names have to be one long word.)
A Boolean is either a liar or a truth teller, either true or false. But in actuality A Boolean actor turns something on or turns something off. True means on and false means off.
The next tutorial is on Arrays.
Number, String, and Boolean are only three types of roles a variable can play. These types of roles are called data types ,and there are many other types of roles that a variable can play.
var myFavorite:String="J.E.L.L.O";
var thatIsNot:Boolean=true;
var myFavNum:Number=4;
var theAlphabet:Array=["a","b","c"];
var txtHello:TextField = new TextField();
var theObject:Object= new Object();
var bruceLeeKickClip:MovieClip=new MovieClip();
Other data types are:
int
Null
uint
void
So, a variable can be anything we want it to be (A variable is something that is able to vary). The best actor is an actor that can be whatever you want him or her to be, right?
Note: there is a shorter way to set actor roles:
actor = “joe”;
This sets a variable named “actor” to a string named “joe”
Or
num = 3;
If you trace num:
trace(num);
//3 will be traced
Notice there are no quotes (num is not a string, it is an actor that is a number 3)If it were:
trace(“num”);
Then ‘num’ would be traced, as a string.
The ‘trace’ function is used to check stuff usually. And now for functions!
Test: Make some cool variable names. And give them roles (one of the ones mentioned)
var weAddaBabyEetsABoy: String= “waaaaaaaaa”;
var turnThoseLightsOff: Boolean= false;
var howManyLicksDoesItTakeToGetToTheCenterOfATootsiePop: String= “none”;
(the semicolons are like periods in a sentence)
Functions
Mercifully, most actors can move and walk and talk on their own, but there are cases when this is not the case, and so, these other types of actors, (babies, dogs, trees, robots) must be directed after another fashion. As long as we can find a way to have a baby cry when we want it to, to have a dog bark when we cue it to, and to make a robot speak when we push a button, we can be safe to say that our movie would be less stressful to make. This is where functions come in handy.
Functions are somewhat like variables. Functions are like mini scripts that can have everything that the script can have inside of them. Functions are like on-the-spot teleprompters. When breaking news comes into the news station, the planned script is abandoned and the teleprompter is read.
Functions-- the teleprompters of Actionscript
Remember that.
Functions can look like this:
function mrFunction ()
{
stuff
more stuff
return value (special stuff)
};
Don’t cry. I will explain. So, you have a new teleprompter. You give it a name just like you do variables. The parenthesis are like stage directions of a movie script. And inside of the braces is the teleprompter stuff. All teleprompter stuff must go inside braces. The teleprompter can only know what to display if the stuff is inside of its braces.
The cool thing about functions is that they can be reused over and over again to flash the same information without another human having to relay the message. The same information can be read over and over again with no change (people get stuff mixed up all the time). This is the good thing about functions.
When you hear a programmer person saying “call the function” or “call a variable”. Think of it in literal terms. You have to call an actor to the stage before he will know it is his time to perform. The actor then, knowing his role, will carry out the list of directions according to his or her script.
One last question you may ask is, “How do you call a function in Actionscript?” Like this:
mrFunction();
think of the two parenthesis as a mouth
There are two well-known pre-made functions:
play();
and
stop();
Though these are easy to call, the stuff inside of the stop and play functions may not look so simple. However, the stop and play functions are pre-made, so we don’t have to worry about how to make them.
That is the basics of Variables and Functions!
More on Functions
Typing “//” before a line makes that line a comment. Comments don‘t show up in the movie, sort of like when a teacher writes all over your rough draft, but the final draft doesn‘t have all those red mark comments. Comments are like directors comments on a movie script that are written on the script, but not in the movie (the actor doesn’t read, “John goes to the table and sits down.”) That would be silly.
This section for the people who listened a little in math class
If you have ever seen, “f(x)” (this is read, “function of x” ), then you may be able to understand what can go into the parenthesis of a function. In f(x), ‘x’ is the argument of the function. We can take any value and put it into the “f” function and the ‘f’ function will return something to us. It’s like if we put a toy into a dog function and get a wet slobbery chewed piece of yucky plastic. Or we could put a fruit into a juicer function and get fruit juice. Now, looking at an example:
apple = "red";
//a variable named apple that is a string , 'red'
function blender (apple)
{
trace (apple+"juice");
}
trace (apple);
//traces “red”
blender ("orange");
//traces “orange juice.” We switched out apple for orange, and when we called the blender funtion we got orange + //“juice.”
Here, I took an orange and put him into the ‘blender’ function. Sort of like f(orange). So, I sort of switched out the apple for the orange. This is what they call passing a variable. The reason ‘apple’ is in the parenthesis of the function is because this is the thing we are going to switch out, just like we do the ‘x’ in f(x).
It’s like we are switching our actors, where the apple is the actor in the script at first, and we pass another actor to act in place of the current actor.
The blender function traces “orange juice!” When apple is traced outside of the function, “red” shows up in the movie. This is because the apple of the blender function takes on another role as long as it is in the blender function, but out side of the blender function, it is just a plain red apple(hasn’t been crushed).
Notice that ‘apple’ is not put in quotes.
If you don’t understand just start again and read until you do. You will understand it eventually. I will give you one more example here:
paper=“paper”;
function creatingMachine(tree)
{
trace(tree+“created”);
};
creatingMachine(paper);
//traces “paper created”
I made a machine that turns trees to paper. Trees were in the machine at first (need something to switch), then I switched tree out for paper, which is the string, “paper” .
Just a reminder, you have to call a function in order for it to act. Remember that the stuff of the function has to go inside the braces, so if it is not inside of the braces, it is not a part of the function.
We called the creatingMachine function and put paper into the parenthesis where there was a tree at first.
Test: Make your own function!
function blender (fruit:String)
{
trace (fruit+" juice");
}
blender ("orange");
//orange juice
Notice the fruit:String part. You see that ‘fruit’ is an actor and his role is a string. In the blender function, fruit plays the role of a string, and the only type of actor (variable) that can be passed into the function is another string. Now, you can have more than one actor in a function script:
function makeEmFly(boy:MovieClip,girl:Number,man:Object,woman:String)
{
trace(“fly” + boy + girl + man + woman )
}
So if we called the function, any actors or actresses we choose to swap in or out will all fly, but we can only switch 'boy' out for other MovieClips or 'girl' our with other Numbers, or 'man' out with other Objects, or 'woman' out with other Strings.
makeEmFly(boyfriend,girlfriend,manly,womanly);
huh?
of course, we would have to make these actors exist before we could call them:
var boyfriend: Number = ...
var girlfriend: MovieClip = ...
var manly: String = ...
var womanly: Object = ...
these are the actors, outside of the function that we can put in place of the other actors.
(who got the joke?)
joke: boyfriend goes with girl. Girlfriend goes with boy. Manly goes with woman, and womanly goes with man.
One last thing about functions. Just like variables have types of roles, functions can have types too. Scrolling up you will see a type named ‘void’.
Void is a type of function that does nothing of itself, it just does its task and that is it, it doesn’t return anything(remember the dog function). The types of a function are put after the parenthesis:
function dogFuntion():void{};
function numberFunction():Number{};
function functionOfString():String{};
function theLieingFunction():Boolean{};
In order for these functions to return something to you once they have done their duty, you have to tell them to, or else they will just instruct the actors and stop.
function dogFunction():String
{
return trace(“chewed piece of junk”);
}
This function not only chews a piece of junk, it returns a chewed piece of junk.
And we can switch stuff in and out of this function:
function dogFunction (a:Object,theShoe:String,myTelephone:Number):String
{
return trace(theShoe + myTelephone + a.name + “is eaten too”)
;
}
Don’t worry about a.name, I will tell you all about properties later. This is just a goofy example.
(who got the joke?)
joke: my shoestring + my telephone number + a dog + is eaten too.
EXTRA INFORMATION
I would like to post some more technical descriptions so that you can check your understanding of variables and functions. (from 3dfa, a free flash program like Adobe). Goes from start to end.
start------------------------------
You can define a function to perform a certain task, and then call that function at any time from any other place in the movie. You don’t need to rewrite the same lines of code over and over again.
Each function must have its own unique name. A function name can be made up of case sensitive letters, numbers and underscores. The first character must be a letter or underscore.
A function can be defined by the following code.
function function_name(argument1,argument2,…){
line1
line2
return value
}
Where
Function_name is the name of the function.
An optional list of arguments can be given, separated by commas. These arguments must be variable names. They will be assigned with the values that are passed to the function when it is called.
The function can contain any number of instructions
The return statement is optional
The code contained in a function definition will not be executed unless the function is called.
A function can be called using the following code. The brackets must always be written, but the values are optional.
v = 0;
change_v();
function change_v (){
v = 10;
}
The variable “v” is assigned the variable 0. Then the function “change_v” is called. Inside the function, the variable “v” is re-assigned with the value 10.
The following is a more complex example of how to use a function. The function is passed two values and it returns a value.
a=“hello”;
b=“world”;
c=join_strings(a,b);
Function join_strings (u, v){
return u +”” + v;}
The string “hello” is assigned to the variable “a”. The string “world” is assigned to the variable “b”. The function “join_string” is called and is passed the values of “a” and “b”. These values arrive in the variables “u” and “v”. The two values are added together and returned into the value “c”.
I have yet to create another way to look at passing variables to functions. I am on the verge of understanding it well enough.
…continued
Here is a real example of the above example:
function helloWorld (myName){
trace ("Hello "+myName+"!");
}
myName = "Frank";
helloWorld ("Bob");
trace (myName);//the variable myName is still Frank!
//an argument is a local variable. Outside of the function myName is Frank
//inside the function myName is changed to Bob and is used as Bob
------------------------------ end