Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 3 - the methods

This is a discussion on Tutorial 3 - the methods within the Object Oriented Programming - tutorials forums, part of the Tutorials category; A method of an Actionscript 3.0 class is nothing else then a function which can implement arguments (and so ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Object Oriented Programming - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-10-07, 06:50
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 3 - the methods

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:
Attached Files
File Type: zip tutorial_3.zip (5.5 KB, 38 views)

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 05-06-08 at 13:53..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 20-10-07, 15:06
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Re: Tutorial 3 - the methods

Hello Flep,

Just finished this lesson, but i have a question regarding the following:

Quote:
Do not assign a name starting by a name to any method.
What do you mean by that?

Thanks a lot for all the wonderful help!
Abdul
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 20-10-07, 15:09
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Re: Tutorial 3 - the methods

ops... sorry
It was an error, the real mean is:
Do not assign a name starting by a number to any method.

I've fixed it
__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 20-10-07, 15:53
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Re: Tutorial 3 - the methods

Cool ! that was quick
thanks man!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5 (permalink)  
Old 31-10-07, 07:09
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Re: Tutorial 3 - the methods

Thank you Abdul for your translations !
__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Flash Multi Gallery
  #6 (permalink)  
Old 31-10-07, 20:12
Junior Member
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0
Abdul is on a distinguished road
Re: Tutorial 3 - the methods

Always my pleasure Filippo
Remember, this is the least!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #7 (permalink)  
Old 27-07-08, 02:21
Junior Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
thawootah is on a distinguished road
Re: Tutorial 3 - the methods

This is a good series of OOP tutorials . much appreciated.!!

I get how classes work much like functions and how they return values.. but I'm still finding it hard to interact with the timeline or library.

I think a tutorial that shows how you would break up a simple AS2 script and convert it to AS3 classes. might help us OOP begginers. Like one that would handle dynamic data and work with the library and pass data to and from functions.

and the document class vs regular class is still confusing to me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 4 - the functions Flep Actionscript for beginners - tutorials 1 21-05-08 10:31
Encapsulation, Getter and Setter methods Flep Tutorials 6 08-02-08 12:34
Tutorial 3 - the loops Flep Actionscript for beginners - tutorials 0 21-11-07 06:50
Tutorial 5 - attribute of the methods Flep Object Oriented Programming - tutorials 0 15-10-07 06:43
getPixel/setPixel methods of the BitmapData Class Flep Tutorials 0 29-09-07 10:04


All times are GMT. The time now is 04:29.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406