Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 4 - the functions

This is a discussion on Tutorial 4 - the functions within the Actionscript for beginners - tutorials forums, part of the Tutorials category; Hello! This is our fourth appointment with the basics of Actionscript 3.0 . After having seen the variables , the loops ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Actionscript for beginners - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 10-12-07, 07:22
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 4 - the functions

Hello!


This is our fourth appointment with the basics of Actionscript 3.0 .
After having seen the variables, the loops and the Arrays, this time I will introduce you to the functions.
The functions can be extremely complex and this is not the final point of this tutorial.
The goal of this tutorial is to give a general explanation to those of you starting with Actionscript so to learn the basics and have a good base to increase your future knowledge in using the functions.

The functions are a set of instructions which is performed once called.
They are a portion of stand alone code, meaning that they can carry out the code without the help of other components.

The functions are composed of:
- an identifier
- some parameters
- some returned parameters

They can be called from various points of our application using the identifier and the parenthesis operator().

Simple function

Let us see how to define a simple function
Code:
function flep():void
{
	trace('ok');
}
function ? I declare to Flash that it is a function
flep() ? is the identifier ( so to be able to call it back later on. More or less like the name of the function) and the parenthesis () which for the time being do not contain any parameters
:void ? let Flash knows that this function does not return any value (the :void is not obligatory but it is a good habit to take to make you remember that a function could return a value)
the code in between the brackets is the code witch will be performed by Flash when the function is called. If I publish the SWF at this time, nothing will happen as the function as not been called into action.

How to call the function flep?
This way:
Code:
function flep():void
{
	trace('ok');
}
flep();
using the identifier and the operator parenthesis ().
Inside the function, there is a command which will be carried out by Flash. If I publish the SWF at this time, I will obtain the following output:
ok

Let us next make a concrete example using a MovieClip.
I create a MovieClip (the shape is not important) and having placed an instance of it on stage, I give it the instance name ?clip_mc?
I position it in the top left angle of the stage.
Now, I want to move clip_mc to some chosen coordinates x and y on stage.

To do so, I create a function named moveClip the following way:
Code:
function moveClip():void
{
	clip_mc.x=300;
	clip_mc.y=250;
}
and I call it back this way:
Code:
moveClip();
If I publish the SWF at this time, I notice that the MovieClip has moved to the desired coordinates as declared in between the brackets of the function moveClip.

Another example of a simple function:
Code:
var flower:String;
function assignName():void
{
	flower='daisy';
}
assignName();
trace(flower);
I created a string type variable named flower.
I created a function named assignName and in between its brackets, I assign a value to the variable flower.
If I publish the SWF at this time, obviously, the variable flower will not have a value as the function assignName (containing the value of the variable flower) has not been called.
So, I call assignName() and immediately afterwards I tell Flash (via the command trace) to display the value of the variable flower. I obtain the following output:
diasy

Function that receives parameters
As said before, an Actionscript function can receive parameters.
This is a fundamental point as the function itself could perform the code included into it based on the value of the parameters sent to it.

Let us take a look at the first example:
Code:
var section:int=10;
function multiply(n:int):void
{
	section=section*n;
}
multiply(5);
trace(section);
I created a variable named sezione of int type and I assigned to it the value of 10.
I created a function named multiply and in between its parenthesis, I assigned a parameter named n of type int.
The function will multiplied the value of the variable sezione by the value passed as parameter.
So, declaring sezione=sezione*n; means that the new value of sezione is the value of sezione multiplied by the value of n.
I call the function passing to it a parameter (a full number as the type int as been declared or Flash would return an error). In this case, I pass the number 5.
I ask Flash to show me the new value of sezione (via trace) and I obtain the following output:
50

I will now make an example using a MovieClip placed on stage (clip_mc).
I create a function that will move the MovieClip to the coordinates passed as parameters.
Code:
function moving(cx:Number,cy:Number):void
{
	clip_mc.x=cx;
	clip_mc.y=cy;
}
moving(200,200);
In this case, I implemented 2 parameters in reception to the function moving and that I assign respectively to the x and y of clip_mc.
If I publish the SWF at this time, I notice that clip_mc has moved to the declared coordinates.

Function that returns a value

An Actionscript function can also return a value.
Up till now, I used :void to tell Flash that the function does not return a value.
If I would change the :void to (as example) :Number, it would mean that this function will return a value of number type.

Let us look at it.
Code:
var people:Number;
function double(n:Number):Number
{
	return n*2;
}
people=double(23);
trace(people);
I declare a variable named people of Number type.
I create the function double that wants a parameter of Number type and will return a parameter of Number type.
With the operator return, I tell Flash to return the value of n (the value received by the function) multiplied by 2.
I directly assign to the variable people the value returned by the function double:
people=double(23);
If I would publish the SWF at this time, I obtain the following output:
46

In brief, saying double(23); is the same as saying 46.
In fact if I do a trace(double(23));, the output would be 46.

Let us make another example with a MovieClip.
I create a function that each time it will be called, it will return a rounded shape MovieClip.
Code:
function createClip(radius:Number):MovieClip
{
	var clip:MovieClip=new MovieClip();
	clip.name='clip';
	clip.graphics.beginFill(0xFF0033,1);
	clip.graphics.drawCircle(300,300,radius);
	clip.graphics.endFill();
	return clip;
}
addChild(createClip(50));
trace(createClip(50).name);
I create a function named createClip that wants as parameter of Number type and returns a MovieClip.
Inside the function, I create a variable named clip of MovieClip type and I instantiate it, meaning I create a new MovieClip using the operator new. I assign a name to the MovieClip. I tell clip to draw a circle with the coordinates x 300 and y 300 and a radius based on the value of the parameter radius.
I return clip.

In this case, we can understand easily that the function returns a MovieClip as I add it to the stage using the method addChild the following way
addChild(createClip(50);
In fact, if I do a trace(createClip(50)); I obtain the following output:
[object MovieClip]
clip


Source files:
Attached Files
File Type: zip tut_4.zip (7.0 KB, 78 views)


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

  #2 (permalink)  
Old 21-05-08, 10:31
Junior Member
 
Join Date: Sep 2007
Posts: 1
Rep Power: 0
katya.lensky is on a distinguished road
Re: Tutorial 4 - the functions

Hello flep studio people!

I am just a beginner in the world of flash, still learning the syntax for the actionscript.
I am working on a project where I have to write a function but not sure about the syntax. If you have a free minute, I would greatly appreciate if you give me a hand with it! Thank you.

I have thumbnail_mc, pictureA_mc, yes_mc, no_mc and clr_btn.
thumbnail_mc on press startDrag();
thumbnail_mc on release stopDrag();
if thumbnail_mc ends up on top of pictureA_mc, play yes_mc
else play no_mc
clr_btn on release, thumbnail_mc goes back to the original position.
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
Actionscript 3 variables and functions from child movieclip coma Actionscript 3.0 newbies 9 25-11-08 21:41
Tutorial 3 - i cicli Flep Actioscript 3.0 base - tutorials 12 23-06-08 23:58
External Swf functions wonīt work! madmad Actionscript 3.0 newbies 0 28-05-08 08:53
Tutorial 7 - the packages Flep Object Oriented Programming - tutorials 3 13-11-07 02:42
Tutorial Cercasi joyz Actionscript 3.0 base 22 30-07-07 10:52


All times are GMT. The time now is 21: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