Action Sci Rpt 1 Handle

Embed Size (px)

Citation preview

  • 8/8/2019 Action Sci Rpt 1 Handle

    1/27

    Getting a handle on ActionScript

    A basic primer for non-programmers

  • 8/8/2019 Action Sci Rpt 1 Handle

    2/27

    Intermediate Flash with ActionScript 2

    ActionScript

    Similar to JavaScript

    Reusable pieces of code

    Action panel automates process Somewhat accessible for non-programmers

    Flash MX 2004 Utilizes ActionScript 2.0

    syntax

  • 8/8/2019 Action Sci Rpt 1 Handle

    3/27

    Intermediate Flash with ActionScript 3

    Variables

    Containers for data types

    Comparison in Flash MX 2004

    Symbols are containers for different objects you create

    Variables are containers you create with ActionScript

    They store the values of the different objects you create

    They can change dynamically

    You can see the value in Flash my using the trace function

    Example trace (VariableName); Trace also used as a debugging tool

  • 8/8/2019 Action Sci Rpt 1 Handle

    4/27

    Intermediate Flash with ActionScript 4

    Objects and Classes

    Classes are the blueprint to define how

    objects behave.

    Example: Dog is a member of the animal class.

    Dog is the object, Animal is the class.

    All objects are members of a Class and are

    instances of that Class.

    Objects are data types such as sound,graphics, text, numeric values

  • 8/8/2019 Action Sci Rpt 1 Handle

    5/27

    Intermediate Flash with ActionScript 5

    Properties

    Each class has a pre-defined set of

    properties.

    Each object of a class can have its ownvalues set to its properties.

    Example; Movie Clip properties include:

    _height, _width, _rotation

    You can define and change the properties ofeach object (instance) of a Class through

    ActionScript.

  • 8/8/2019 Action Sci Rpt 1 Handle

    6/27

    Intermediate Flash with ActionScript 6

    Methods

    The things objects can do

    Example; Sound class has a setVolume method

    When an object does something using amethod we say the method is called or that

    the object calls the method

    You can add your own methods

  • 8/8/2019 Action Sci Rpt 1 Handle

    7/27

    Intermediate Flash with ActionScript 7

    Objects requiring construction

    To use certain objects within a Class you

    must create an instance of the Class by

    giving it a name (instantiating)myColor = new Color();

    myDate = new Date ();

    mySound = new Sound();

  • 8/8/2019 Action Sci Rpt 1 Handle

    8/27

    Intermediate Flash with ActionScript 8

    Calling a method

    The next step involves calling an objects

    methods or evaluating and assigning new

    properties. You can call a method by entering

    an expression using dot syntax

    Example:

    // construct the object

    myDate = new Date ();// call the method of the constructed object

    myDisplay = myDate.getdate();

  • 8/8/2019 Action Sci Rpt 1 Handle

    9/27

    Intermediate Flash with ActionScript 9

    Dot Syntax

    Statements use dot syntax to put together

    objects, properties, and methods

    Example; movieclip1._rotation = 45 OBJECT PROPERTY VALUE

    We use multiple dots to maintain object hierarchy

  • 8/8/2019 Action Sci Rpt 1 Handle

    10/27

    Intermediate Flash with ActionScript 10

    Dot Syntax

    Methods are called in the same way

    Example; mouse. gotoAndPlay (scene1, 20)

    The parenthesis after gotoAndPlay signifies amethod rather than a property

    The statement inside the parenthesis is an

    argument or a parameter

  • 8/8/2019 Action Sci Rpt 1 Handle

    11/27

    Intermediate Flash with ActionScript 11

    More punctuation

    Semicolon functions as a period does in asentence Example:

    stopAllSounds();play();

    Curly braces group together related blocks ofActionScript statements

    on (release) {stopAllSounds();

    play();

    }

  • 8/8/2019 Action Sci Rpt 1 Handle

    12/27

    Intermediate Flash with ActionScript 12

    Actions Panel

    Type as in a text-editing application

    When you use ActionScript 2.0 syntax you

    get Strict Typing

  • 8/8/2019 Action Sci Rpt 1 Handle

    13/27

    Intermediate Flash with ActionScript 13

    Flash MX 2004

    ActionScript Categories

  • 8/8/2019 Action Sci Rpt 1 Handle

    14/27

    Intermediate Flash with ActionScript 14

    ActionScript 2.0

    More like other programming languages

    Strict Typing

    Optional Good for troubleshooting code

    Opens up code hinting window

    Example:

    var myVar:Number Tells Flash content of variable is a particular data type

    Using var is not required but tells Flash you are declaring a

    variable

  • 8/8/2019 Action Sci Rpt 1 Handle

    15/27

    Santa Rosa Junior College

    I

    ntermediate Flash with ActionScriptJeffrey Diamond C IS 75.31A

    Functions

  • 8/8/2019 Action Sci Rpt 1 Handle

    16/27

    Intermediate Flash with ActionScript 16

    Characteristics of Functions

    Functions hold other actions

    Allows you to control when functions are

    executed when you call the function

  • 8/8/2019 Action Sci Rpt 1 Handle

    17/27

    Intermediate Flash with ActionScript 17

    Advantages of Functions

    Flexible

    Convenient

    Reusable

    Centralized

    Created in one place but executed fromanywhere in the movie.

    Ex.- three buttons in three MCs with the samepurpose. We can put one copy in a function onthe main timeline and invoke it from each buttonas needed.

  • 8/8/2019 Action Sci Rpt 1 Handle

    18/27

    Intermediate Flash with ActionScript 18

    Functions Fundamentals

    Declaration- creating them

    Invocation- calling the function

    Arguments and parameters- providing thedata to manipulate

    Termination- ending the execution

    Scope- determining the availability and

    lifespan

  • 8/8/2019 Action Sci Rpt 1 Handle

    19/27

    Intermediate Flash with ActionScript 19

    Creating the function

    We need a function name and a block of

    statement to perform

    function functionName () {

    Statement one;

    Statement two;

    Statement three;

    }

    Creating the function does NOT execute the

    function

  • 8/8/2019 Action Sci Rpt 1 Handle

    20/27

    Intermediate Flash with ActionScript 20

    Running or invoking the function

    Simply proclaim the functions name

    (invocation)

    on (release) {

    functionName ();

    }

  • 8/8/2019 Action Sci Rpt 1 Handle

    21/27

    Intermediate Flash with ActionScript 21

    Function Availability

    Directly accessible for invocation from:

    Code or button attached to timeline of movie clip

    that bears the function declaration

    Indirectly accessible using dot syntax

    Ex.- myClip.myOtherClip.myfunction();

  • 8/8/2019 Action Sci Rpt 1 Handle

    22/27

    Intermediate Flash with ActionScript 22

    Passing information to functions

    Statements

    function moveBall(){

    ball._x +=10;

    ball._y +=10;

    }

  • 8/8/2019 Action Sci Rpt 1 Handle

    23/27

    Intermediate Flash with ActionScript 23

    Creating functions with parameters

    Parameters are the variables that can be usedwithin the function

    We provide a list of identifiers between the

    parenthesis of the function declarationfunction moveClip (theClip, xDist, yDist) {theClip._x += xDist;

    theClip._y +=yDist;

    }

    We replace hard-coded values = more flexibility Allows you to modify how all the actions within that function

    behave

  • 8/8/2019 Action Sci Rpt 1 Handle

    24/27

    Intermediate Flash with ActionScript 24

    Invoking functions with parameters

    To pass multiple parameters we separate

    them with commas

    Each of the parameters is assigned as the

    value of the corresponding parameter named

    in the function declaration

    Example:

    on (release){moveClip (clip1, 100, 200)

    }

  • 8/8/2019 Action Sci Rpt 1 Handle

    25/27

    Intermediate Flash with ActionScript 25

    Terminating a function

    Done with a return statement

    ActionScript does it by default at the end of a

    function invocation if no return statement is

    there

    Otherwise you can use return to prematurely

    end a function (usually done in the context of

    an if then statement)

  • 8/8/2019 Action Sci Rpt 1 Handle

    26/27

    Intermediate Flash with ActionScript 26

    Returning values from functions

    Return terminates a function but it can also

    send a value back to the script that invoked

    the function

    return expression

  • 8/8/2019 Action Sci Rpt 1 Handle

    27/27

    Intermediate Flash with ActionScript 27

    Function Lifespan

    A function defined on a movie clip timeline is

    lost when that clip is removed from the stage.

    Defining a function on the MTL is the best

    way to insure the functions permanence