RSS link icon

Functions with actionscript 3 

 

In this Flash actionscript tutorial I will give you enough knowledge about constructing your own functions with actionscript 3.0, In this Flash tutorial we will be looking at three examples, or actually two, but the third is and extended version of the another.

We will look at how a simple function is build, and how to call it in flash. Then will will build on that function so it can receive giving values. And in the last example we will look at how to make a function return a value to us.

In my example we will have one element on the stage, its a smiley I made, and converted to a movieclip, remember to give it an instance name "smiley", so that we can reference to it in actionscript.

smiley flash

First we will look at how to construct a simple function in actionscript.

This is how a function will look in its most stripped form.

function moveSmiley():void {
    Smiley.rotation += 25;
    Smiley.y -= 30;
}
moveSmiley();

First line is to tell flash its a function we want to make, then naming it, "moveSmiley" I usually start with low case letter, and second word upper case, remember to end it with (), then a colon and void, void is to say that we dont want to return any value, if we for example wanted to return a number we calculated in the function then it wouldn't be void, but :Number

Then between our brackets { } we will be placing some code later.

Now this function will not do anything yet, because we need to call it first, and thats done easy with.

moveSmiley();

What this does it that it moves the smiley a bit up on the stage and rotates it 25 degrees.

Now that we did this we can extend the function because the function we just made could be more reusable, and dynamic, meaning we want to add some parameters so that we can call the function and tell it how much we want it moved up, and how much rotation for the smiley.

And here is the code.

function moveSmiley(Smiley:MovieClip, movement:Number = 100, rotateSmiley:Number = 90):void {
    Smiley.rotation += rotateSmiley;
    Smiley.y -= movement;
}
moveSmiley(Smiley, 100, 20);

Yes this might seem a bit confusing, but actually all we did was in the first line add two parameters, a movement:Number to move the smiley, and a rotation number, the reason why I choose to add an = and a number is to have a standard value, so if I forget to put in some numbers when I call the function it has a standard value, (I think thats a neat and great tip).

Now within our brackets { } we set the smiley.rotation to += the rotateSmiley input value.

The same we did for smiley.y.

That last thing I want to show you with functions is how to receive a value, this is just a simple function, we will reuse some code from before, make the smiley go up 10 from its actual position, then the new thing will be to receive its now y position in the function.

function moveSmiley():Number
{
    Smiley.y -= 10;
    return Smiley.y;
}
trace(moveSmiley());

As you might see, we have replaced the void with Number in the first line, this is now because we want to return a number when we are done.

As said before, the function will move the smiley graphic 10 up on the y axis, then return its new value to our trace function.

The trace function is just for us coders to use, to check values etc.


youssef says: Wednesday, August 20, 2008

thank u so much


zach wilson says: Sunday, August 03, 2008

good info, simple and to the point. Muchos gracias.


Jimbo says: Friday, July 25, 2008

Cheers, I was struggling to get to grips with AS3, having already been reasonably competent with AS2. This has helped a lot.


alkei58comp says: Wednesday, June 18, 2008

Thanks you so much for this tutorial.


budijoi says: Sunday, May 11, 2008

....thank you for this tutChr(34)s....


mangus says: Friday, April 25, 2008

great tutorial... ty i was lookin 4 sometin like this.

   


 

 

 

 1

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact