A Flash Developer Resource Site














Internet Commerce

Partners & Affiliates














Developer Channel

internet.com


Featured Flash FLA
Gallery Downloads 11336 Flash Movies | 2 New Flash Movies Added
What's New | Top 100

Featured FLA

» Author: Surjit Dhami
» Title: Book
» Description: Book
» More by Surjit Dhami


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

Featured Site

» Posted in the Flash Kit Links section
» Title: All-American Rejects
» Description: Get to know this great band by exploring their "practice room".


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

» Create xml slideshow with free template
» How to Insert a Multilingual Subtitle Into Your Flash Video Studio
» How to Create Cool Halloween Slideshow
» Debugging flash using the Firebug console
» Create Flash Slideshow on Blogger
» FLASH TRICKS IN WEB ADVERTISING: FLASH BANNERS
» HTML Photo Gallery Tutorial
» Create your first flash site – PART 1
» How to Make a Flash Photo Gallery
» Unknown Tag: Title10
Random Tutorial | Add Site

Webmaster (IL)
Next Step Systems
US-IL-Chicago

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.46 Votes: 185
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
 
   
 

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs