A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11303 Flash Movies | 7 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Nitin Tikhe
» Title: Cart
» Description: This Animation Tut is a fun and useful for kids below 15 years. Watch the Flag, Doors, Stick and Horse movements.
» More by Nitin Tikhe


Random FLAs | Add Flash Movie
Featured Flash Site
Gallery Downloads 6008 Flash Sites | 0 New Flash Links
What's New | Top 100 Flash Site

Featured Site

» Posted in the Flash Kit Links section
» Title: Banana Swimwear
» Description: This is a banana swim wear interactive catalog we designed and animated in Flash


Random Links | Add your own Flash Related Links
Flash Tutorials 1255 Tutorials 7 New Tutorials Added!
What's New | Top100

» Make flash video player for broadcasting live streaming video / TV on website
» How to convert the project file of Flash Demo Builder 2.0 into FLV file
» FLV to PSP for Mac - How to convert YouTube video to PSP on mac
» How to Convert FLV to MP4 for Playback on iPod
» how to download and convert youtube video to AVI with Leawo Free FLV converter
» Flash Multi-player Game Tutorial - TicTacToe
» How to make Flash elearning tutorials with screen recorder?
» Fader API:Slideshow with MovieClips on stage
» How to convert MS PPT file into an FLV File
» Unknown Tag: Title10
Random Tutorial | Add Site

Network Design Manager
The Computer Merchant, Ltd
US-VA-Hampton

Justtechjobs.com Post A Job | Post A Resume


Tutorials Home What's New Top Rated Submit myTutes Random!

Search Tutorials


Tutorials Tutorials » Actionscripting/Basic

Categories Functions for Beginners
Author: Andy Armistead (with a little help from FlashGuru)

 
Page 5
«prev 1 2 3 4 5 6 next»

Our first Custom Function

Now, we don’t really expect Flash to go shopping for us (though in the wonderful world of online shopping even this might be possible). Let’s look at a real example of a useful function that we could regularly use in Flash.

We are going to write a function that will work out percentages for us. So, the way we usually say this is something like:

“10 out of twenty is fifty percent.”

Define the tasks

I haven’t done maths in many a year, but I do remember that we work out a percentage by doing:

‘Number divided by the total possible. Then times it by 100 and that’s your percentage”
So this translates to:
(number/total)*100 = percentage
(because in flash the ‘/’ means divide by and the ‘*’ means multiply by).

That is the task that we want to do, so in the same way as we did for shopping we would define the tasks as:

function percent() {
 percentage = (number/total)*100;
}
We’ve given the function the name ‘percent’ and told it what tasks it needs to do. Easy!

Work out the parameters

In order to do the tasks in the percent() function, it needs to know what value ‘number’ has and what value ‘total’ has, then it can do the rest. So we tell it to expect these values by specifying them as parameters:

function percent (number,total) {
 percentage = (number/total)*100;
}
Notice, that because we need to give two different parameters, we can just put them inside the round brackets separated by a comma. Pretty straightforward eh? Because we are defining the function, we can put as many and as few parameters as we like. That’s one of the beauties of writing custom functions… you get to decide everything.

Getting the return

The point of doing this function is that we want to get the percentage back from it. Otherwise, there’d be little point in doing it. So we need to return the percentage.

function percent(number,total) {
 percentage = (number/total)*100;
 return percentage;
}
Nothing could be easier.

Using the function

Remember from our shopping example, we needed something to put the food in, so we used a variable ‘cupboard’ that would hold the food that the function returned. Well here, we’re returning the percentage so when we call the function we need to give it somewhere to put the returned value. We’re going to use a variable called ‘pcent’. You could call the variable anything you like, the function won’t be picky; it’ll put the returned value into whatever you tell it to.

So for our example, when we call the function we might do something like:

pcent = percent(10,20);
this tells flash to do the tasks inside the function called percent(). It tells it that the value of ‘number’ is 10 and that the value of ‘total’ is twenty. Flash then goes off and works it all out for you and puts the result into the variable called ‘pcent’. So in this case ‘pcent’ will be set to 50.

One thing to keep in mind here: The order of the parameters is important. If I do:

pcent = percent(20,10);
then Flash will do:
percentage = (20/10)*100;
return percentage;
which would give us a completely different result. In this case ‘pcent’ would equal 200.
When you use a function, it is important that you give it the values it needs in the right order, otherwise the results you get will not be what you expected!

«prev 1 2 3 4 5 6 next»

» Level Basic

Added: : 2001-05-16
Rating: 8.45 Votes: 184
Hits: 8671
» Author
No details available.
» Download
Download the files used in this tutorial.
Download (0 kb)
Get conversion and unzipping tools for PC and Mac here!

» Forums
More help? Search our boards for quick answers!

Please rate this tutorial, 10 is the top rating, you can also click the comments link to read/write a review.
10 9 8 7 6 5 4 3 2 1
Read or Post Comments