Flasher Archive

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


Subject: Functions 101
From: AndyA
Date: Fri, 23 Mar 2001 18:18:17 -0000

Hi All,

Last night (at 4 am actually) I was explaining to someone on the were-here
forums how to get around a problem. He wanted to target a scene from a
button inside a movieclip, which doesn't seem to work terribly well (if at
all). I posted a fairly simple solution for him.

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 (sceneRef, frameRef)

_root.gotoAndPlay (sceneRef, frameRef);
}

In your button you put:

on (release)

_root.playScene ("your_scene_name_here", frame_number_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 (sceneName, frameName);
}

We've told it to gotoAndPlay a scene. But we know that we have to add the
sceneName and frameName. 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(sceneRef, frameRef)

_root.gotoAndPlay (sceneRef, 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 amtch 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 scene name is (by setting the variable sceneRef)
and what the frame name 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 scene
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
scene name and frame number to go to. So we do

_root.playScene ("Mission","start");

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

_root.playScene ("Impossible",1);

and again and again.... without ever having to rewrite the code that we
defined in the function. Notice, that I can use either a label "start" or a
frame number "1". Flash doesn't care... whatever you put in there will be
used inside the function itself. Of course... if you put in something that
doesn't work (like a label that doesn't exist) nothing will happen! But the
function can't do all the work for you now can it?

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






Replies
  RE: Functions 101, AndyA

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