Search Tutorials
' Examining The Class File:So by now you have probably noticed that class files are just external ActionScript Files with a specific structure. Let's take a closer look at that structure. The Class File:
Class Definition:Everything that makes up a class is contained between opening and closing
brackets { }. The name of the class is preceded by the keyword class.
So an empty class named person would look like: Since this is a simple tutorial, we have created our class inside the root of the class path that we added. You can further breakdown your class path into folders called packages, but I will not go into that in this tutorial. Instance Variables or Properties:Shortly following the opening bracket of the class, we see the following
line: Also notice that I have not actually assigned value to this variable yet. And notice the :String. The:String is an example of a concept called variable typing. I will not go into this in great detail here, but just know that when I type a variable it can only accept values of that type. So in this case _myname can only be a string. Finally, notice the _ at the beginning of the variable name. It is a common practice to put an _ before instance variable names to distinguish them from other variables. The purpose of instance variable should become clearer when we examine the code we used in the class_basics.fla. Constructor Function:Following the instance variable, we see a function with a name that matches
the class name: This function is known as the constructor and is automatically called when the class is instantiated. Instantiated simply means I create an instance of the class using the new keyword (e.g., new Person();). In our example we pass something called a parameter to the function. A parameter is anything that appears between the () when you call a function. In our case we pass a string and use that value to set the _myname instance variable. Please note the following regarding constructors:
Methods:Finally, at the end of our class we see: Functions within a class are referred to as methods. While the constructor
method is called automatically, other methods within a class have to be
specifically called (though you can call other methods from within the constructor).
To call a method of a class instance you use the following syntax: instanceName.methodName(); You will see examples of this when we go over the code from the class_basics.fla in the next section. These methods can also accept parameters, in which case, the syntax would look like: Summary:So that concludes our brief look at the structure of a simple class. There is a lot more that can go into a class file and there are quite a few things that I did not explain, but for now, let's move on to exploring what happened when we used the code in the class_basics.fla. '
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|