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 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.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