A method of an Actionscript 3.0 class is nothing else then a function which can implement arguments (and so receive parameters), return a value, doing both of them or none of them and so remaining a simple function.
Also, a method can have public or private attribute or be static. I will explain it further on.
Those functions placed in a class are called methods.
A classic example of a method could be gotoAndPlay(10) of the MovieClip Class.
Naturally no one knows the logic behind this method, other then the Adobe engineers and architects, but we do know that calling the method, our timeline is moved to keyframe 10 (10 being passed as parameter) and from there the Flash film will play on.
Having said that, next are some important examples on how to create our methods for our class"
How to define a method
The correct syntax is: attribute MethodName=function()
I create Third.as and I associate it to third.fla as the Document Class.
Code:
package
{
import flash.display.MovieClip;
public class Third extends MovieClip
{
public function Third()
{
}
private function myMethod():void
{
}
}
}
to notice in the method (function) myMethod:
private differently of the building function seen in lesson 1 (always public), private do not allow this method to be called from outside the class or one of its instance. We will see those attributes in more details in the next lessons.
function tells Flash that it is a function and so, a method.
myMethod is the name associated to the function so to be able to recall it by name
:void indicates that this method will not return any value.
How to call a method
The correct syntax is, as seen before: mioMetodo();
Code:
package
{
import flash.display.MovieClip;
public class Third extends MovieClip
{
public function Third()
{
myMethod();
}
private function myMethod():void
{
trace('The method myMethod has been called');
}
}
}
in this case, the building function calls the method " myMethod" into which is placed the code. Clearly, once the method called, Flash will carry out the existing code and would return the trace.
Instead of a trace, we could assign a value to a property calling the method as follow:
Code:
package
{
import flash.display.MovieClip;
public class Third extends MovieClip
{
private var aName:String;
public function Third()
{
myMethod();
}
private function myMethod():void
{
trace('The method myMethod has been called');
aName='Filippo';
trace(aName);
}
}
}
I create a string property named "aName".
Calling mioMetodo(), I assign a value to that property and following it of a trace, Flash would return the following output:
The method myMethod has been called
Filippo
Methods with arguments
Let us now see how to define a method which implements arguments, meaning that it needs variables to be executed.
Code:
package
{
import flash.display.MovieClip;
public class Third extends MovieClip
{
private var aName:String;
public function Third()
{
myMethod();
changeName('Ulisse');
}
private function myMethod():void
{
trace('The method myMethod has been called ');
aName='Filippo';
trace(aName);
}
private function changeName(s:String):void
{
aName=s;
trace(aName);
}
}
}
in this case, after the call of the method "myMethod", I call a new method named changeName:
"(s:String):void" means that this method requires a string value when called and this value will take the name of "s". In fact, inside the method, I assign the value "s" to the property "nome" and the output of the trace will be:
The method myMethod has been called
Filippo
Ulisse
So, the call of the method " changeName" has to be done this way:
changeName('Ulisse');
if we do not pass a string to the method, Flash would return the following error:
1136: Incorrect number of arguments. Expected 1.
Flash tells us the number of arguments is wrong as " changeName" requires one.
The number of arguments is unlimited as long as they have a unique name.
In the following example, I add a new int property (round number) named "years".
I call the method "myMethod" and into it I assign "s" to the property "aName" and "n" to the property "years": changeName('Ulisse',35);
Code:
package
{
import flash.display.MovieClip;
public class Third extends MovieClip
{
private var aName:String;
private var years:int;
public function Third()
{
myMethod();
changeName('Ulisse',35);
}
private function myMethod():void
{
trace(' il metodo myMethod Ë stato chiamato');
aName='Filippo';
trace(aName);
}
private function changeName(s:String,n:int):void
{
aName=s;
years=n;
trace(aName);
trace(years);
}
}
}
output:
The method myMethod has been called
Filippo
Ulisse
35
How to return a value using a method
The class method can also return a value.
For example:
Code:
package
{
import flash.display.MovieClip;
public class Third extends MovieClip
{
private var aName:String;
private var years:int;
public function Third()
{
myMethod();
changeName('Ulisse',35);
}
private function myMethod():void
{
trace('The method myMethod has been calledí);
aName='Filippo';
trace(aName);
}
private function changeName(s:String,n:int):void
{
aName=s;
years=n;
trace(aName);
trace(years);
aName=changeAll(30);
trace(aName);
trace(years);
}
private function changeAll(n:int):String
{
years=n;
var newName:String='Arianna';
return newName;
}
}
}
I create a new method named "changeAll" which requires as parameter an int number and returns a string:
I assign to the property "anni" the value of "n" (value passed on call of the method)
anni=n;
I create a local variable (local as it is inside the method and once the code executed, Flash would free its memory) of string type and I assign to it a value of "Arianna"
var newName:String='Arianna';
I return the variable "newName"
return newName;
The call of the method cambiaTutto is made from the method "changeName" the following way:
aName=changeName(30);
basically I assign directly to the property the returned value of the method "changeAll", passing it a number which the method "changeAll" itself will use to assign it to the property "years".
Even more, we can see that the last trace is carried out inside the method "changeName" immediately after the call of "changeAll". If I would have placed it as the last command of the method "changeAll", Flash would have not execute it as once the command "return" is reached the successive lines are ignored.
Output:
The method myMethod has been called
Filippo
Ulisse
35
Arianna
30
A method can only return one value.
Do not assign a name starting by a number to any method.
In a class, each method needs a unique name.
Source files: