1 / 15

ActionScript: Functions, Parameters, Functions that return a value, Variables, setInterval Functions

ActionScript: Functions, Parameters, Functions that return a value, Variables, setInterval Functions. MMP 220 Multimedia Programming. This material was prepared for students in MMP220 Multimedia Programming as as part of a curriculum redesign project funded by

chadrick
Télécharger la présentation

ActionScript: Functions, Parameters, Functions that return a value, Variables, setInterval Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ActionScript: Functions, Parameters, Functions that return a value, Variables, setInterval Functions MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia Programming as as part of a curriculum redesign project funded by National Science Foundation grant #0511209 Co PI’s Christopher Stein and Jody Culkin BMCC CUNY http://teachingmultimedia.net

  2. Functions • Functions in ActionScript are a way of grouping a block of code that performs a specific task when that function is called. • Writing functions in ActionScript is similar to writing world or class level methods in Alice.

  3. Advantages of Functions  Code is more readable because repetition (redundancy) is eliminated.  Program is more efficient by reusing functions rather than retyping blocks of code.  Functions are centralized place to make changes in code- changes apply each time function is called. • Well-written functions can be reused in other programs, developers can build a library of functions that can be used again and again. • Encapsulating code in functions provides the basis for user interaction. A user-initiated action can invoke a function, rather than running an application as a single routine.

  4. Writing Custom Functions • Writing a function is called declaring or defining a function. Here is a model of the syntax of a function. function functionName():datatype{ statements }

  5. Here is an example of a custom function called moveClip that moves a movieClip named square_mc 5 pixels each time it is called. function moveClip():Void{ square_mc._x +=5; }

  6. Calling a function Like world or class level methods you defined in Alice, custom functions you define must be called in ActionScript. To call a function, you need the type the name of the function, followed by parentheses, which are called the function call operator followed by a semicolon. moveClip();

  7. Passing Parameters Here is our moveClip function adjusted, with 2 parameters added- one refers to the name of the clip to be moved, one refers to how far the clip will be moved. function moveClip(mWhichClip:MovieClip,nDistance:Number):Void{ mWhichClip._x +=nDistance; }

  8. Call to function with parameters As you can see, when we define our parameters, we declare their datatype. Here is the call to the function with the parameters passed in: moveClip(square_mc, 5);

  9. Creating a Function that returns a value When we were using Alice, we used Alice’s primitive functions to ask questions about things, like distances for example. You can define a function that returns a value, using the keyword return. Here is an example that adds 2 numbers: function addThem(nA:Number, nB:Number){ var nSum = nA + nB; return nSum; }

  10. Function Call Here we store the value returned by addThem in a variable. var nTotal:Number = addThem(340 + 220);

  11. Variables • A variable is a container or placeholder for a value. • Variables can hold all different datatypes- Boolean, Numbers, Strings, Objects. var grow:Boolean = true;

  12. Variable Scope • Scope is the area within something is defined within ActionScript. In this context, we are talking about variables. • Variables can have the scope (retain their value) of an entire movie, on a particular timeline, or in a particular function. A local variable is defined within a function and does not retain its value outside of that function.

  13. Creating Interval Functions • By using the setInterval() command, you can specify a function and an interval (in milliseconds) on which the function can be continually called. setInterval(moveClip,25,square_mc, 5);

  14. In the following example, our setInterval function is stored in a variable, so we can call clearInterval when we want to stop our function from being called. var nInterval:Number = setInterval(moveClip,25,square_mc, 5); To stop the function from being called, we would clear the interval like so: ClearInterval(nInterval);

  15. Assignment • Read Chapter 4 of the ActionScript Bible • Write a custom function and call it from an event handler method. You might consider writing a function that returns a value. Due Thursday April. 5.

More Related