“But I don't want to be a “programmer!” ActionScript for journalists Presentation by Mindy...

22
“But I don't want to be a programmer!” ActionScript for journalists Presentation by Mindy McAdams Flashjournalism.com

Transcript of “But I don't want to be a “programmer!” ActionScript for journalists Presentation by Mindy...

“But I don't want to be a “programmer!”

ActionScript for journalistsPresentation by Mindy McAdamsFlashjournalism.com

Functions in scripting languages

A function is a self-contained script that can be executed on demand, and which does not run until it is told to run

Actions you have already seen — such as stop() and play() — can be contained inside a function

Conventions of a function

In Exercise 8.7, you see a typical function for the first time in this book (“Synchronizing images to loaded audio”)

A typical ActionScript function looks like this:function nameOfFunction() {

stuff here;

}

Using a function

The function:function nameOfFunction() {

stuff here;}

Calling the function (later):nameOfFunction();

When you call a function, whatever script is inside { } will be executed

Choosing a name for a function

The name of a function must not be any of the reserved words in ActionScript (see pp. 479-480)

This is the reason why you see some strange names for functions, such as loadTheMovie – because you cannot name a function “load,” or “loadMovie”

ActionScript would be “confused” if you used a reserved word to name a function

Similarity to other languages

ActionScript is based on an international standard known as ECMAscript

JavaScript is also based on ECMAscript ActionScript has many similarities to

JavaScript, but they are NOT the same thing!

Don’t assume you can use terms from JavaScript

Look up the ActionScript methods, etc., in the Flash Help files

When to create a function

You realize you will need to do the same thing more than once in the Flash movie

The one function can be called many times

A function can be called on a frame, or on a button

Example of a function

Say you want a function to pause a Sound object (stored in a variable named x)

Write the function (once):function pauseMySound() {

p = Math.floor(x.position/1000);

x.stop;

} Call the function (many times):pauseMySound();

Define variables outside functions

There are two variables in this function:function pauseMySound() {

p = Math.floor(x.position/1000);x.stop;

} You must declare the variables BEFORE the

function, and OUTSIDE of the {}var p = 0;var x = new Sound();

Remember: A variable is declared (with var) only ONCE in a movie

Passing a variable to a function

If you want the function to be performed on different variables in different parts of your movie

Write the function (once):function pauseMySound(theSound) {

p = Math.floor(theSound.position/1000);

theSound.stop;

} Call the function (in this case, only for x):pauseMySound(x);

Passing a variable (2)

If you have something in parens here:

function pauseMySound(theSound) {p =

Math.floor(theSound.position/1000);

theSound.stop;

} You MUST put an existing variable name

in the parens when you call the function:

pauseMySound(x);

NOT passing a variable

If you have nothing in parens here:

function pauseMySound() {p = Math.floor(x.position/1000);

x.stop;

} You MUST NOT put anything in the parens

when you call the function:

pauseMySound();

Things we do with functions

Test a condition (if … then)

Do something again and again until some condition is met Usually with a loop Or with … onEnterFrame Or with … setInterval

Testing a condition

The condition (if) is defined in parens; for example:if (x.getVolume() > 0)

The result (meaning “then do this”) is contained in curly braces:{ x.setVolume(0); }

This says: “If the current volume of sound x is greater than zero, then set the volume of x to zero”

Testing a condition (2)

The function:function volumeToZero() {

if (x.getVolume() > 0) {

x.setVolume(0);

}

} This says: “If the current volume of

sound x is greater than zero, then set the volume of x to zero”

Adding “else” to a condition

function changeMyVolume () {if (x.getVolume() > 0) {

x.setVolume(0); } else {

x.setVolume(100); }

} This says: “If the current volume of

sound x is greater than zero, then set the volume of x to zero; otherwise, set the volume of x to 100; ”

Running a “for” loop

There are different kinds of loops in scripting; a “for” loop is very common

The beginning of a “for” loop:for (i=0; i<=7; i++) { stuff; }

This means: The initial value of i is zero For as long as i is less than or equal to 7 … Increment i (that is, add 1 to i)

The initial value of i can be anything The condition can be anything You can decrement or increment i

Use of onEnterFrame

Explained in the book, pp. 213-214, and in Exercise 10.5, Step 6

If you use _root.onEnterFrame -- You must eventually stop or cancel

it (otherwise, disaster!):delete _root.onEnterFrame;

Never try to use two simultaneous instances of _root.onEnterFrame

Use of setInterval

Explained in the book, Exercise 10.7, Step 9

If you use setInterval -- You must eventually stop or cancel

it (otherwise, disaster!) To stop a setInterval you must

first name the interval; this means you CAN run two or more setIntervals at the same time

Naming and clearing setInterval

Say you have created a function named “changePhoto”

To use setInterval with that function:timer = setInterval(changePhoto, 3000);

You have named the interval “timer” To stop this setInterval --clearInterval(timer);

What does setInterval do?

Use it to determine how often a function will executetimer = setInterval(changePhoto, 3000);

3000 is 3 seconds Usually we combine setInterval

with a condition, e.g., “If we have shown fewer than 10 photos, then show a new photo after 3 seconds have passed”

The End

Presentation by Mindy McAdamsFlashjournalism.com