Flasher Archive

[Previous] [Next] - [Index] [Thread Index] - [Previous in Thread] [Next in Thread]


Subject: Function 101
From: AndyA
Date: Fri, 23 Mar 2001 21:36:31 -0000

Hi All, here's the ammended code and tutorial... so that it works.

I'm not sure what was wrong with the last one, but I'm working it out as we
speak!

Apologies all round.

Andy

Quote
---------------------------------------------------------
You have two options:

1) remove the scenes and use frame labels instead
2) in Frame 1 of the maintimeline put the following code

function playScene (frameRef)

_root.gotoAndPlay (frameRef);
}

In the scene to which you go, you put a unique Label.

In your button you put:

on (release)

_root.playScene (frame_label_here);
}



It should work.
----------------------------------------------------------
Quote

His response was that he simply couldn't understand functions and that it
was all too much for him, so he was about to rebuild his entire (quite
sizeable) fla. As a response I posted a quick Functions 101 for him to the
list, which I thought might be useful to people here, since we had the
thread on levels and depth.

The text is 'as was' and might be full of holes or unclear bits, but I post
it anyway in the hopes of helping someone. (Incidentally, it refers back to
the example above).

regards

Andy

Quote
-----------------------------------------------------------

What is a function?
===================

A function is just a bit of code that is reusable. Think of it like a set of
tasks packaged up. Take going shopping at the supermarket. To do this I:

get my shopping bag
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 ()

get my shopping bag
go to the supermarket
buy the things on my list
pay for the food
come home
}

Voila! One definition of shopping. Notice the tasks all go between the curly
brackets. 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.

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. So instead of putting the list into the function where it sould 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)

get my shopping bag
go to the supermarket
buy the things on my... shoppingList
pay for the food
come home
}

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

get my shopping bag
go to the supermarket
buy the things on my... shoppingList
pay for the... foodBought
come home

return foodBought
}

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 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
everytime I want to go shopping, all I have to do is say:

goShopping (tuesdaysList);

and the shopping will get done properly using tuesday's list. But where does
the food go? well I have to 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!

cupboard = goShopping(tuesdaysList);

So this will send someone shopping (hopefully not me) with tuesdays
shoppinglist and will fill the cupboard with the food bought while shopping.

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 actions will be necessary) 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)

get my shopping bag
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:

cupboard = goShopping(tuesdaysList);

Or if we want to get shopping on thursday too, we might do the following
somewhere else

thursdayCupboard = goShopping (thursdaysList);

Easy isn't it?


The real world
==============

Now, let's do the same with our real example of sending the playhead to play
a frame in a scene from a button inside a movieclip.

First we need to give flash its instructions. So in Frame 1 of the main
timeline we define the function:

function playScene()

_root.gotoAndPlay (frameRef);
}

We've told it to gotoAndPlay a label. But we know that we have to add the
frame label represented by the variable 'frameRef'. If the function is going
to be reusable, then we need to be able to give the function those values
when we actually run it (just like the shopping list). So we tell the
function to expect the values it needs by writing:

function playSAcene(frameRef)

_root.gotoAndPlay (frameRef);
}

Note: Whatever the names that I define inside the round brackets, those are
used inside the function itself. Since they are just variables that I will
set when I use the function later, I can call them whatever I like when I
define the function. As long as the variable names in the actions match the
variable names in the parameters.

So that's the definition of the function done. When we run the function we
need to tell it what the frame label is (by setting the variable frameRef)
and it will do the rest.

OK... now we have to actually use the function. In our button in the
movieclip, that's where we want to actually tell the movie to goto the frame
label and play so we need to make the function run. We wanted the movie to
do the task when we pressed a button, so in the code for the button we
write:

on (release) {
_root.playScene ();
}

Because we didn't want the function to return anything, we don't need a
variable to store the result. But we still need to tell the function which
frame label to go to. So we do

_root.playScene ("start");

in another button, we can tell it to go somewhere else:

_root.playScene ("Impossible");

and again and again.... without ever having to rewrite the code that we
defined in the function.

Summary
=======

That's how functions work. It's just reusable bits of code that you can use
anywhere in your script as long as you give it the bits of information it
needs to do its job! You make the code run by using a codeword which is the
name of the function. In this case playScene().





[Previous] [Next] - [Index] [Thread Index] - [Next in Thread] [Previous in Thread]