Internet Commerce

Partners & Affiliates

Developer Channel


Featured Flash FLA
Gallery Downloads 11401 Flash Movies | 5 New Flash Movies Added
What's New | Top 100

Featured FLA

»  Author: Nick Kouvaris
»  Title: Znax
»  Description: Znax is a board game. Click 4 tiles of the same color and form squares as big as you can. You will erase all the tiles inside the square and collect points. Get maximum score if you make a square with game edges.
»  More by: Nick Kouvaris


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

Featured Site

»  Author Agence WOP Digital Agency
»  Title: Electricdrum
»  Description: French WOP Agency, 3D websites, Flash (Papervision, Away 3D), event or institutional projects. The agency operates on all digital projects: consulting, design, graphic design, development, online communication. The WOP agency follows you on the implementation of original, creative and optimized digital projects.


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

» How To Make A Simple Animation Using Christmas Clips
» Simple Step by step flash game tutorial Spot the diffrence
» How To Make A Moving Text Slide
» Create Flash Banner With Text Float Effect
» How To Make Zoo Photos Slideshow
» How To Make A Dolphin Photos Slideshow
» How To Make A Fathers Day Slideshow
» How To Make A Transparent Background of Your Flash File
» Create Flash Banner With Text Disco Light Effect Today we will introduce you a Text Disco Light eff
» Unknown Tag: Title10
Random Tutorial | Add Site


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 3
«prev 1 2 3 4 5 6 next»

A function for Going Shopping

Take going shopping at the supermarket. It’s something we do over and over again. It’s basically the same tasks every time. So if we could write functions in real life, it would be a good candidate for one. To write such a function, I first need to think about the individual tasks that are involved in ‘shopping at the supermarket’. The list of tasks might turn out looking something like this:

go to the supermarket
buy the things on my list
pay for the food
come home
put food away

This could be made into a function. I define the function by doing something like this:

function goShopping () {
go to the supermarket;
buy the things on my list;
pay for the food;
come home;
put food away;
}
Voila! One definition of shopping.

I've given the function a name ‘goShopping’. This is so that when we actually use the function later, flash knows which actions it needs to do. All of the tasks that the function ‘goShopping’ includes are put inside the curly brackets. You can think of them a little like:

{ means “tasks start here” } means “tasks stop here”

Parameters change, tasks don't

You'll notice that in order to do my shopping I need a shopping list and that might change each time I go shopping. I need to write a new list each time. The tasks inside the function’s curly brackets will be the same every time the function is done.

So instead of putting the list into the function where it would be the same every time I went shopping, I need to give the function a different list each time I do it. I send the shopping list in using a 'parameter'. Like this:

function goShopping (shoppingList) {
go to the supermarket;
buy the things on my: shoppingList;
pay for the food;
come home;
put food away;
}

The parameter is just a bit of information that the function needs to have in order to do its tasks properly. When we are defining the function, we need to tell it to expect a shopping list, even though we can’t know yet exactly what the shopping list will look like.

A parameter is a variable that can change every time the function is done.

Getting a return on your effort

OK... but the whole point of going shopping was to get food. What happened to that? Well… I want to get some food out of the function. A bit of the function has already bought the food (according to my list) so I'm most of the way there. But I need to tell my function to give it to me. I do this using 'return'

function goShopping (shoppingList) {
go to the supermarket
buy the things on my: shoppingList
pay for the: foodBought
come home
return: foodBought
}

“return” is a special word inside a function. It tells the function what it is supposed to spit out at the end. Sometimes a function won’t need to return anything, but when it does, this is how we tell it what it is that we want back.

Doing the deed

To actually go shopping I need to 'do' the function. This is sometimes called 'calling the function' or 'invoking the function'. It all boils down to the same thing though, we want the tasks in the function done! In order to do that we need to tell flash to go do the shopping and give it a shopping list. So now every time I want to go shopping, all I have to do is say, for example:

todaysList = “milk,sugar,baked beans,loadsa chocolate”
goShopping (todaysList);
and the shopping will get done properly using todaysList. But where does the food go? Remember, we told the function to return the food to us and rather than have it just all over the floor, we should put it somewhere, so I put it into something that can hold different stuff: a variable... (well I put food in the cupboard actually)... so a variable called ‘cupboard’!
todaysList = “milk,sugar,baked beans, loadsa chocolate”
cupboard = goShopping(todaysList);
So this will send someone shopping (hopefully not me) with today’s shoppingList and will fill the cupboard with the food bought while shopping.

If my function doesn’t return anything (and not all of them do), then I don’t need somewhere to put it, but because this one returns the food, I’ll need to put it somewhere.

A variable can be used to store the results.

Where does what go and when?

So, now with flash, it's usually a good idea to get the function definitions, (where you tell flash which tasks it should do) right at the beginning in Frame 1 of scene1 of the movie. Why? because flash must read its instructions before it can do the job (makes sense!). If we give flash all of its instructions at the very beginning, then whenever we tell it what to do, we can be sure it has already read the instructions.

So in Frame 1 of the main timeline we put the function definition:

function goShopping (shoppingList) {
go to the supermarket
buy the things on my: shoppingList
pay for the: foodBought
come home
return: foodBought
}
For the rest of this movie, we'll be able to tell Flash to go shopping whenever we want using:
todaysList = “milk,sugar,baked beans, loadsa chocolate”
cupboard = goShopping(todaysList);
Or if we want to get shopping on thursday too, we might do the following somewhere else
ThursdaysList = “chocolate, beans, spam, beans, chocolate, spam”
thursdayCupboard = goShopping (thursdaysList);
Easy isn't it?

«prev 1 2 3 4 5 6 next»

» Level Basic

Added: : 2001-05-16
Rating: 8.46 Votes: 185
Hits: 8682
» 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