Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "“But I don't want to be a “programmer!” ActionScript for journalists Presentation by Mindy McAdams Flashjournalism.com."— Presentation transcript:

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

2 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

3 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; }

4 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

5 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

6 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

7 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

8 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();

9 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

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

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

12 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();

13 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

14 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”

15 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”

16 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; ”

17 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

18 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

19 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 setInterval s at the same time

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

21 What does setInterval do?  Use it to determine how often a function will execute timer = 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”

22 The End Presentation by Mindy McAdams Flashjournalism.com


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

Similar presentations


Ads by Google