Lesson 1
Breaking up the script
Every AS3 script starts with the word package. This is different from AS2 scripts, which directly start with the word class. You will see in a later lesson what exactly a package is.
package
{
Next we have to import all the classes we need for the movie. Here we import the Sprite class and the TextField class. The Sprite class is new in AS3 and is a base class. The Sprite object is similar to a movieclip but without a timeline. The Sprite class is useful for display including graphics. So you will find the Sprite class in many scripts. In order to use the Sprite class and its methods we need to extend our newly created Starter_1 class to the Sprite class. If we don't do that we will later get an error, because addChild(object). is a method, which the Sprite class has inherited. If you know AS2 you might have noticed that the class declaration contains the word public. Does it mean there are private classes, too? You need to wait until a later lesson for this answer. For now just note that we need to add the word public.
import flash.display.Sprite;
import flash.text.TextField;
//
public class Starter_1 extends Sprite
{
What is a class? A class is a collection of properties and methods, in AS3 terms a collection of objects. We create a custom class like this one here and then we can add the class properties and methods to an existing class like the Sprite class but also use the methods from the Sprite class, which is the superclass of the Starter_1 class.
Back to our script: we now declare a variable, which is the variable for the TextField. All variables used in the movie will be declared after the class declaration except for variables we want to be local. We will learn that in another chapter. We also need to add the word private or public at this point. What that means, sorry a later chapter. For now just add it. If we omit that we will not get an error but a warning. This is different from AS2, where we did not have to enter private or public. However, AS3 is much stricter in its rules compared to AS2.
private var tField:TextField;
In the next line we introduce the Constructor. Unlike other functions in the class the constructor is initiated earlier. Therefore, we need to be careful what to write inside. The best is to leave it empty or as in our case here we need the constructor to initiate a function. Some people omit the constructor completely when it is empty. However, I like to leave it there as a contrast to the other functions. It's up to you. Important here is that the constructor does not have any return type. I will explain in a second what that is.
public function Starter_1 ()
{
myTest();
}
Now there is our main function. We call this function "private". After the parenthesis we need to add something. We add void, because the function does not return any value. later we will learn about functions returning a value.
private function myTest():void
{
Now we come to the function contents. Usually people would just write a trace action here, but for learning purposes I will place a textfield here. Then you know how to place a textfield. Check this line:
tField = new TextField();
This is kind of familiar to us from creating instances of other objects in AS2 such as myxml = new XML();. Just we don't know this for a textfield. Well, in AS3 there is no createtextField method any more. All instances are created using the new word. That is definitely much simpler than to remember all the code regarding creating certain objects.
I don't have to say much about the next lines except that properties like x, y, width, height are now uniformly written without the underscore.
tField.autoSize = "left"; tField.background = true; tField.border = true; tField.x = 20; tField.y = 75; tField.text = "Hello You, what is your name?";
However, the next line is not familiar to you and this is something you should not forget. In order to place the instance on stage you need to use the method addChild(objectname). But again we don't have to care about the level the instance is placed, since that is automatically done. Looks to me much simpler than AS2 or not?
addChild(tField); } } }
And that brings us to the end of the first lesson. From now on I will not any more go into the details of what we have just learnt but only focus on the new aspect.
In the next lesson we will learn more about what a package is.